From e024b45e0ab052f2092b8cca7a3c3c78f9022319 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 17 Jan 2021 21:08:13 -0600 Subject: [PATCH] Refactor CLI exit code and error handling --- cmd/command.go | 5 +++++ cmd/help.go | 6 +++--- cmd/ls.go | 4 ++-- cmd/lume/main.go | 7 ++++++- cmd/main.go | 26 ++++++++++++++------------ cmd/poweroff.go | 4 ++-- cmd/poweron.go | 4 ++-- cmd/setcolor.go | 10 +++++----- cmd/setstate.go | 4 ++-- cmd/setwhite.go | 8 ++++---- cmd/toggle.go | 4 ++-- 11 files changed, 47 insertions(+), 35 deletions(-) diff --git a/cmd/command.go b/cmd/command.go index 8c90a8a..aa66d24 100644 --- a/cmd/command.go +++ b/cmd/command.go @@ -8,6 +8,11 @@ import ( lifx "git.kill0.net/chill9/lume" ) +const ( + ExitSuccess = iota + ExitError +) + type Config struct { AccessToken string `toml:"access_token"` Colors map[string][]float32 `toml:"colors"` diff --git a/cmd/help.go b/cmd/help.go index bdf2cc9..8f68693 100644 --- a/cmd/help.go +++ b/cmd/help.go @@ -25,8 +25,8 @@ func HelpCmd(args CmdArgs) (int, error) { } else if len(argv) >= 1 { subCmd, ok := commandRegistry[argv[0]] if !ok { - fmt.Printf("unknown command: %s\n", argv[0]) - return 1, nil + fmt.Printf("unknown commnnd: %s\n", argv[0]) + return ExitError, nil } if subCmd.Use != "" { @@ -38,7 +38,7 @@ func HelpCmd(args CmdArgs) (int, error) { subCmd.Flags.PrintDefaults() } - return 0, nil + return ExitSuccess, nil } func printHelp(commands map[string]Command) { diff --git a/cmd/ls.go b/cmd/ls.go index cacaf0b..7c224d8 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -23,8 +23,8 @@ func LsCmd(args CmdArgs) (int, error) { selector := args.Flags.String("selector") lights, err := c.ListLights(selector) if err != nil { - return 1, err + return ExitError, err } PrintLights(lights) - return 0, nil + return ExitSuccess, nil } diff --git a/cmd/lume/main.go b/cmd/lume/main.go index 51d754b..f22f9d9 100644 --- a/cmd/lume/main.go +++ b/cmd/lume/main.go @@ -1,11 +1,16 @@ package main import ( + "fmt" "os" lumecmd "git.kill0.net/chill9/lume/cmd" ) func main() { - os.Exit(lumecmd.Main(os.Args)) + exitCode, err := lumecmd.Main(os.Args) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + } + os.Exit(exitCode) } diff --git a/cmd/main.go b/cmd/main.go index 9f13ed7..6031846 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,6 +1,7 @@ package lumecmd import ( + "errors" "flag" "fmt" "os" @@ -12,19 +13,19 @@ import ( const lumercFile string = ".lumerc" -func Main(args []string) int { +func Main(args []string) (int, error) { var config Config + var err error configPath := getConfigPath() if configPath == "" { - fmt.Println("fatal: ~/.lumerc was not found") - return 1 + err = errors.New("fatal: ~/.lumerc was not found") + return ExitError, err } if _, err := toml.DecodeFile(configPath, &config); err != nil { - fmt.Printf("fatal: failed to parse %s\n", configPath) - fmt.Println(err) - return 1 + err = fmt.Errorf("fatal: failed to parse %s", configPath) + return ExitError, err } envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN") @@ -33,8 +34,8 @@ func Main(args []string) int { } if config.AccessToken == "" { - fmt.Println("access token is not set") - return 1 + err = errors.New("fatal: access token is not set") + return ExitError, err } flag.Parse() @@ -50,8 +51,8 @@ func Main(args []string) int { cmd, ok := GetCommand(command) if !ok { - fmt.Printf("lume: '%s' is not lume command. See 'lume help'\n", command) - return 1 + err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command) + return ExitError, err } fs := cmd.Flags fs.Parse(args[2:]) @@ -59,9 +60,10 @@ func Main(args []string) int { cmdArgs.Flags = Flags{FlagSet: fs} exitCode, err := cmd.Func(cmdArgs) if err != nil { - fmt.Fprintf(os.Stderr, "fatal: %s\n", err) + err = fmt.Errorf("fatal: %s", err) } - return exitCode + + return exitCode, err } func getConfigPath() string { diff --git a/cmd/poweroff.go b/cmd/poweroff.go index ece8785..941f16c 100644 --- a/cmd/poweroff.go +++ b/cmd/poweroff.go @@ -33,8 +33,8 @@ func PoweroffCmd(args CmdArgs) (int, error) { r, err := c.SetState(selector, state) if err != nil { - return 1, err + return ExitError, err } PrintResults(r.Results) - return 0, nil + return ExitSuccess, nil } diff --git a/cmd/poweron.go b/cmd/poweron.go index 3d97031..711411a 100644 --- a/cmd/poweron.go +++ b/cmd/poweron.go @@ -33,8 +33,8 @@ func PoweronCmd(args CmdArgs) (int, error) { r, err := c.SetState(selector, state) if err != nil { - return 1, err + return ExitError, err } PrintResults(r.Results) - return 0, nil + return ExitSuccess, nil } diff --git a/cmd/setcolor.go b/cmd/setcolor.go index 374c899..70247e0 100644 --- a/cmd/setcolor.go +++ b/cmd/setcolor.go @@ -81,17 +81,17 @@ func SetColorCmd(args CmdArgs) (int, error) { } else if rgbFlag != "" { color, err := parseRGB(rgbFlag) if err != nil { - return 1, err + return ExitError, err } state.Color = color } else if name != "" { hsb, ok := args.Config.Colors[name] if !ok { - return 1, fmt.Errorf("%s is not a defined color", name) + return ExitError, fmt.Errorf("%s is not a defined color", name) } color, err := lifx.NewHSBColor(hsb[0], hsb[1], hsb[2]) if err != nil { - return 1, err + return ExitError, err } state.Color = color } @@ -111,12 +111,12 @@ func SetColorCmd(args CmdArgs) (int, error) { r, err := c.SetState(selector, state) if err != nil { fmt.Printf("fatal: %s\n", err) - return 1, err + return ExitError, err } if !fast { PrintResults(r.Results) } - return 0, nil + return ExitSuccess, nil } diff --git a/cmd/setstate.go b/cmd/setstate.go index d65ea0c..aca286c 100644 --- a/cmd/setstate.go +++ b/cmd/setstate.go @@ -75,12 +75,12 @@ func SetStateCmd(args CmdArgs) (int, error) { r, err := c.SetState(selector, state) if err != nil { - return 1, err + return ExitError, err } if !fast { PrintResults(r.Results) } - return 0, nil + return ExitSuccess, nil } diff --git a/cmd/setwhite.go b/cmd/setwhite.go index c10d267..05bc648 100644 --- a/cmd/setwhite.go +++ b/cmd/setwhite.go @@ -58,7 +58,7 @@ func SetWhiteCmd(args CmdArgs) (int, error) { kelvin := args.Flags.Int16("kelvin") color, err := lifx.NewWhite(kelvin) if err != nil { - return 1, err + return ExitError, err } state.Color = color } @@ -68,7 +68,7 @@ func SetWhiteCmd(args CmdArgs) (int, error) { name := args.Flags.String("name") color, err := lifx.NewWhiteString(name) if err != nil { - return 1, err + return ExitError, err } state.Color = color } @@ -93,12 +93,12 @@ func SetWhiteCmd(args CmdArgs) (int, error) { r, err := c.SetState(selector, state) if err != nil { - return 1, err + return ExitError, err } if !fast { PrintResults(r.Results) } - return 0, nil + return ExitSuccess, nil } diff --git a/cmd/toggle.go b/cmd/toggle.go index 0508e8e..6509cbc 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -29,8 +29,8 @@ func ToggleCmd(args CmdArgs) (int, error) { selector := args.Flags.String("selector") r, err := c.Toggle(selector, duration) if err != nil { - return 1, err + return ExitError, err } PrintResults(r.Results) - return 0, nil + return ExitSuccess, nil }