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 { type Command struct {
Name string Name string
Func func(CmdArgs) int Func func(CmdArgs) (int, error)
Flags *flag.FlagSet Flags *flag.FlagSet
} }

View File

@ -2,7 +2,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
) )
var ( var (
@ -21,14 +20,13 @@ func init() {
}) })
} }
func LsCmd(args CmdArgs) int { func LsCmd(args CmdArgs) (int, error) {
c := args.Client c := args.Client
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 {
fmt.Printf("fatal: %s\n", err) return 1, err
return 1
} }
PrintLights(lights) PrintLights(lights)
return 0 return 0, nil
} }

View File

@ -46,5 +46,9 @@ func main() {
fs.Parse(os.Args[2:]) fs.Parse(os.Args[2:])
cmdArgs.Flags = lumecmd.Flags{fs} 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 c := args.Client
state := lifx.State{} state := lifx.State{}
selector := args.Flags.String("selector") selector := args.Flags.String("selector")
fmt.Println(args.Config)
power := args.Flags.String("power") power := args.Flags.String("power")
if power != "" { if power != "" {
state.Power = power state.Power = power
@ -73,8 +75,7 @@ func SetColorCmd(args CmdArgs) int {
} else if rgbFlag != "" { } else if rgbFlag != "" {
color, err := parseRGB(rgbFlag) color, err := parseRGB(rgbFlag)
if err != nil { if err != nil {
fmt.Printf("fatal: %s\n", err) return 1, err
return 1
} }
state.Color = color state.Color = color
} }
@ -94,12 +95,12 @@ func SetColorCmd(args CmdArgs) int {
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 return 1, err
} }
if !fast { if !fast {
PrintResults(r.Results) PrintResults(r.Results)
} }
return 0 return 0, nil
} }

View File

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

View File

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

View File

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