input Getenvs to FlagXYZ

master
bel 2023-03-25 10:58:13 -06:00
parent ae1e32391c
commit a1a12b1873
9 changed files with 45 additions and 20 deletions

View File

@ -6,6 +6,10 @@ import (
"os"
)
var (
FlagButtonV01 = os.Getenv("BUTTON_V01") == "true"
)
type Parser interface {
Read() []Button
Close()
@ -13,7 +17,7 @@ type Parser interface {
}
func New(ctx context.Context, src raw.Raw) Parser {
if os.Getenv("BUTTON_PARSER_V01") == "true" {
if FlagButtonV01 {
return NewV01(ctx, src)
}
return NewPlaintext(src)

View File

@ -5,6 +5,10 @@ import (
"os"
)
var (
FlagButtonPlaintextRelease = os.Getenv("BUTTON_PLAINTEXT_RELEASE")
)
type Plaintext struct {
src raw.Raw
release byte
@ -12,8 +16,8 @@ type Plaintext struct {
func NewPlaintext(src raw.Raw) Plaintext {
releaseChar := byte('!')
if v := os.Getenv("BUTTON_PLAINTEXT_RELEASE"); v != "" {
releaseChar = byte(v[0])
if FlagButtonPlaintextRelease != "" {
releaseChar = byte(FlagButtonPlaintextRelease[0])
}
return Plaintext{
src: src,

View File

@ -12,7 +12,10 @@ import (
"gopkg.in/yaml.v2"
)
var debugging = os.Getenv("DEBUG") == "true"
var (
FlagDebug = os.Getenv("DEBUG") == "true"
FlagButtonV01Config = os.Getenv("BUTTON_V01_CONFIG")
)
type (
V01 struct {
@ -41,7 +44,7 @@ type (
func NewV01(ctx context.Context, src raw.Raw) V01 {
var cfg v01Cfg
b, _ := ioutil.ReadFile(os.Getenv("BUTTON_PARSER_V01_CONFIG"))
b, _ := ioutil.ReadFile(FlagButtonV01Config)
yaml.Unmarshal(b, &cfg)
ctx, can := context.WithCancel(ctx)
return V01{
@ -99,7 +102,7 @@ func (t v01Transformation) pipe(s string) string {
}
func (v01 V01) telemetry(msg v01Msg) {
if debugging {
if FlagDebug {
log.Printf("%s|%dms", msg.U, time.Now().UnixNano()/int64(time.Millisecond)-msg.T)
}
}
@ -112,7 +115,7 @@ func (msg v01Msg) buttons() []Button {
for i := range msg.N {
buttons[len(msg.Y)+i] = Button{Char: msg.N[i], Down: false}
}
if debugging {
if FlagDebug {
log.Printf("%+v", msg)
}
return buttons

View File

@ -46,7 +46,7 @@ func TestV01WithCfg(t *testing.T) {
- transformation:
w: i
`), os.ModePerm)
os.Setenv("BUTTON_PARSER_V01_CONFIG", p)
button.FlagButtonV01Config = p
t.Run("unknown user ignored", func(t *testing.T) {
v01 := button.NewV01(context.Background(), constSrc(`{"U":"qt","Y":"w"}`))

View File

@ -3,6 +3,7 @@ package input_test
import (
"context"
"mayhem-party/src/device/input"
"mayhem-party/src/device/input/raw"
"mayhem-party/src/device/input/wrap"
"os"
"path"
@ -32,10 +33,10 @@ func TestNewRemapped(t *testing.T) {
}
wrap.FlagRemapFile = remap
os.Setenv("RAW_RANDOM_WEIGHT_FILE", rand)
raw.FlagRawRandomWeightFile = rand
t.Cleanup(func() {
wrap.FlagRemapFile = ""
os.Unsetenv("RAW_RANDOM_WEIGHT_FILE")
raw.FlagRawRandomWeightFile = ""
})
r := input.New(context.Background())
@ -72,9 +73,9 @@ func TestNewRandomWeightFile(t *testing.T) {
t.Fatal(err)
}
os.Setenv("RAW_RANDOM_WEIGHT_FILE", p)
raw.FlagRawRandomWeightFile = p
t.Cleanup(func() {
os.Unsetenv("RAW_RANDOM_WEIGHT_FILE")
raw.FlagRawRandomWeightFile = ""
})
r := input.New(context.Background())

View File

@ -50,7 +50,7 @@ func (kb Keyboard) Read() []byte {
if err != nil && err != io.EOF {
panic(err)
}
if os.Getenv("DEBUG") == "true" {
if FlagDebug {
log.Printf("raw.Keyboard.Read() %s", b[:n])
}
return b[:n]

View File

@ -6,21 +6,27 @@ import (
"strconv"
)
var (
FlagRawKeyboard = os.Getenv("RAW_KEYBOARD") == "true"
FlagRawUDP = os.Getenv("RAW_UDP")
FlagRawRandomWeightFile = os.Getenv("RAW_RANDOM_WEIGHT_FILE")
)
type Raw interface {
Read() []byte
Close()
}
func New(ctx context.Context) Raw {
if os.Getenv("RAW_KEYBOARD") == "true" {
if FlagRawKeyboard {
return NewKeyboard()
}
if port, _ := strconv.Atoi(os.Getenv("RAW_UDP")); port != 0 {
if port, _ := strconv.Atoi(FlagRawUDP); port != 0 {
return NewUDP(ctx, port)
}
generator := randomCharFromRange('a', 'g')
if p := os.Getenv("RAW_RANDOM_WEIGHT_FILE"); p != "" {
generator = randomCharFromWeightFile(p)
if FlagRawRandomWeightFile != "" {
generator = randomCharFromWeightFile(FlagRawRandomWeightFile)
}
return NewRandom(generator)
}

View File

@ -8,6 +8,10 @@ import (
"strconv"
)
var (
FlagDebug = os.Getenv("DEBUG") == "true"
)
type UDP struct {
conn net.PacketConn
c chan []byte
@ -29,14 +33,13 @@ func NewUDP(ctx context.Context, port int) UDP {
}
func (udp UDP) listen() {
debugging := os.Getenv("DEBUG") == "true"
for udp.ctx.Err() == nil {
buff := make([]byte, 256)
n, _, err := udp.conn.ReadFrom(buff)
if err != nil && udp.ctx.Err() == nil {
panic(err)
}
if debugging {
if FlagDebug {
log.Printf("raw.UDP.Read() => %s", buff[:n])
}
select {

View File

@ -9,6 +9,10 @@ import (
"time"
)
var (
FlagBufferedStickyDuration = os.Getenv("WRAP_BUFFERED_STICKY_DURATION")
)
type Buffered struct {
ctx context.Context
can context.CancelFunc
@ -22,7 +26,7 @@ type Buffered struct {
func NewBuffered(ctx context.Context, input Wrap) *Buffered {
ctx, can := context.WithCancel(ctx)
expirationInterval := time.Millisecond * 125
if d, err := time.ParseDuration(os.Getenv("WRAP_BUFFERED_STICKY_DURATION")); err == nil {
if d, err := time.ParseDuration(FlagBufferedStickyDuration); err == nil {
expirationInterval = d
}
result := &Buffered{