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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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