From e5101e25ac5174476c0950199f869445ec94a19b Mon Sep 17 00:00:00 2001 From: Jacalz Date: Wed, 11 Dec 2024 10:14:16 +0100 Subject: [PATCH] Pass down device in cli --- args.go | 9 ++++++--- device/input.go | 2 +- interactive.go | 10 +++++----- main.go | 5 ++++- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/args.go b/args.go index abc7043..d93af1b 100644 --- a/args.go +++ b/args.go @@ -3,6 +3,8 @@ package main import ( "errors" "flag" + + "github.com/Jacalz/hegelmote/device" ) var ( @@ -16,6 +18,7 @@ type arguments struct { ip string port string interactive bool + amplifier device.Device } func parseArguments() (arguments, error) { @@ -24,12 +27,12 @@ func parseArguments() (arguments, error) { // Flags for starting in interactive mode. flag.BoolVar(&args.interactive, "i", false, "starts an interactive command terminal") flag.BoolVar(&args.interactive, "interactive", false, "starts an interactive command terminal") - + flag.UintVar(&args.amplifier, "device", 1, "sets the device to use for input mappings") flag.Parse() - if len(flag.Args()) == 0 { + if flag.NArg() == 0 { return arguments{}, errTooFewArgs - } else if len(flag.Args()) > 2 { + } else if flag.NArg() > 2 { return arguments{}, errTooManyArgs } diff --git a/device/input.go b/device/input.go index df3be11..952ea8e 100644 --- a/device/input.go +++ b/device/input.go @@ -6,7 +6,7 @@ import ( ) // Device defines the Hegel amplifier to target. -type Device uint8 +type Device = uint const ( Röst Device = iota diff --git a/interactive.go b/interactive.go index ab459e3..54d8648 100644 --- a/interactive.go +++ b/interactive.go @@ -14,7 +14,7 @@ import ( var errInvalidCommand = errors.New("invalid command format") -func runInteractiveMode(control *remote.Control) { +func runInteractiveMode(control *remote.Control, amplifier device.Device) { input := bufio.NewScanner(os.Stdin) input.Split(bufio.ScanLines) @@ -28,7 +28,7 @@ func runInteractiveMode(control *remote.Control) { case "volume": handleVolumeCommand(commands[1:], control) case "input", "source": - handleSourceCommand(commands[1:], control) + handleSourceCommand(commands[1:], control, amplifier) case "reset": handleResetCommand(commands[1:], control) case "exit", "quit": @@ -118,7 +118,7 @@ func handleVolumeCommand(subcommands []string, control *remote.Control) { } } -func handleSourceCommand(subcommands []string, control *remote.Control) { +func handleSourceCommand(subcommands []string, control *remote.Control, amplifier device.Device) { switch subcommands[0] { case "set": if len(subcommands) == 1 { @@ -130,7 +130,7 @@ func handleSourceCommand(subcommands []string, control *remote.Control) { err = control.SetSourceNumber(uint(number)) } else { input := strings.Join(subcommands[1:], " ") - err = control.SetSourceName(device.H95, input) + err = control.SetSourceName(amplifier, input) } if err != nil { @@ -142,7 +142,7 @@ func handleSourceCommand(subcommands []string, control *remote.Control) { exitWithError(err) } - source, _ := device.NameFromNumber(device.H95, number) + source, _ := device.NameFromNumber(amplifier, number) fmt.Println("Selected input:", number, "-", source) } } diff --git a/main.go b/main.go index cf7ec89..a2c39c7 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "os" @@ -24,7 +25,9 @@ func main() { } if args.interactive { - runInteractiveMode(command) + runInteractiveMode(command, args.amplifier) return } + + flag.PrintDefaults() }