diff --git a/src/device/input/button/parser.go b/src/device/input/button/parser.go index 9ca66d2..cbdba8e 100644 --- a/src/device/input/button/parser.go +++ b/src/device/input/button/parser.go @@ -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) diff --git a/src/device/input/button/plaintext.go b/src/device/input/button/plaintext.go index 84df7b8..c6d7d79 100644 --- a/src/device/input/button/plaintext.go +++ b/src/device/input/button/plaintext.go @@ -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, diff --git a/src/device/input/button/v01.go b/src/device/input/button/v01.go index b3da54f..3af599b 100644 --- a/src/device/input/button/v01.go +++ b/src/device/input/button/v01.go @@ -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 diff --git a/src/device/input/button/v01_exported_test.go b/src/device/input/button/v01_exported_test.go index 29a2176..2372d18 100644 --- a/src/device/input/button/v01_exported_test.go +++ b/src/device/input/button/v01_exported_test.go @@ -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"}`)) diff --git a/src/device/input/input_exported_test.go b/src/device/input/input_exported_test.go index 9dd1218..cb438f9 100644 --- a/src/device/input/input_exported_test.go +++ b/src/device/input/input_exported_test.go @@ -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()) diff --git a/src/device/input/raw/keyboard.go b/src/device/input/raw/keyboard.go index f9d53ee..9d5cc3f 100644 --- a/src/device/input/raw/keyboard.go +++ b/src/device/input/raw/keyboard.go @@ -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] diff --git a/src/device/input/raw/raw.go b/src/device/input/raw/raw.go index 27b2d62..bbf1f6f 100644 --- a/src/device/input/raw/raw.go +++ b/src/device/input/raw/raw.go @@ -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) } diff --git a/src/device/input/raw/udp.go b/src/device/input/raw/udp.go index edfc7b4..05b2329 100644 --- a/src/device/input/raw/udp.go +++ b/src/device/input/raw/udp.go @@ -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 { diff --git a/src/device/input/wrap/buffered.go b/src/device/input/wrap/buffered.go index c30a63e..8e30bab 100644 --- a/src/device/input/wrap/buffered.go +++ b/src/device/input/wrap/buffered.go @@ -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{