Compare commits

...

2 Commits

Author SHA1 Message Date
4ea3545ea7
add set-state command 2020-03-27 19:31:41 -05:00
8df5b1d779
change error format 2020-03-27 19:31:12 -05:00
6 changed files with 96 additions and 5 deletions

View File

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

View File

@ -25,7 +25,7 @@ func LsCmd(args CmdArgs) int {
selector := args.Flags.String("selector")
lights, err := c.ListLights(selector)
if err != nil {
fmt.Println(err)
fmt.Printf("fatal: %s\n", err)
return 1
}
PrintLights(lights)

View File

@ -47,7 +47,7 @@ func main() {
cmd, ok := lumecmd.GetCommand(command)
if !ok {
fmt.Println("ERROR")
fmt.Printf("lume: '%s' is not lume command. See 'lume' --help.'\n", command)
os.Exit(1)
}
fs := cmd.Flags

86
cmd/setstate.go Normal file
View File

@ -0,0 +1,86 @@
package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/go-lifx"
)
func init() {
var cmdName string = "set-state"
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
selector := fs.String("selector", "all", "Set the selector")
fs.StringVar(selector, "s", "all", "Set the selector")
power := fs.String("power", "", "power state")
fs.StringVar(power, "p", "", "power state")
color := fs.String("color", "", "color state")
fs.StringVar(color, "c", "", "color state")
brightness := fs.String("brightness", "", "brightness state")
fs.StringVar(brightness, "b", "", "brightness state")
duration := fs.Float64("duration", 1.0, "duration state")
fs.Float64Var(duration, "d", 1.0, "duration state")
infrared := fs.String("infrared", "", "infrared state")
fs.StringVar(infrared, "i", "", "infrared state")
fast := fs.Bool("fast", false, "fast state")
fs.BoolVar(fast, "f", false, "fast state")
RegisterCommand(cmdName, Command{
Func: SetStateCmd,
Flags: fs,
})
}
func SetStateCmd(args CmdArgs) int {
c := args.Client
state := lifx.State{}
selector := args.Flags.String("selector")
power := args.Flags.String("power")
if power != "" {
state.Power = power
}
color := args.Flags.String("color")
if color != "" {
state.Color = lifx.NamedColor(color)
}
brightnessFlag := args.Flags.String("brightness")
if brightnessFlag != "" {
brightness := args.Flags.Float64("brightness")
state.Brightness = brightness
}
duration := args.Flags.Float64("duration")
state.Duration = duration
infraredFlag := args.Flags.String("infrared")
if infraredFlag != "" {
infrared := args.Flags.Float64("infrared")
state.Infrared = infrared
}
fast := args.Flags.Bool("fast")
state.Fast = fast
r, err := c.SetState(selector, state)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1
}
if !fast {
PrintResults(r.Results)
}
return 0
}

View File

@ -24,7 +24,7 @@ func ToggleCmd(args CmdArgs) int {
selector := args.Flags.String("selector")
r, err := c.Toggle(selector, duration)
if err != nil {
fmt.Println(err)
fmt.Printf("fatal: %s\n", err)
return 1
}
PrintResults(r.Results)

View File

@ -3,7 +3,7 @@ package lifx
import (
//"crypto/tls"
"encoding/json"
"fmt"
"errors"
"net/http"
"time"
)
@ -97,7 +97,7 @@ func NewApiError(resp *http.Response) error {
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return err
}
return fmt.Errorf("fatal: %s", s.Error)
return errors.New(s.Error)
}
func IsApiError(resp *http.Response) bool {
@ -120,6 +120,10 @@ func (c *Client) SetState(selector string, state State) (*Response, error) {
}
defer resp.Body.Close()
if IsApiError(resp) {
return nil, NewApiError(resp)
}
if state.Fast && resp.StatusCode == http.StatusAccepted {
return nil, nil
}