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"
"strconv"
"git.kill0.net/chill9/lume"
lifx "git.kill0.net/chill9/lume"
)
const (
ExitSuccess = iota
ExitError
)
type Config struct {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ import (
"flag"
"fmt"
"git.kill0.net/chill9/lume"
lifx "git.kill0.net/chill9/lume"
)
func init() {
@ -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
}

View File

@ -3,7 +3,7 @@ package lumecmd
import (
"flag"
"git.kill0.net/chill9/lume"
lifx "git.kill0.net/chill9/lume"
)
func init() {
@ -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
}

View File

@ -3,7 +3,7 @@ package lumecmd
import (
"flag"
"git.kill0.net/chill9/lume"
lifx "git.kill0.net/chill9/lume"
)
func init() {
@ -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
}

View File

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