Make output format flags boolean
This commit is contained in:
parent
baf7daa1bb
commit
05f445ddf2
@ -43,7 +43,10 @@ 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("output-format")
|
format, err := getOutputFormatFromFlags(ctx.Flags)
|
||||||
|
if err != nil {
|
||||||
|
return ExitFailure, err
|
||||||
|
}
|
||||||
|
|
||||||
if format == "" && ctx.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = ctx.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package lumecmd
|
package lumecmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -100,6 +101,22 @@ func GetCommand(name string) (Command, bool) {
|
|||||||
func mergeGlobalFlags(fs *flag.FlagSet) {
|
func mergeGlobalFlags(fs *flag.FlagSet) {
|
||||||
fs.Bool("debug", false, "Enable debug mode")
|
fs.Bool("debug", false, "Enable debug mode")
|
||||||
|
|
||||||
outputFormat := fs.String("output-format", defaultOutputFormat, "Set the output format")
|
formatTable := fs.Bool("table", false, "Format output as an ASCII table")
|
||||||
fs.StringVar(outputFormat, "o", defaultOutputFormat, "Set the output format")
|
fs.BoolVar(formatTable, "t", false, "Format output as an ASCII table")
|
||||||
|
|
||||||
|
fs.Bool("simple", false, "Format output simply")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getOutputFormatFromFlags(fs Flags) (string, error) {
|
||||||
|
formatSimple := fs.Bool("simple")
|
||||||
|
formatTable := fs.Bool("table")
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case formatSimple && formatTable:
|
||||||
|
return "", errors.New("only one output format permitted")
|
||||||
|
case formatTable:
|
||||||
|
return "table", nil
|
||||||
|
default:
|
||||||
|
return "simple", nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,10 @@ 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("output-format")
|
format, err := getOutputFormatFromFlags(ctx.Flags)
|
||||||
|
if err != nil {
|
||||||
|
return ExitFailure, err
|
||||||
|
}
|
||||||
|
|
||||||
if format == "" && ctx.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = ctx.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
|
@ -32,8 +32,11 @@ 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("output-format")
|
|
||||||
state := lifx.State{Power: "off", Duration: duration}
|
state := lifx.State{Power: "off", Duration: duration}
|
||||||
|
format, err := getOutputFormatFromFlags(ctx.Flags)
|
||||||
|
if err != nil {
|
||||||
|
return ExitFailure, err
|
||||||
|
}
|
||||||
|
|
||||||
if format == "" && ctx.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = ctx.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
|
@ -32,8 +32,11 @@ 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("output-format")
|
|
||||||
state := lifx.State{Power: "on", Duration: duration}
|
state := lifx.State{Power: "on", Duration: duration}
|
||||||
|
format, err := getOutputFormatFromFlags(ctx.Flags)
|
||||||
|
if err != nil {
|
||||||
|
return ExitFailure, err
|
||||||
|
}
|
||||||
|
|
||||||
if format == "" && ctx.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = ctx.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
|
@ -54,7 +54,10 @@ 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("output-format")
|
format, err := getOutputFormatFromFlags(ctx.Flags)
|
||||||
|
if err != nil {
|
||||||
|
return ExitFailure, err
|
||||||
|
}
|
||||||
|
|
||||||
if format == "" && ctx.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = ctx.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
|
@ -47,7 +47,10 @@ 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("output-format")
|
format, err := getOutputFormatFromFlags(ctx.Flags)
|
||||||
|
if err != nil {
|
||||||
|
return ExitFailure, err
|
||||||
|
}
|
||||||
|
|
||||||
if format == "" && ctx.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = ctx.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
|
@ -50,7 +50,10 @@ 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("output-format")
|
format, err := getOutputFormatFromFlags(ctx.Flags)
|
||||||
|
if err != nil {
|
||||||
|
return ExitFailure, err
|
||||||
|
}
|
||||||
|
|
||||||
if format == "" && ctx.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = ctx.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
|
@ -30,7 +30,10 @@ 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("output-format")
|
format, err := getOutputFormatFromFlags(ctx.Flags)
|
||||||
|
if err != nil {
|
||||||
|
return ExitFailure, err
|
||||||
|
}
|
||||||
|
|
||||||
if format == "" && ctx.Config.OutputFormat != "" {
|
if format == "" && ctx.Config.OutputFormat != "" {
|
||||||
format = ctx.Config.OutputFormat
|
format = ctx.Config.OutputFormat
|
||||||
|
Loading…
Reference in New Issue
Block a user