input Getenvs to FlagXYZ

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

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 {