return errors to main()

This commit is contained in:
Ryan Cavicchioni 2020-03-29 00:08:17 -05:00
parent 6b033f10de
commit 3a6ee185fb
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
7 changed files with 26 additions and 31 deletions

View File

@ -25,7 +25,7 @@ type Flags struct {
type Command struct {
Name string
Func func(CmdArgs) int
Func func(CmdArgs) (int, error)
Flags *flag.FlagSet
}

View File

@ -2,7 +2,6 @@ package lumecmd
import (
"flag"
"fmt"
)
var (
@ -21,14 +20,13 @@ func init() {
})
}
func LsCmd(args CmdArgs) int {
func LsCmd(args CmdArgs) (int, error) {
c := args.Client
selector := args.Flags.String("selector")
lights, err := c.ListLights(selector)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
return 1, err
}
PrintLights(lights)
return 0
return 0, nil
}

View File

@ -46,5 +46,9 @@ func main() {
fs.Parse(os.Args[2:])
cmdArgs.Flags = lumecmd.Flags{fs}
os.Exit(cmd.Func(cmdArgs))
exitCode, err := cmd.Func(cmdArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: %s\n", err)
}
os.Exit(exitCode)
}

View File

@ -42,11 +42,13 @@ func init() {
})
}
func SetColorCmd(args CmdArgs) int {
func SetColorCmd(args CmdArgs) (int, error) {
c := args.Client
state := lifx.State{}
selector := args.Flags.String("selector")
fmt.Println(args.Config)
power := args.Flags.String("power")
if power != "" {
state.Power = power
@ -73,8 +75,7 @@ func SetColorCmd(args CmdArgs) int {
} else if rgbFlag != "" {
color, err := parseRGB(rgbFlag)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
return 1, err
}
state.Color = color
}
@ -94,12 +95,12 @@ func SetColorCmd(args CmdArgs) int {
r, err := c.SetState(selector, state)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
return 1, err
}
if !fast {
PrintResults(r.Results)
}
return 0
return 0, nil
}

View File

@ -2,7 +2,6 @@ package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/lume"
)
@ -39,7 +38,7 @@ func init() {
})
}
func SetStateCmd(args CmdArgs) int {
func SetStateCmd(args CmdArgs) (int, error) {
c := args.Client
state := lifx.State{}
selector := args.Flags.String("selector")
@ -74,13 +73,12 @@ func SetStateCmd(args CmdArgs) int {
r, err := c.SetState(selector, state)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
return 1, err
}
if !fast {
PrintResults(r.Results)
}
return 0
return 0, nil
}

View File

@ -2,7 +2,6 @@ package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/lume"
)
@ -42,7 +41,7 @@ func init() {
})
}
func SetWhiteCmd(args CmdArgs) int {
func SetWhiteCmd(args CmdArgs) (int, error) {
c := args.Client
state := lifx.State{}
selector := args.Flags.String("selector")
@ -57,8 +56,7 @@ func SetWhiteCmd(args CmdArgs) int {
kelvin := args.Flags.Int16("kelvin")
color, err := lifx.NewWhite(kelvin)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
return 1, err
}
state.Color = color
}
@ -68,8 +66,7 @@ func SetWhiteCmd(args CmdArgs) int {
name := args.Flags.String("name")
color, err := lifx.NewWhiteString(name)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
return 1, err
}
state.Color = color
}
@ -94,13 +91,12 @@ func SetWhiteCmd(args CmdArgs) int {
r, err := c.SetState(selector, state)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
return 1, err
}
if !fast {
PrintResults(r.Results)
}
return 0
return 0, nil
}

View File

@ -2,7 +2,6 @@ package lumecmd
import (
"flag"
"fmt"
)
func init() {
@ -22,15 +21,14 @@ func init() {
})
}
func ToggleCmd(args CmdArgs) int {
func ToggleCmd(args CmdArgs) (int, error) {
c := args.Client
duration := args.Flags.Float64("duration")
selector := args.Flags.String("selector")
r, err := c.Toggle(selector, duration)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
return 1, err
}
PrintResults(r.Results)
return 0
return 0, nil
}