Refactor global flags
This commit is contained in:
		@@ -30,8 +30,6 @@ func NewCmdBreathe() Command {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			fs.Float64("peak", lifx.DefaultBreathePeak, "Defines where in a period the target color is at its maximum (min: 0.0, max: 1.0)")
 | 
								fs.Float64("peak", lifx.DefaultBreathePeak, "Defines where in a period the target color is at its maximum (min: 0.0, max: 1.0)")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fs.String("format", defaultOutputFormat, "Set the output format")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return fs
 | 
								return fs
 | 
				
			||||||
		}(),
 | 
							}(),
 | 
				
			||||||
		Use:   "[--selector <selector>] --color <color> [--from-color <color>] [--period <period>] [--cycles <cycles>] [--persist <persist>] [--power-on] [--peak <peak>]",
 | 
							Use:   "[--selector <selector>] --color <color> [--from-color <color>] [--period <period>] [--cycles <cycles>] [--persist <persist>] [--power-on] [--peak <peak>]",
 | 
				
			||||||
@@ -45,7 +43,7 @@ func BreatheCmd(ctx Context) (int, error) {
 | 
				
			|||||||
	c := ctx.Client
 | 
						c := ctx.Client
 | 
				
			||||||
	breathe := lifx.NewBreathe()
 | 
						breathe := lifx.NewBreathe()
 | 
				
			||||||
	selector := ctx.Flags.String("selector")
 | 
						selector := ctx.Flags.String("selector")
 | 
				
			||||||
	format := ctx.Flags.String("format")
 | 
						format := ctx.Flags.String("output-format")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if format == "" && ctx.Config.OutputFormat != "" {
 | 
						if format == "" && ctx.Config.OutputFormat != "" {
 | 
				
			||||||
		format = ctx.Config.OutputFormat
 | 
							format = ctx.Config.OutputFormat
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -81,6 +81,13 @@ func RegisterCommand(cmd Command) error {
 | 
				
			|||||||
	if _, ok := commandRegistry[cmd.Name]; ok {
 | 
						if _, ok := commandRegistry[cmd.Name]; ok {
 | 
				
			||||||
		return fmt.Errorf("%s command is already registered")
 | 
							return fmt.Errorf("%s command is already registered")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if cmd.Flags == nil {
 | 
				
			||||||
 | 
							cmd.Flags = flag.NewFlagSet(cmd.Name, flag.ExitOnError)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mergeGlobalFlags(cmd.Flags)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	commandRegistry[cmd.Name] = cmd
 | 
						commandRegistry[cmd.Name] = cmd
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -89,3 +96,10 @@ func GetCommand(name string) (Command, bool) {
 | 
				
			|||||||
	cmd, ok := commandRegistry[name]
 | 
						cmd, ok := commandRegistry[name]
 | 
				
			||||||
	return cmd, ok
 | 
						return cmd, ok
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func mergeGlobalFlags(fs *flag.FlagSet) {
 | 
				
			||||||
 | 
						fs.Bool("debug", false, "Enable debug mode")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						outputFormat := fs.String("output-format", defaultOutputFormat, "Set the output format")
 | 
				
			||||||
 | 
						fs.StringVar(outputFormat, "o", defaultOutputFormat, "Set the output format")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,8 +14,6 @@ func NewCmdLs() Command {
 | 
				
			|||||||
			selector := fs.String("selector", defaultSelector, "Set the selector")
 | 
								selector := fs.String("selector", defaultSelector, "Set the selector")
 | 
				
			||||||
			fs.StringVar(selector, "s", defaultSelector, "Set the selector")
 | 
								fs.StringVar(selector, "s", defaultSelector, "Set the selector")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fs.String("format", defaultOutputFormat, "Set the output format")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return fs
 | 
								return fs
 | 
				
			||||||
		}(),
 | 
							}(),
 | 
				
			||||||
		Use:   "[--selector=<selector>]",
 | 
							Use:   "[--selector=<selector>]",
 | 
				
			||||||
@@ -28,7 +26,7 @@ func LsCmd(ctx Context) (int, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	c := ctx.Client
 | 
						c := ctx.Client
 | 
				
			||||||
	selector := ctx.Flags.String("selector")
 | 
						selector := ctx.Flags.String("selector")
 | 
				
			||||||
	format := ctx.Flags.String("format")
 | 
						format := ctx.Flags.String("output-format")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if format == "" && ctx.Config.OutputFormat != "" {
 | 
						if format == "" && ctx.Config.OutputFormat != "" {
 | 
				
			||||||
		format = ctx.Config.OutputFormat
 | 
							format = ctx.Config.OutputFormat
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,8 +19,6 @@ func NewCmdPoweroff() Command {
 | 
				
			|||||||
			selector := fs.String("selector", defaultSelector, "Set the selector")
 | 
								selector := fs.String("selector", defaultSelector, "Set the selector")
 | 
				
			||||||
			fs.StringVar(selector, "s", defaultSelector, "Set the selector")
 | 
								fs.StringVar(selector, "s", defaultSelector, "Set the selector")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fs.String("format", defaultOutputFormat, "Set the output format")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return fs
 | 
								return fs
 | 
				
			||||||
		}(),
 | 
							}(),
 | 
				
			||||||
		Use:   "[--selector <selector>] [--duration <sec>]",
 | 
							Use:   "[--selector <selector>] [--duration <sec>]",
 | 
				
			||||||
@@ -34,7 +32,7 @@ func PoweroffCmd(ctx Context) (int, error) {
 | 
				
			|||||||
	c := ctx.Client
 | 
						c := ctx.Client
 | 
				
			||||||
	duration := ctx.Flags.Float64("duration")
 | 
						duration := ctx.Flags.Float64("duration")
 | 
				
			||||||
	selector := ctx.Flags.String("selector")
 | 
						selector := ctx.Flags.String("selector")
 | 
				
			||||||
	format := ctx.Flags.String("format")
 | 
						format := ctx.Flags.String("output-format")
 | 
				
			||||||
	state := lifx.State{Power: "off", Duration: duration}
 | 
						state := lifx.State{Power: "off", Duration: duration}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if format == "" && ctx.Config.OutputFormat != "" {
 | 
						if format == "" && ctx.Config.OutputFormat != "" {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,8 +19,6 @@ func NewCmdPoweron() Command {
 | 
				
			|||||||
			selector := fs.String("selector", defaultSelector, "Set the selector")
 | 
								selector := fs.String("selector", defaultSelector, "Set the selector")
 | 
				
			||||||
			fs.StringVar(selector, "s", defaultSelector, "Set the selector")
 | 
								fs.StringVar(selector, "s", defaultSelector, "Set the selector")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fs.String("format", defaultOutputFormat, "Set the output format")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return fs
 | 
								return fs
 | 
				
			||||||
		}(),
 | 
							}(),
 | 
				
			||||||
		Use:   "[--selector <selector>] [--duration <sec>]",
 | 
							Use:   "[--selector <selector>] [--duration <sec>]",
 | 
				
			||||||
@@ -34,7 +32,7 @@ func PoweronCmd(ctx Context) (int, error) {
 | 
				
			|||||||
	c := ctx.Client
 | 
						c := ctx.Client
 | 
				
			||||||
	duration := ctx.Flags.Float64("duration")
 | 
						duration := ctx.Flags.Float64("duration")
 | 
				
			||||||
	selector := ctx.Flags.String("selector")
 | 
						selector := ctx.Flags.String("selector")
 | 
				
			||||||
	format := ctx.Flags.String("format")
 | 
						format := ctx.Flags.String("output-format")
 | 
				
			||||||
	state := lifx.State{Power: "on", Duration: duration}
 | 
						state := lifx.State{Power: "on", Duration: duration}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if format == "" && ctx.Config.OutputFormat != "" {
 | 
						if format == "" && ctx.Config.OutputFormat != "" {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -41,8 +41,6 @@ func NewCmdSetColor() Command {
 | 
				
			|||||||
			fast := fs.Bool("fast", defaultFast, "fast state")
 | 
								fast := fs.Bool("fast", defaultFast, "fast state")
 | 
				
			||||||
			fs.BoolVar(fast, "f", defaultFast, "fast state")
 | 
								fs.BoolVar(fast, "f", defaultFast, "fast state")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fs.String("format", defaultOutputFormat, "Set the output format")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return fs
 | 
								return fs
 | 
				
			||||||
		}(),
 | 
							}(),
 | 
				
			||||||
		Use:   "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
 | 
							Use:   "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
 | 
				
			||||||
@@ -56,7 +54,7 @@ func SetColorCmd(ctx Context) (int, error) {
 | 
				
			|||||||
	c := ctx.Client
 | 
						c := ctx.Client
 | 
				
			||||||
	state := lifx.State{}
 | 
						state := lifx.State{}
 | 
				
			||||||
	selector := ctx.Flags.String("selector")
 | 
						selector := ctx.Flags.String("selector")
 | 
				
			||||||
	format := ctx.Flags.String("format")
 | 
						format := ctx.Flags.String("output-format")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if format == "" && ctx.Config.OutputFormat != "" {
 | 
						if format == "" && ctx.Config.OutputFormat != "" {
 | 
				
			||||||
		format = ctx.Config.OutputFormat
 | 
							format = ctx.Config.OutputFormat
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,8 +34,6 @@ func NewCmdSetState() Command {
 | 
				
			|||||||
			fast := fs.Bool("fast", defaultFast, "fast state")
 | 
								fast := fs.Bool("fast", defaultFast, "fast state")
 | 
				
			||||||
			fs.BoolVar(fast, "f", defaultFast, "fast state")
 | 
								fs.BoolVar(fast, "f", defaultFast, "fast state")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fs.String("format", defaultOutputFormat, "Set the output format")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return fs
 | 
								return fs
 | 
				
			||||||
		}(),
 | 
							}(),
 | 
				
			||||||
		Use:   "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
 | 
							Use:   "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
 | 
				
			||||||
@@ -49,7 +47,7 @@ func SetStateCmd(ctx Context) (int, error) {
 | 
				
			|||||||
	c := ctx.Client
 | 
						c := ctx.Client
 | 
				
			||||||
	state := lifx.State{}
 | 
						state := lifx.State{}
 | 
				
			||||||
	selector := ctx.Flags.String("selector")
 | 
						selector := ctx.Flags.String("selector")
 | 
				
			||||||
	format := ctx.Flags.String("format")
 | 
						format := ctx.Flags.String("output-format")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if format == "" && ctx.Config.OutputFormat != "" {
 | 
						if format == "" && ctx.Config.OutputFormat != "" {
 | 
				
			||||||
		format = ctx.Config.OutputFormat
 | 
							format = ctx.Config.OutputFormat
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,8 +37,6 @@ func NewCmdSetWhite() Command {
 | 
				
			|||||||
			fast := fs.Bool("fast", defaultFast, "fast state")
 | 
								fast := fs.Bool("fast", defaultFast, "fast state")
 | 
				
			||||||
			fs.BoolVar(fast, "f", defaultFast, "fast state")
 | 
								fs.BoolVar(fast, "f", defaultFast, "fast state")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fs.String("format", defaultOutputFormat, "Set the output format")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return fs
 | 
								return fs
 | 
				
			||||||
		}(),
 | 
							}(),
 | 
				
			||||||
		Use:   "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
 | 
							Use:   "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
 | 
				
			||||||
@@ -52,7 +50,7 @@ func SetWhiteCmd(ctx Context) (int, error) {
 | 
				
			|||||||
	c := ctx.Client
 | 
						c := ctx.Client
 | 
				
			||||||
	state := lifx.State{}
 | 
						state := lifx.State{}
 | 
				
			||||||
	selector := ctx.Flags.String("selector")
 | 
						selector := ctx.Flags.String("selector")
 | 
				
			||||||
	format := ctx.Flags.String("format")
 | 
						format := ctx.Flags.String("output-format")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if format == "" && ctx.Config.OutputFormat != "" {
 | 
						if format == "" && ctx.Config.OutputFormat != "" {
 | 
				
			||||||
		format = ctx.Config.OutputFormat
 | 
							format = ctx.Config.OutputFormat
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,8 +17,6 @@ func NewCmdToggle() Command {
 | 
				
			|||||||
			selector := fs.String("selector", defaultSelector, "Set the selector")
 | 
								selector := fs.String("selector", defaultSelector, "Set the selector")
 | 
				
			||||||
			fs.StringVar(selector, "s", defaultSelector, "Set the selector")
 | 
								fs.StringVar(selector, "s", defaultSelector, "Set the selector")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fs.String("format", defaultOutputFormat, "Set the output format")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			return fs
 | 
								return fs
 | 
				
			||||||
		}(),
 | 
							}(),
 | 
				
			||||||
		Use:   "[--selector <selector>] [--duration <sec>]",
 | 
							Use:   "[--selector <selector>] [--duration <sec>]",
 | 
				
			||||||
@@ -32,7 +30,7 @@ func ToggleCmd(ctx Context) (int, error) {
 | 
				
			|||||||
	c := ctx.Client
 | 
						c := ctx.Client
 | 
				
			||||||
	duration := ctx.Flags.Float64("duration")
 | 
						duration := ctx.Flags.Float64("duration")
 | 
				
			||||||
	selector := ctx.Flags.String("selector")
 | 
						selector := ctx.Flags.String("selector")
 | 
				
			||||||
	format := ctx.Flags.String("format")
 | 
						format := ctx.Flags.String("output-format")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if format == "" && ctx.Config.OutputFormat != "" {
 | 
						if format == "" && ctx.Config.OutputFormat != "" {
 | 
				
			||||||
		format = ctx.Config.OutputFormat
 | 
							format = ctx.Config.OutputFormat
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user