Make output format flags boolean

This commit is contained in:
Ryan Cavicchioni 2021-04-11 02:05:05 +00:00
parent baf7daa1bb
commit 05f445ddf2
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
9 changed files with 51 additions and 10 deletions

View File

@ -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

View File

@ -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
}
} }

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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