Compare commits

..

2 Commits

Author SHA1 Message Date
e024b45e0a Refactor CLI exit code and error handling 2021-01-17 21:08:13 -06:00
5cc5be7846 Fix import alias 2021-01-17 21:07:53 -06:00
11 changed files with 51 additions and 39 deletions

View File

@ -5,7 +5,12 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"git.kill0.net/chill9/lume" lifx "git.kill0.net/chill9/lume"
)
const (
ExitSuccess = iota
ExitError
) )
type Config struct { type Config struct {

View File

@ -25,8 +25,8 @@ func HelpCmd(args CmdArgs) (int, error) {
} else if len(argv) >= 1 { } else if len(argv) >= 1 {
subCmd, ok := commandRegistry[argv[0]] subCmd, ok := commandRegistry[argv[0]]
if !ok { if !ok {
fmt.Printf("unknown command: %s\n", argv[0]) fmt.Printf("unknown commnnd: %s\n", argv[0])
return 1, nil return ExitError, nil
} }
if subCmd.Use != "" { if subCmd.Use != "" {
@ -38,7 +38,7 @@ func HelpCmd(args CmdArgs) (int, error) {
subCmd.Flags.PrintDefaults() subCmd.Flags.PrintDefaults()
} }
return 0, nil return ExitSuccess, nil
} }
func printHelp(commands map[string]Command) { func printHelp(commands map[string]Command) {

View File

@ -23,8 +23,8 @@ func LsCmd(args CmdArgs) (int, error) {
selector := args.Flags.String("selector") selector := args.Flags.String("selector")
lights, err := c.ListLights(selector) lights, err := c.ListLights(selector)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
PrintLights(lights) PrintLights(lights)
return 0, nil return ExitSuccess, nil
} }

View File

@ -1,11 +1,16 @@
package main package main
import ( import (
"fmt"
"os" "os"
lumecmd "git.kill0.net/chill9/lume/cmd" lumecmd "git.kill0.net/chill9/lume/cmd"
) )
func main() { 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)
} }

View File

@ -1,6 +1,7 @@
package lumecmd package lumecmd
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -12,19 +13,19 @@ import (
const lumercFile string = ".lumerc" const lumercFile string = ".lumerc"
func Main(args []string) int { func Main(args []string) (int, error) {
var config Config var config Config
var err error
configPath := getConfigPath() configPath := getConfigPath()
if configPath == "" { if configPath == "" {
fmt.Println("fatal: ~/.lumerc was not found") err = errors.New("fatal: ~/.lumerc was not found")
return 1 return ExitError, err
} }
if _, err := toml.DecodeFile(configPath, &config); err != nil { if _, err := toml.DecodeFile(configPath, &config); err != nil {
fmt.Printf("fatal: failed to parse %s\n", configPath) err = fmt.Errorf("fatal: failed to parse %s", configPath)
fmt.Println(err) return ExitError, err
return 1
} }
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN") envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
@ -33,8 +34,8 @@ func Main(args []string) int {
} }
if config.AccessToken == "" { if config.AccessToken == "" {
fmt.Println("access token is not set") err = errors.New("fatal: access token is not set")
return 1 return ExitError, err
} }
flag.Parse() flag.Parse()
@ -50,8 +51,8 @@ func Main(args []string) int {
cmd, ok := GetCommand(command) cmd, ok := GetCommand(command)
if !ok { if !ok {
fmt.Printf("lume: '%s' is not lume command. See 'lume help'\n", command) err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command)
return 1 return ExitError, err
} }
fs := cmd.Flags fs := cmd.Flags
fs.Parse(args[2:]) fs.Parse(args[2:])
@ -59,9 +60,10 @@ func Main(args []string) int {
cmdArgs.Flags = Flags{FlagSet: fs} cmdArgs.Flags = Flags{FlagSet: fs}
exitCode, err := cmd.Func(cmdArgs) exitCode, err := cmd.Func(cmdArgs)
if err != nil { 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 { func getConfigPath() string {

View File

@ -33,8 +33,8 @@ func PoweroffCmd(args CmdArgs) (int, error) {
r, err := c.SetState(selector, state) r, err := c.SetState(selector, state)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
PrintResults(r.Results) PrintResults(r.Results)
return 0, nil return ExitSuccess, nil
} }

View File

@ -33,8 +33,8 @@ func PoweronCmd(args CmdArgs) (int, error) {
r, err := c.SetState(selector, state) r, err := c.SetState(selector, state)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
PrintResults(r.Results) PrintResults(r.Results)
return 0, nil return ExitSuccess, nil
} }

View File

@ -4,7 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"git.kill0.net/chill9/lume" lifx "git.kill0.net/chill9/lume"
) )
func init() { func init() {
@ -81,17 +81,17 @@ func SetColorCmd(args CmdArgs) (int, error) {
} else if rgbFlag != "" { } else if rgbFlag != "" {
color, err := parseRGB(rgbFlag) color, err := parseRGB(rgbFlag)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
state.Color = color state.Color = color
} else if name != "" { } else if name != "" {
hsb, ok := args.Config.Colors[name] hsb, ok := args.Config.Colors[name]
if !ok { 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]) color, err := lifx.NewHSBColor(hsb[0], hsb[1], hsb[2])
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
state.Color = color state.Color = color
} }
@ -111,12 +111,12 @@ func SetColorCmd(args CmdArgs) (int, error) {
r, err := c.SetState(selector, state) r, err := c.SetState(selector, state)
if err != nil { if err != nil {
fmt.Printf("fatal: %s\n", err) fmt.Printf("fatal: %s\n", err)
return 1, err return ExitError, err
} }
if !fast { if !fast {
PrintResults(r.Results) PrintResults(r.Results)
} }
return 0, nil return ExitSuccess, nil
} }

View File

@ -3,7 +3,7 @@ package lumecmd
import ( import (
"flag" "flag"
"git.kill0.net/chill9/lume" lifx "git.kill0.net/chill9/lume"
) )
func init() { func init() {
@ -75,12 +75,12 @@ func SetStateCmd(args CmdArgs) (int, error) {
r, err := c.SetState(selector, state) r, err := c.SetState(selector, state)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
if !fast { if !fast {
PrintResults(r.Results) PrintResults(r.Results)
} }
return 0, nil return ExitSuccess, nil
} }

View File

@ -3,7 +3,7 @@ package lumecmd
import ( import (
"flag" "flag"
"git.kill0.net/chill9/lume" lifx "git.kill0.net/chill9/lume"
) )
func init() { func init() {
@ -58,7 +58,7 @@ func SetWhiteCmd(args CmdArgs) (int, error) {
kelvin := args.Flags.Int16("kelvin") kelvin := args.Flags.Int16("kelvin")
color, err := lifx.NewWhite(kelvin) color, err := lifx.NewWhite(kelvin)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
state.Color = color state.Color = color
} }
@ -68,7 +68,7 @@ func SetWhiteCmd(args CmdArgs) (int, error) {
name := args.Flags.String("name") name := args.Flags.String("name")
color, err := lifx.NewWhiteString(name) color, err := lifx.NewWhiteString(name)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
state.Color = color state.Color = color
} }
@ -93,12 +93,12 @@ func SetWhiteCmd(args CmdArgs) (int, error) {
r, err := c.SetState(selector, state) r, err := c.SetState(selector, state)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
if !fast { if !fast {
PrintResults(r.Results) PrintResults(r.Results)
} }
return 0, nil return ExitSuccess, nil
} }

View File

@ -29,8 +29,8 @@ func ToggleCmd(args CmdArgs) (int, error) {
selector := args.Flags.String("selector") selector := args.Flags.String("selector")
r, err := c.Toggle(selector, duration) r, err := c.Toggle(selector, duration)
if err != nil { if err != nil {
return 1, err return ExitError, err
} }
PrintResults(r.Results) PrintResults(r.Results)
return 0, nil return ExitSuccess, nil
} }