diff --git a/cmd/command.go b/cmd/command.go index 8d78ecc..2a14f2b 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -13,7 +13,7 @@ const ( ExitFailure ) -type CmdArgs struct { +type Context struct { Flags Flags Args []string Client *lifx.Client @@ -27,7 +27,7 @@ type Flags struct { type Command struct { Name string - Func func(CmdArgs) (int, error) + Func func(Context) (int, error) Flags *flag.FlagSet Use string Short string diff --git a/cmd/help.go b/cmd/help.go index 3430d65..ad5ff60 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -14,11 +14,11 @@ func NewCmdHelp() Command { } } -func HelpCmd(args CmdArgs) (int, error) { - if len(args.Args) == 0 { +func HelpCmd(ctx Context) (int, error) { + if len(ctx.Args) == 0 { printHelp(commandRegistry) - } else if len(args.Args) >= 1 { - printCmdHelp(args.Args[0]) + } else if len(ctx.Args) >= 1 { + printCmdHelp(ctx.Args[0]) } return ExitSuccess, nil diff --git a/cmd/ls.go b/cmd/ls.go index c071188..713d308 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -23,15 +23,15 @@ func NewCmdLs() Command { } } -func LsCmd(args CmdArgs) (int, error) { +func LsCmd(ctx Context) (int, error) { var p Printer - c := args.Client - selector := args.Flags.String("selector") - format := args.Flags.String("format") + c := ctx.Client + selector := ctx.Flags.String("selector") + format := ctx.Flags.String("format") - if format == "" && args.Config.OutputFormat != "" { - format = args.Config.OutputFormat + if format == "" && ctx.Config.OutputFormat != "" { + format = ctx.Config.OutputFormat } lights, err := c.ListLights(selector) diff --git a/cmd/main.go b/cmd/main.go index ae986a4..9668561 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -57,7 +57,7 @@ func Main(args []string) (int, error) { lifx.WithUserAgent(config.userAgent), ) - cmdArgs := CmdArgs{ + Context := Context{ Client: c, Config: *config, Args: args[2:], @@ -72,11 +72,11 @@ func Main(args []string) (int, error) { fs := cmd.Flags if fs != nil { fs.Parse(args[2:]) - cmdArgs.Flags = Flags{FlagSet: fs} + Context.Flags = Flags{FlagSet: fs} } - cmdArgs.Name = command + Context.Name = command - exitCode, err := cmd.Func(cmdArgs) + exitCode, err := cmd.Func(Context) if err != nil { err = fmt.Errorf("fatal: %s", err) } diff --git a/cmd/poweroff.go b/cmd/poweroff.go index c094b33..f8e2b21 100644 --- a/cmd/poweroff.go +++ b/cmd/poweroff.go @@ -28,17 +28,17 @@ func NewCmdPoweroff() Command { } } -func PoweroffCmd(args CmdArgs) (int, error) { +func PoweroffCmd(ctx Context) (int, error) { var p Printer - c := args.Client - duration := args.Flags.Float64("duration") - selector := args.Flags.String("selector") - format := args.Flags.String("format") + c := ctx.Client + duration := ctx.Flags.Float64("duration") + selector := ctx.Flags.String("selector") + format := ctx.Flags.String("format") state := lifx.State{Power: "off", Duration: duration} - if format == "" && args.Config.OutputFormat != "" { - format = args.Config.OutputFormat + if format == "" && ctx.Config.OutputFormat != "" { + format = ctx.Config.OutputFormat } r, err := c.SetState(selector, state) diff --git a/cmd/poweron.go b/cmd/poweron.go index c9054b0..af94a02 100644 --- a/cmd/poweron.go +++ b/cmd/poweron.go @@ -28,17 +28,17 @@ func NewCmdPoweron() Command { } } -func PoweronCmd(args CmdArgs) (int, error) { +func PoweronCmd(ctx Context) (int, error) { var p Printer - c := args.Client - duration := args.Flags.Float64("duration") - selector := args.Flags.String("selector") - format := args.Flags.String("format") + c := ctx.Client + duration := ctx.Flags.Float64("duration") + selector := ctx.Flags.String("selector") + format := ctx.Flags.String("format") state := lifx.State{Power: "on", Duration: duration} - if format == "" && args.Config.OutputFormat != "" { - format = args.Config.OutputFormat + if format == "" && ctx.Config.OutputFormat != "" { + format = ctx.Config.OutputFormat } r, err := c.SetState(selector, state) diff --git a/cmd/setcolor.go b/cmd/setcolor.go index 47a5149..dacec05 100644 --- a/cmd/setcolor.go +++ b/cmd/setcolor.go @@ -50,30 +50,30 @@ func NewCmdSetColor() Command { } } -func SetColorCmd(args CmdArgs) (int, error) { +func SetColorCmd(ctx Context) (int, error) { var p Printer - c := args.Client + c := ctx.Client state := lifx.State{} - selector := args.Flags.String("selector") - format := args.Flags.String("format") + selector := ctx.Flags.String("selector") + format := ctx.Flags.String("format") - if format == "" && args.Config.OutputFormat != "" { - format = args.Config.OutputFormat + if format == "" && ctx.Config.OutputFormat != "" { + format = ctx.Config.OutputFormat } - power := args.Flags.String("power") + power := ctx.Flags.String("power") if power != "" { state.Power = power } - hueFlag := args.Flags.String("hue") - saturationFlag := args.Flags.String("saturation") - rgbFlag := args.Flags.String("rgb") - name := args.Flags.String("name") + hueFlag := ctx.Flags.String("hue") + saturationFlag := ctx.Flags.String("saturation") + rgbFlag := ctx.Flags.String("rgb") + name := ctx.Flags.String("name") if (hueFlag == "" || saturationFlag == "") && rgbFlag == "" && name == "" { - printCmdHelp(args.Name) + printCmdHelp(ctx.Name) return ExitFailure, nil } @@ -81,12 +81,12 @@ func SetColorCmd(args CmdArgs) (int, error) { color := lifx.HSBKColor{} if hueFlag != "" { - hue := args.Flags.Float32("hue") + hue := ctx.Flags.Float32("hue") color.H = lifx.Float32Ptr(hue) } if saturationFlag != "" { - saturation := args.Flags.Float32("saturation") + saturation := ctx.Flags.Float32("saturation") color.S = lifx.Float32Ptr(saturation) } state.Color = color @@ -98,7 +98,7 @@ func SetColorCmd(args CmdArgs) (int, error) { } state.Color = color } else if name != "" { - hsb, ok := args.Config.Colors[name] + hsb, ok := ctx.Config.Colors[name] if !ok { return ExitFailure, fmt.Errorf("%s is not a defined color", name) } @@ -109,16 +109,16 @@ func SetColorCmd(args CmdArgs) (int, error) { state.Color = color } - brightnessFlag := args.Flags.String("brightness") + brightnessFlag := ctx.Flags.String("brightness") if brightnessFlag != "" { - brightness := args.Flags.Float64("brightness") + brightness := ctx.Flags.Float64("brightness") state.Brightness = brightness } - duration := args.Flags.Float64("duration") + duration := ctx.Flags.Float64("duration") state.Duration = duration - fast := args.Flags.Bool("fast") + fast := ctx.Flags.Bool("fast") state.Fast = fast r, err := c.SetState(selector, state) diff --git a/cmd/setstate.go b/cmd/setstate.go index 1e044db..b3a6583 100644 --- a/cmd/setstate.go +++ b/cmd/setstate.go @@ -43,48 +43,48 @@ func NewCmdSetState() Command { } } -func SetStateCmd(args CmdArgs) (int, error) { +func SetStateCmd(ctx Context) (int, error) { var p Printer - c := args.Client + c := ctx.Client state := lifx.State{} - selector := args.Flags.String("selector") - format := args.Flags.String("format") + selector := ctx.Flags.String("selector") + format := ctx.Flags.String("format") - if format == "" && args.Config.OutputFormat != "" { - format = args.Config.OutputFormat + if format == "" && ctx.Config.OutputFormat != "" { + format = ctx.Config.OutputFormat } - power := args.Flags.String("power") + power := ctx.Flags.String("power") if power != "" { state.Power = power } - color := args.Flags.String("color") + color := ctx.Flags.String("color") if color != "" { state.Color = lifx.NamedColor(color) } - brightnessFlag := args.Flags.String("brightness") + brightnessFlag := ctx.Flags.String("brightness") if brightnessFlag != "" { - brightness := args.Flags.Float64("brightness") + brightness := ctx.Flags.Float64("brightness") state.Brightness = brightness } - duration := args.Flags.Float64("duration") + duration := ctx.Flags.Float64("duration") state.Duration = duration - infraredFlag := args.Flags.String("infrared") + infraredFlag := ctx.Flags.String("infrared") if infraredFlag != "" { - infrared := args.Flags.Float64("infrared") + infrared := ctx.Flags.Float64("infrared") state.Infrared = infrared } - fast := args.Flags.Bool("fast") + fast := ctx.Flags.Bool("fast") state.Fast = fast if power == "" && color == "" && brightnessFlag == "" && infraredFlag == "" { - printCmdHelp(args.Name) + printCmdHelp(ctx.Name) return ExitFailure, nil } diff --git a/cmd/setwhite.go b/cmd/setwhite.go index 73870b4..86a14b7 100644 --- a/cmd/setwhite.go +++ b/cmd/setwhite.go @@ -46,26 +46,26 @@ func NewCmdSetWhite() Command { } } -func SetWhiteCmd(args CmdArgs) (int, error) { +func SetWhiteCmd(ctx Context) (int, error) { var p Printer - c := args.Client + c := ctx.Client state := lifx.State{} - selector := args.Flags.String("selector") - format := args.Flags.String("format") + selector := ctx.Flags.String("selector") + format := ctx.Flags.String("format") - if format == "" && args.Config.OutputFormat != "" { - format = args.Config.OutputFormat + if format == "" && ctx.Config.OutputFormat != "" { + format = ctx.Config.OutputFormat } - power := args.Flags.String("power") + power := ctx.Flags.String("power") if power != "" { state.Power = power } - kelvinFlag := args.Flags.String("kelvin") + kelvinFlag := ctx.Flags.String("kelvin") if kelvinFlag != "" { - kelvin := args.Flags.Int16("kelvin") + kelvin := ctx.Flags.Int16("kelvin") color, err := lifx.NewWhite(kelvin) if err != nil { return ExitFailure, err @@ -73,9 +73,9 @@ func SetWhiteCmd(args CmdArgs) (int, error) { state.Color = color } - name := args.Flags.String("name") + name := ctx.Flags.String("name") if name != "" { - name := args.Flags.String("name") + name := ctx.Flags.String("name") color, err := lifx.NewWhiteString(name) if err != nil { return ExitFailure, err @@ -83,26 +83,26 @@ func SetWhiteCmd(args CmdArgs) (int, error) { state.Color = color } - brightnessFlag := args.Flags.String("brightness") + brightnessFlag := ctx.Flags.String("brightness") if brightnessFlag != "" { - brightness := args.Flags.Float64("brightness") + brightness := ctx.Flags.Float64("brightness") state.Brightness = brightness } - duration := args.Flags.Float64("duration") + duration := ctx.Flags.Float64("duration") state.Duration = duration - infraredFlag := args.Flags.String("infrared") + infraredFlag := ctx.Flags.String("infrared") if infraredFlag != "" { - infrared := args.Flags.Float64("infrared") + infrared := ctx.Flags.Float64("infrared") state.Infrared = infrared } - fast := args.Flags.Bool("fast") + fast := ctx.Flags.Bool("fast") state.Fast = fast if power == "" && kelvinFlag == "" && name == "" && brightnessFlag == "" && infraredFlag == "" { - printCmdHelp(args.Name) + printCmdHelp(ctx.Name) return ExitFailure, nil } diff --git a/cmd/show.go b/cmd/show.go index 6e87629..9def9aa 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -24,10 +24,10 @@ func NewCmdShow() Command { } } -func ShowCmd(args CmdArgs) (int, error) { +func ShowCmd(ctx Context) (int, error) { var indent int - c := args.Client - selector := args.Flags.String("selector") + c := ctx.Client + selector := ctx.Flags.String("selector") lights, err := c.ListLights(selector) if err != nil { diff --git a/cmd/toggle.go b/cmd/toggle.go index af24e68..4da47a6 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -26,16 +26,16 @@ func NewCmdToggle() Command { } } -func ToggleCmd(args CmdArgs) (int, error) { +func ToggleCmd(ctx Context) (int, error) { var p Printer - c := args.Client - duration := args.Flags.Float64("duration") - selector := args.Flags.String("selector") - format := args.Flags.String("format") + c := ctx.Client + duration := ctx.Flags.Float64("duration") + selector := ctx.Flags.String("selector") + format := ctx.Flags.String("format") - if format == "" && args.Config.OutputFormat != "" { - format = args.Config.OutputFormat + if format == "" && ctx.Config.OutputFormat != "" { + format = ctx.Config.OutputFormat } r, err := c.Toggle(selector, duration) diff --git a/cmd/version.go b/cmd/version.go index d2df460..e36f32c 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -16,7 +16,7 @@ func NewCmdVersion() Command { } } -func VersionCmd(args CmdArgs) (int, error) { +func VersionCmd(ctx Context) (int, error) { var b strings.Builder fmt.Fprintf(&b, "lume, version %s\n", Version)