From 67cd97e2d8fc3a96a54cb1fdb9825bb2ba030775 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sat, 28 Mar 2020 15:22:34 -0500 Subject: [PATCH] store CLI defaults into variables --- cmd/command.go | 12 ++++++++++++ cmd/ls.go | 4 ++-- cmd/setstate.go | 28 ++++++++++++++-------------- cmd/setwhite.go | 28 ++++++++++++++-------------- cmd/toggle.go | 10 ++++++---- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/cmd/command.go b/cmd/command.go index d60f4d2..912a8f3 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -26,6 +26,18 @@ type Command struct { var commandRegistry = make(map[string]Command) +var ( + defaultSelector string = "all" + defaultDuration float64 = 1.0 + defaultPower string = "" + defaultColor string = "" + defaultBrightness string = "" + defaultInfrared string = "" + defaultFast bool = false + defaultWhiteKelvin string = "" + defaultWhiteName string = "" +) + func (f Flags) String(name string) string { return f.FlagSet.Lookup(name).Value.String() } diff --git a/cmd/ls.go b/cmd/ls.go index be3ffd9..00c353e 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -12,8 +12,8 @@ var ( func init() { var cmdName string = "ls" fs := flag.NewFlagSet(cmdName, flag.ExitOnError) - selector := fs.String("selector", "all", "Set the selector") - fs.StringVar(selector, "s", "all", "Set the selector") + selector := fs.String("selector", defaultSelector, "Set the selector") + fs.StringVar(selector, "s", defaultSelector, "Set the selector") RegisterCommand(cmdName, Command{ Func: LsCmd, diff --git a/cmd/setstate.go b/cmd/setstate.go index d509dcc..e28a45b 100644 --- a/cmd/setstate.go +++ b/cmd/setstate.go @@ -12,26 +12,26 @@ func init() { fs := flag.NewFlagSet(cmdName, flag.ExitOnError) - selector := fs.String("selector", "all", "Set the selector") - fs.StringVar(selector, "s", "all", "Set the selector") + selector := fs.String("selector", defaultSelector, "Set the selector") + fs.StringVar(selector, "s", defaultSelector, "Set the selector") - power := fs.String("power", "", "power state") - fs.StringVar(power, "p", "", "power state") + power := fs.String("power", defaultPower, "power state") + fs.StringVar(power, "p", defaultPower, "power state") - color := fs.String("color", "", "color state") - fs.StringVar(color, "c", "", "color state") + color := fs.String("color", defaultColor, "color state") + fs.StringVar(color, "c", defaultColor, "color state") - brightness := fs.String("brightness", "", "brightness state") - fs.StringVar(brightness, "b", "", "brightness state") + brightness := fs.String("brightness", defaultBrightness, "brightness state") + fs.StringVar(brightness, "b", defaultBrightness, "brightness state") - duration := fs.Float64("duration", 1.0, "duration state") - fs.Float64Var(duration, "d", 1.0, "duration state") + duration := fs.Float64("duration", defaultDuration, "duration state") + fs.Float64Var(duration, "d", defaultDuration, "duration state") - infrared := fs.String("infrared", "", "infrared state") - fs.StringVar(infrared, "i", "", "infrared state") + infrared := fs.String("infrared", defaultInfrared, "infrared state") + fs.StringVar(infrared, "i", defaultInfrared, "infrared state") - fast := fs.Bool("fast", false, "fast state") - fs.BoolVar(fast, "f", false, "fast state") + fast := fs.Bool("fast", defaultFast, "fast state") + fs.BoolVar(fast, "f", defaultFast, "fast state") RegisterCommand(cmdName, Command{ Func: SetStateCmd, diff --git a/cmd/setwhite.go b/cmd/setwhite.go index d076554..c953991 100644 --- a/cmd/setwhite.go +++ b/cmd/setwhite.go @@ -15,26 +15,26 @@ func init() { selector := fs.String("selector", "all", "the selector") fs.StringVar(selector, "s", "all", "the selector") - power := fs.String("power", "", "power state") - fs.StringVar(power, "p", "", "power state") + power := fs.String("power", defaultPower, "power state") + fs.StringVar(power, "p", defaultPower, "power state") - kelvin := fs.String("kelvin", "", "kelvin level") - fs.StringVar(kelvin, "k", "", "kelvin level") + kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level") + fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level") - name := fs.String("name", "", "named white level") - fs.StringVar(name, "n", "", "named white level") + name := fs.String("name", defaultWhiteName, "named white level") + fs.StringVar(name, "n", defaultWhiteName, "named white level") - brightness := fs.String("brightness", "", "brightness state") - fs.StringVar(brightness, "b", "", "brightness state") + brightness := fs.String("brightness", defaultBrightness, "brightness state") + fs.StringVar(brightness, "b", defaultBrightness, "brightness state") - duration := fs.Float64("duration", 1.0, "duration state") - fs.Float64Var(duration, "d", 1.0, "duration state") + duration := fs.Float64("duration", defaultDuration, "duration state") + fs.Float64Var(duration, "d", defaultDuration, "duration state") - infrared := fs.String("infrared", "", "infrared state") - fs.StringVar(infrared, "i", "", "infrared state") + infrared := fs.String("infrared", defaultInfrared, "infrared state") + fs.StringVar(infrared, "i", defaultInfrared, "infrared state") - fast := fs.Bool("fast", false, "fast state") - fs.BoolVar(fast, "f", false, "fast state") + fast := fs.Bool("fast", defaultFast, "fast state") + fs.BoolVar(fast, "f", defaultFast, "fast state") RegisterCommand(cmdName, Command{ Func: SetWhiteCmd, diff --git a/cmd/toggle.go b/cmd/toggle.go index ef7e940..a5e0e0c 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -9,10 +9,12 @@ func init() { var cmdName string = "set-state" fs := flag.NewFlagSet(cmdName, flag.ExitOnError) - duration := fs.Float64("duration", 1.0, "Set the duration") - fs.Float64Var(duration, "d", 1.0, "Set the duration") - selector := fs.String("selector", "all", "Set the selector") - fs.StringVar(selector, "s", "all", "Set the selector") + + duration := fs.Float64("duration", defaultDuration, "Set the duration") + fs.Float64Var(duration, "d", defaultDuration, "Set the duration") + + selector := fs.String("selector", defaultSelector, "Set the selector") + fs.StringVar(selector, "s", defaultSelector, "Set the selector") RegisterCommand(cmdName, Command{ Func: ToggleCmd,