Compare commits

...

16 Commits

Author SHA1 Message Date
8364201d7b
Add helper method to exit with error code 2021-01-20 23:26:33 -06:00
c3495ac57a
Add method to validate configuration 2021-01-20 23:26:33 -06:00
b9e98679a1
Refactor CLI exit code and error handling 2021-01-20 23:26:33 -06:00
c816ee43e1
Fix import alias 2021-01-20 23:26:33 -06:00
2da3bcc2ff
Return error codes instead of exiting from Main() 2021-01-20 23:26:33 -06:00
4c6c3744c9
Move lume main into the lumecmd package 2021-01-20 23:26:33 -06:00
84c1be5054
Print error if .lumerc cannot be found 2021-01-20 23:26:32 -06:00
1e4a5f988e
Use snake case in the configuration file 2021-01-20 23:26:32 -06:00
e8abc45377
Add poweron and poweroff commands 2021-01-20 23:26:32 -06:00
6e1863f374
Add CLI output sort functions 2021-01-20 23:26:32 -06:00
d0691362d8
Use a map to track column widths 2021-01-20 23:26:32 -06:00
715fb435e1
Refactor configuration file loading 2021-01-20 23:26:31 -06:00
63631be598
Fix composite literal uses unkeyed fields 2021-01-20 23:26:31 -06:00
30fae38ad1
Move Windows console hack into constrained file to fix Linux build 2021-01-20 23:26:31 -06:00
c838c28e49
Add .lumerc to .gitignore 2021-01-20 23:26:31 -06:00
af2721720d
Remove __debug_bin 2021-01-20 23:26:27 -06:00
16 changed files with 298 additions and 138 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
.lumerc

View File

@ -1 +1 @@
AccessToken = "token" access_token = "token"

View File

@ -1,15 +1,21 @@
package lumecmd package lumecmd
import ( import (
"errors"
"flag" "flag"
"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 {
AccessToken string AccessToken string `toml:"access_token"`
Colors map[string][]float32 `toml:"colors"` Colors map[string][]float32 `toml:"colors"`
} }
@ -87,3 +93,12 @@ func GetCommand(name string) (Command, bool) {
cmd, ok := commandRegistry[name] cmd, ok := commandRegistry[name]
return cmd, ok return cmd, ok
} }
// Validate configuration struct
func (c *Config) Validate() error {
var err error
if c.AccessToken == "" {
err = errors.New("access_token is not set")
}
return err
}

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

@ -4,10 +4,6 @@ import (
"flag" "flag"
) )
var (
idWidth, locationWidth, groupWidth, labelWidth, lastSeenWidth, powerWidth int
)
func init() { func init() {
var cmdName string = "ls" var cmdName string = "ls"
fs := flag.NewFlagSet(cmdName, flag.ExitOnError) fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
@ -27,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
} }

Binary file not shown.

19
cmd/lume/init_windows.go Normal file
View File

@ -0,0 +1,19 @@
// +build windows
// https://stackoverflow.com/a/52579002
package main
import (
"os"
"golang.org/x/sys/windows"
)
func init() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}

View File

