s/CmdArgs/Context/g
This commit is contained in:
parent
88d7d7a0b4
commit
57983b5a52
@ -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
|
||||
|
@ -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
|
||||
|
12
cmd/ls.go
12
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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user