s/CmdArgs/Context/g
This commit is contained in:
parent
88d7d7a0b4
commit
57983b5a52
@ -13,7 +13,7 @@ const (
|
|||||||
ExitFailure
|
ExitFailure
|
||||||
)
|
)
|
||||||
|
|
||||||
type CmdArgs struct {
|
type Context struct {
|
||||||
Flags Flags
|
Flags Flags
|
||||||
Args []string
|
Args []string
|
||||||
Client *lifx.Client
|
Client *lifx.Client
|
||||||
@ -27,7 +27,7 @@ type Flags struct {
|
|||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Name string
|
Name string
|
||||||
Func func(CmdArgs) (int, error)
|
Func func(Context) (int, error)
|
||||||
Flags *flag.FlagSet
|
Flags *flag.FlagSet
|
||||||
Use string
|
Use string
|
||||||
Short string
|
Short string
|
||||||
|
@ -14,11 +14,11 @@ func NewCmdHelp() Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func HelpCmd(args CmdArgs) (int, error) {
|
func HelpCmd(ctx Context) (int, error) {
|
||||||
if len(args.Args) == 0 {
|
if len(ctx.Args) == 0 {
|
||||||
printHelp(commandRegistry)
|
printHelp(commandRegistry)
|
||||||
} else if len(args.Args) >= 1 {
|
} else if len(ctx.Args) >= 1 {
|
||||||
printCmdHelp(args.Args[0])
|
printCmdHelp(ctx.Args[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
return ExitSuccess, nil
|
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
|
var p Printer
|
||||||
|
|
||||||
c := args.Client
|
c := ctx.Client
|
||||||
selector := args.Flags.String("selector")
|
selector := ctx.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
format := ctx.Flags.String("format")
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = args.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
lights, err := c.ListLights(selector)
|
lights, err := c.ListLights(selector)
|
||||||
|
@ -57,7 +57,7 @@ func Main(args []string) (int, error) {
|
|||||||
lifx.WithUserAgent(config.userAgent),
|
lifx.WithUserAgent(config.userAgent),
|
||||||
)
|
)
|
||||||
|
|
||||||
cmdArgs := CmdArgs{
|
Context := Context{
|
||||||
Client: c,
|
Client: c,
|
||||||
Config: *config,
|
Config: *config,
|
||||||
Args: args[2:],
|
Args: args[2:],
|
||||||
@ -72,11 +72,11 @@ func Main(args []string) (int, error) {
|
|||||||
fs := cmd.Flags
|
fs := cmd.Flags
|
||||||
if fs != nil {
|
if fs != nil {
|
||||||
fs.Parse(args[2:])
|
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 {
|
if err != nil {
|
||||||
err = fmt.Errorf("fatal: %s", err)
|
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
|
var p Printer
|
||||||
|
|
||||||
c := args.Client
|
c := ctx.Client
|
||||||
duration := args.Flags.Float64("duration")
|
duration := ctx.Flags.Float64("duration")
|
||||||
selector := args.Flags.String("selector")
|
selector := ctx.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
format := ctx.Flags.String("format")
|
||||||
state := lifx.State{Power: "off", Duration: duration}
|
state := lifx.State{Power: "off", Duration: duration}
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = args.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := c.SetState(selector, state)
|
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
|
var p Printer
|
||||||
|
|
||||||
c := args.Client
|
c := ctx.Client
|
||||||
duration := args.Flags.Float64("duration")
|
duration := ctx.Flags.Float64("duration")
|
||||||
selector := args.Flags.String("selector")
|
selector := ctx.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
format := ctx.Flags.String("format")
|
||||||
state := lifx.State{Power: "on", Duration: duration}
|
state := lifx.State{Power: "on", Duration: duration}
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = args.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := c.SetState(selector, state)
|
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
|
var p Printer
|
||||||
|
|
||||||
c := args.Client
|
c := ctx.Client
|
||||||
state := lifx.State{}
|
state := lifx.State{}
|
||||||
selector := args.Flags.String("selector")
|
selector := ctx.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
format := ctx.Flags.String("format")
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = args.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
power := args.Flags.String("power")
|
power := ctx.Flags.String("power")
|
||||||
if power != "" {
|
if power != "" {
|
||||||
state.Power = power
|
state.Power = power
|
||||||
}
|
}
|
||||||
|
|
||||||
hueFlag := args.Flags.String("hue")
|
hueFlag := ctx.Flags.String("hue")
|
||||||
saturationFlag := args.Flags.String("saturation")
|
saturationFlag := ctx.Flags.String("saturation")
|
||||||
rgbFlag := args.Flags.String("rgb")
|
rgbFlag := ctx.Flags.String("rgb")
|
||||||
name := args.Flags.String("name")
|
name := ctx.Flags.String("name")
|
||||||
|
|
||||||
if (hueFlag == "" || saturationFlag == "") && rgbFlag == "" && name == "" {
|
if (hueFlag == "" || saturationFlag == "") && rgbFlag == "" && name == "" {
|
||||||
printCmdHelp(args.Name)
|
printCmdHelp(ctx.Name)
|
||||||
return ExitFailure, nil
|
return ExitFailure, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,12 +81,12 @@ func SetColorCmd(args CmdArgs) (int, error) {
|
|||||||
color := lifx.HSBKColor{}
|
color := lifx.HSBKColor{}
|
||||||
|
|
||||||
if hueFlag != "" {
|
if hueFlag != "" {
|
||||||
hue := args.Flags.Float32("hue")
|
hue := ctx.Flags.Float32("hue")
|
||||||
color.H = lifx.Float32Ptr(hue)
|
color.H = lifx.Float32Ptr(hue)
|
||||||
}
|
}
|
||||||
|
|
||||||
if saturationFlag != "" {
|
if saturationFlag != "" {
|
||||||
saturation := args.Flags.Float32("saturation")
|
saturation := ctx.Flags.Float32("saturation")
|
||||||
color.S = lifx.Float32Ptr(saturation)
|
color.S = lifx.Float32Ptr(saturation)
|
||||||
}
|
}
|
||||||
state.Color = color
|
state.Color = color
|
||||||
@ -98,7 +98,7 @@ func SetColorCmd(args CmdArgs) (int, error) {
|
|||||||
}
|
}
|
||||||
state.Color = color
|
state.Color = color
|
||||||
} else if name != "" {
|
} else if name != "" {
|
||||||
hsb, ok := args.Config.Colors[name]
|
hsb, ok := ctx.Config.Colors[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return ExitFailure, fmt.Errorf("%s is not a defined color", name)
|
return ExitFailure, fmt.Errorf("%s is not a defined color", name)
|
||||||
}
|
}
|
||||||
@ -109,16 +109,16 @@ func SetColorCmd(args CmdArgs) (int, error) {
|
|||||||
state.Color = color
|
state.Color = color
|
||||||
}
|
}
|
||||||
|
|
||||||
brightnessFlag := args.Flags.String("brightness")
|
brightnessFlag := ctx.Flags.String("brightness")
|
||||||
if brightnessFlag != "" {
|
if brightnessFlag != "" {
|
||||||
brightness := args.Flags.Float64("brightness")
|
brightness := ctx.Flags.Float64("brightness")
|
||||||
state.Brightness = brightness
|
state.Brightness = brightness
|
||||||
}
|
}
|
||||||
|
|
||||||
duration := args.Flags.Float64("duration")
|
duration := ctx.Flags.Float64("duration")
|
||||||
state.Duration = duration
|
state.Duration = duration
|
||||||
|
|
||||||
fast := args.Flags.Bool("fast")
|
fast := ctx.Flags.Bool("fast")
|
||||||
state.Fast = fast
|
state.Fast = fast
|
||||||
|
|
||||||
r, err := c.SetState(selector, state)
|
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
|
var p Printer
|
||||||
|
|
||||||
c := args.Client
|
c := ctx.Client
|
||||||
state := lifx.State{}
|
state := lifx.State{}
|
||||||
selector := args.Flags.String("selector")
|
selector := ctx.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
format := ctx.Flags.String("format")
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = args.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
power := args.Flags.String("power")
|
power := ctx.Flags.String("power")
|
||||||
if power != "" {
|
if power != "" {
|
||||||
state.Power = power
|
state.Power = power
|
||||||
}
|
}
|
||||||
|
|
||||||
color := args.Flags.String("color")
|
color := ctx.Flags.String("color")
|
||||||
if color != "" {
|
if color != "" {
|
||||||
state.Color = lifx.NamedColor(color)
|
state.Color = lifx.NamedColor(color)
|
||||||
}
|
}
|
||||||
|
|
||||||
brightnessFlag := args.Flags.String("brightness")
|
brightnessFlag := ctx.Flags.String("brightness")
|
||||||
if brightnessFlag != "" {
|
if brightnessFlag != "" {
|
||||||
brightness := args.Flags.Float64("brightness")
|
brightness := ctx.Flags.Float64("brightness")
|
||||||
state.Brightness = brightness
|
state.Brightness = brightness
|
||||||
}
|
}
|
||||||
|
|
||||||
duration := args.Flags.Float64("duration")
|
duration := ctx.Flags.Float64("duration")
|
||||||
state.Duration = duration
|
state.Duration = duration
|
||||||
|
|
||||||
infraredFlag := args.Flags.String("infrared")
|
infraredFlag := ctx.Flags.String("infrared")
|
||||||
if infraredFlag != "" {
|
if infraredFlag != "" {
|
||||||
infrared := args.Flags.Float64("infrared")
|
infrared := ctx.Flags.Float64("infrared")
|
||||||
state.Infrared = infrared
|
state.Infrared = infrared
|
||||||
}
|
}
|
||||||
|
|
||||||
fast := args.Flags.Bool("fast")
|
fast := ctx.Flags.Bool("fast")
|
||||||
state.Fast = fast
|
state.Fast = fast
|
||||||
|
|
||||||
if power == "" && color == "" && brightnessFlag == "" && infraredFlag == "" {
|
if power == "" && color == "" && brightnessFlag == "" && infraredFlag == "" {
|
||||||
printCmdHelp(args.Name)
|
printCmdHelp(ctx.Name)
|
||||||
return ExitFailure, nil
|
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
|
var p Printer
|
||||||
|
|
||||||
c := args.Client
|
c := ctx.Client
|
||||||
state := lifx.State{}
|
state := lifx.State{}
|
||||||
selector := args.Flags.String("selector")
|
selector := ctx.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
format := ctx.Flags.String("format")
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = args.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
power := args.Flags.String("power")
|
power := ctx.Flags.String("power")
|
||||||
if power != "" {
|
if power != "" {
|
||||||
state.Power = power
|
state.Power = power
|
||||||
}
|
}
|
||||||
|
|
||||||
kelvinFlag := args.Flags.String("kelvin")
|
kelvinFlag := ctx.Flags.String("kelvin")
|
||||||
if kelvinFlag != "" {
|
if kelvinFlag != "" {
|
||||||
kelvin := args.Flags.Int16("kelvin")
|
kelvin := ctx.Flags.Int16("kelvin")
|
||||||
color, err := lifx.NewWhite(kelvin)
|
color, err := lifx.NewWhite(kelvin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
@ -73,9 +73,9 @@ func SetWhiteCmd(args CmdArgs) (int, error) {
|
|||||||
state.Color = color
|
state.Color = color
|
||||||
}
|
}
|
||||||
|
|
||||||
name := args.Flags.String("name")
|
name := ctx.Flags.String("name")
|
||||||
if name != "" {
|
if name != "" {
|
||||||
name := args.Flags.String("name")
|
name := ctx.Flags.String("name")
|
||||||
color, err := lifx.NewWhiteString(name)
|
color, err := lifx.NewWhiteString(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
@ -83,26 +83,26 @@ func SetWhiteCmd(args CmdArgs) (int, error) {
|
|||||||
state.Color = color
|
state.Color = color
|
||||||
}
|
}
|
||||||
|
|
||||||
brightnessFlag := args.Flags.String("brightness")
|
brightnessFlag := ctx.Flags.String("brightness")
|
||||||
if brightnessFlag != "" {
|
if brightnessFlag != "" {
|
||||||
brightness := args.Flags.Float64("brightness")
|
brightness := ctx.Flags.Float64("brightness")
|
||||||
state.Brightness = brightness
|
state.Brightness = brightness
|
||||||
}
|
}
|
||||||
|
|
||||||
duration := args.Flags.Float64("duration")
|
duration := ctx.Flags.Float64("duration")
|
||||||
state.Duration = duration
|
state.Duration = duration
|
||||||
|
|
||||||
infraredFlag := args.Flags.String("infrared")
|
infraredFlag := ctx.Flags.String("infrared")
|
||||||
if infraredFlag != "" {
|
if infraredFlag != "" {
|
||||||
infrared := args.Flags.Float64("infrared")
|
infrared := ctx.Flags.Float64("infrared")
|
||||||
state.Infrared = infrared
|
state.Infrared = infrared
|
||||||
}
|
}
|
||||||
|
|
||||||
fast := args.Flags.Bool("fast")
|
fast := ctx.Flags.Bool("fast")
|
||||||
state.Fast = fast
|
state.Fast = fast
|
||||||
|
|
||||||
if power == "" && kelvinFlag == "" && name == "" && brightnessFlag == "" && infraredFlag == "" {
|
if power == "" && kelvinFlag == "" && name == "" && brightnessFlag == "" && infraredFlag == "" {
|
||||||
printCmdHelp(args.Name)
|
printCmdHelp(ctx.Name)
|
||||||
return ExitFailure, nil
|
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
|
var indent int
|
||||||
c := args.Client
|
c := ctx.Client
|
||||||
selector := args.Flags.String("selector")
|
selector := ctx.Flags.String("selector")
|
||||||
lights, err := c.ListLights(selector)
|
lights, err := c.ListLights(selector)
|
||||||
|
|
||||||
if err != nil {
|
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
|
var p Printer
|
||||||
|
|
||||||
c := args.Client
|
c := ctx.Client
|
||||||
duration := args.Flags.Float64("duration")
|
duration := ctx.Flags.Float64("duration")
|
||||||
selector := args.Flags.String("selector")
|
selector := ctx.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
format := ctx.Flags.String("format")
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = args.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := c.Toggle(selector, duration)
|
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
|
var b strings.Builder
|
||||||
|
|
||||||
fmt.Fprintf(&b, "lume, version %s\n", Version)
|
fmt.Fprintf(&b, "lume, version %s\n", Version)
|
||||||
|
Loading…
Reference in New Issue
Block a user