@ -1,90 +1,11 @@
package main package main
import ( import (
"flag"
"fmt"
"os" "os"
"path"
lifx "git.kill0.net/chill9/lume"
lumecmd "git.kill0.net/chill9/lume/cmd" lumecmd "git.kill0.net/chill9/lume/cmd"
"github.com/BurntSushi/toml"
"golang.org/x/sys/windows"
) )
const lumercFile = ".lumerc"
func main() { func main() {
var originalMode uint32 lumecmd.ExitWithCode(lumecmd.Main(os.Args))
stdout := windows.Handle(os.Stdout.Fd()) }
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
defer windows.SetConsoleMode(stdout, originalMode)
var config lumecmd.Config
config = loadConfig()
if config.AccessToken == "" {
config.AccessToken = os.Getenv("LIFX_ACCESS_TOKEN")
}
if config.AccessToken == "" {
fmt.Println("access token is not set")
os.Exit(1)
}
flag.Parse()
command := flag.Arg(0)
c := lifx.NewClient(config.AccessToken)
cmdArgs := lumecmd.CmdArgs{
Client: c,
Config: config,
}
cmd, ok := lumecmd.GetCommand(command)
if !ok {
fmt.Printf("lume: '%s' is not lume command. See 'lume help'\n", command)
os.Exit(1)
}
fs := cmd.Flags
fs.Parse(os.Args[2:])
cmdArgs.Flags = lumecmd.Flags{fs}
exitCode, err := cmd.Func(cmdArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: %s\n", err)
}
os.Exit(exitCode)
}
func loadConfig() lumecmd.Config {
var config lumecmd.Config
var tryPath, configPath string
homeDir, err := os.UserHomeDir()
if err == nil {
tryPath = path.Join(homeDir, lumercFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
cwd, err := os.Getwd()
if err == nil {
tryPath = path.Join(cwd, lumercFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
if configPath != "" {
toml.DecodeFile(configPath, &config)
}
return config
}

90
cmd/main.go Normal file
View File

@ -0,0 +1,90 @@
package lumecmd
import (
"errors"
"flag"
"fmt"
"os"
"path"
lifx "git.kill0.net/chill9/lume"
"github.com/BurntSushi/toml"
)
const lumercFile string = ".lumerc"
func Main(args []string) (int, error) {
var config Config
var err error
configPath := getConfigPath()
if configPath == "" {
err = errors.New("fatal: ~/.lumerc was not found")
return ExitError, err
}
if _, err := toml.DecodeFile(configPath, &config); err != nil {
err = fmt.Errorf("fatal: failed to parse %s", configPath)
return ExitError, err
}
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
config.AccessToken = envAccessToken
}
if err = config.Validate(); err != nil {
return ExitError, fmt.Errorf("fatal: %s", err)
}
flag.Parse()
command := flag.Arg(0)
c := lifx.NewClient(config.AccessToken)
cmdArgs := CmdArgs{
Client: c,
Config: config,
}
cmd, ok := GetCommand(command)
if !ok {
err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command)
return ExitError, err
}
fs := cmd.Flags
fs.Parse(args[2:])
cmdArgs.Flags = Flags{FlagSet: fs}
exitCode, err := cmd.Func(cmdArgs)
if err != nil {
err = fmt.Errorf("fatal: %s", err)
}
return exitCode, err
}
func getConfigPath() string {
var tryPath, configPath string
// ~/.lumerc
homeDir, err := os.UserHomeDir()
if err == nil {
tryPath = path.Join(homeDir, lumercFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
// ./.lumerc
cwd, err := os.Getwd()
if err == nil {
tryPath = path.Join(cwd, lumercFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
return configPath
}

40
cmd/poweroff.go Normal file
View File

@ -0,0 +1,40 @@
package lumecmd
import (
"flag"
lifx "git.kill0.net/chill9/lume"
)
func init() {
var cmdName string = "poweroff"
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
RegisterCommand(cmdName, Command{
Func: PoweroffCmd,
Flags: fs,
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Power on",
})
}
func PoweroffCmd(args CmdArgs) (int, error) {
c := args.Client
duration := args.Flags.Float64("duration")
selector := args.Flags.String("selector")
state := lifx.State{Power: "off", Duration: duration}
r, err := c.SetState(selector, state)
if err != nil {
return ExitError, err
}
PrintResults(r.Results)
return ExitSuccess, nil
}

40
cmd/poweron.go Normal file
View File

@ -0,0 +1,40 @@
package lumecmd
import (
"flag"
lifx "git.kill0.net/chill9/lume"
)
func init() {
var cmdName string = "poweron"
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
RegisterCommand(cmdName, Command{
Func: PoweronCmd,
Flags: fs,
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Power on",
})
}
func PoweronCmd(args CmdArgs) (int, error) {
c := args.Client
duration := args.Flags.Float64("duration")
selector := args.Flags.String("selector")
state := lifx.State{Power: "on", Duration: duration}
r, err := c.SetState(selector, state)
if err != nil {
return ExitError, err
}
PrintResults(r.Results)
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
} }

View File

@ -2,11 +2,13 @@ package lumecmd
import ( import (
"fmt" "fmt"
"os"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"git.kill0.net/chill9/lume" lifx "git.kill0.net/chill9/lume"
) )
func powerColor(s string) string { func powerColor(s string) string {
@ -28,78 +30,88 @@ func statusColor(s lifx.Status) string {
} }
func PrintResults(res []lifx.Result) { func PrintResults(res []lifx.Result) {
var length, idWidth, labelWidth, statusWidth int var length int
var widths map[string]int
widths = make(map[string]int)
for _, r := range res { for _, r := range res {
length = len(r.Id) length = len(r.Id)
if idWidth < length { if widths["id"] < length {
idWidth = length widths["id"] = length
} }
length = len(r.Label) length = len(r.Label)
if labelWidth < length { if widths["label"] < length {
labelWidth = length widths["label"] = length
} }
length = len(r.Status) length = len(r.Status)
if statusWidth < length { if widths["status"] < length {
statusWidth = length widths["status"] = length
} }
} }
sortResults(res)
for _, r := range res { for _, r := range res {
fmt.Printf("%*s %*s %*s\n", fmt.Printf("%*s %*s %*s\n",
idWidth, r.Id, widths["id"], r.Id,
labelWidth, r.Label, widths["label"], r.Label,
statusWidth, statusColor(r.Status)) widths["status"], statusColor(r.Status))
} }
} }
func PrintLights(lights []lifx.Light) { func PrintLights(lights []lifx.Light) {
var length int var length int
var widths map[string]int
widths = make(map[string]int)
for _, l := range lights { for _, l := range lights {
length = len(l.Id) length = len(l.Id)
if idWidth < length { if widths["id"] < length {
idWidth = length widths["id"] = length
} }
length = len(l.Location.Name) length = len(l.Location.Name)
if locationWidth < length { if widths["location"] < length {
locationWidth = length widths["location"] = length
} }
length = len(l.Group.Name) length = len(l.Group.Name)
if groupWidth < length { if widths["group"] < length {
groupWidth = length widths["group"] = length
} }
length = len(l.Label) length = len(l.Label)
if labelWidth < length { if widths["label"] < length {
labelWidth = length widths["label"] = length
} }
length = len(l.LastSeen.Local().Format(time.RFC3339)) length = len(l.LastSeen.Local().Format(time.RFC3339))
if lastSeenWidth < length { if widths["last_seen"] < length {
lastSeenWidth = length widths["last_seen"] = length
} }
length = len(l.Power) length = len(l.Power)
if powerWidth < length { if widths["power"] < length {
powerWidth = length widths["power"] = length
} }
} }
sortLights(lights)
fmt.Printf("total %d\n", len(lights)) fmt.Printf("total %d\n", len(lights))
for _, l := range lights { for _, l := range lights {
fmt.Printf( fmt.Printf(
"%*s %*s %*s %*s %*s %-*s\n", "%*s %*s %*s %*s %*s %-*s\n",
idWidth, l.Id, widths["id"], l.Id,
locationWidth, l.Location.Name, widths["loction"], l.Location.Name,
groupWidth, l.Group.Name, widths["group"], l.Group.Name,
labelWidth, l.Label, widths["label"], l.Label,
lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339), widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
powerWidth, powerColor(l.Power), widths["power"], powerColor(l.Power),
) )
} }
} }
@ -121,3 +133,29 @@ func parseRGB(s string) (lifx.RGBColor, error) {
} }
return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b)) return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b))
} }
func sortLights(lights []lifx.Light) {
sort.Slice(lights, func(i, j int) bool {
if lights[i].Group.Name < lights[j].Group.Name {
return true
}
if lights[i].Group.Name > lights[j].Group.Name {
return false
}
return lights[i].Label < lights[j].Label
})
}
func sortResults(res []lifx.Result) {
sort.Slice(res, func(i, j int) bool {
return res[i].Label < res[j].Label
})
}
func ExitWithCode(code int, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
os.Exit(code)
}