Compare commits
16 Commits
08dc79f00a
...
8364201d7b
Author | SHA1 | Date | |
---|---|---|---|
8364201d7b | |||
c3495ac57a | |||
b9e98679a1 | |||
c816ee43e1 | |||
2da3bcc2ff | |||
4c6c3744c9 | |||
84c1be5054 | |||
1e4a5f988e | |||
e8abc45377 | |||
6e1863f374 | |||
d0691362d8 | |||
715fb435e1 | |||
63631be598 | |||
30fae38ad1 | |||
c838c28e49 | |||
af2721720d |
1
.gitignore
vendored
1
.gitignore
vendored
@ -15,3 +15,4 @@
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
.lumerc
|
||||
|
@ -1 +1 @@
|
||||
AccessToken = "token"
|
||||
access_token = "token"
|
@ -1,15 +1,21 @@
|
||||
package lumecmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.kill0.net/chill9/lume"
|
||||
lifx "git.kill0.net/chill9/lume"
|
||||
)
|
||||
|
||||
const (
|
||||
ExitSuccess = iota
|
||||
ExitError
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AccessToken string
|
||||
AccessToken string `toml:"access_token"`
|
||||
Colors map[string][]float32 `toml:"colors"`
|
||||
}
|
||||
|
||||
@ -87,3 +93,12 @@ func GetCommand(name string) (Command, bool) {
|
||||
cmd, ok := commandRegistry[name]
|
||||
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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -4,10 +4,6 @@ import (
|
||||
"flag"
|
||||
)
|
||||
|
||||
var (
|
||||
idWidth, locationWidth, groupWidth, labelWidth, lastSeenWidth, powerWidth int
|
||||
)
|
||||
|
||||
func init() {
|
||||
var cmdName string = "ls"
|
||||
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
|
||||
@ -27,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
|
||||
}
|
||||
|
Binary file not shown.
19
cmd/lume/init_windows.go
Normal file
19
cmd/lume/init_windows.go
Normal 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)
|
||||
}
|
@ -1,90 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
lifx "git.kill0.net/chill9/lume"
|
||||
lumecmd "git.kill0.net/chill9/lume/cmd"
|
||||
"github.com/BurntSushi/toml"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
const lumercFile = ".lumerc"
|
||||
|
||||
func main() {
|
||||
var originalMode uint32
|
||||
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
|
||||
}
|
||||
lumecmd.ExitWithCode(lumecmd.Main(os.Args))
|
||||
}
|
90
cmd/main.go
Normal file
90
cmd/main.go
Normal 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
40
cmd/poweroff.go
Normal 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
40
cmd/poweron.go
Normal 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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
96
cmd/util.go
96
cmd/util.go
@ -2,11 +2,13 @@ package lumecmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.kill0.net/chill9/lume"
|
||||
lifx "git.kill0.net/chill9/lume"
|
||||
)
|
||||
|
||||
func powerColor(s string) string {
|
||||
@ -28,78 +30,88 @@ func statusColor(s lifx.Status) string {
|
||||
}
|
||||
|
||||
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 {
|
||||
length = len(r.Id)
|
||||
if idWidth < length {
|
||||
idWidth = length
|
||||
if widths["id"] < length {
|
||||
widths["id"] = length
|
||||
}
|
||||
|
||||
length = len(r.Label)
|
||||
if labelWidth < length {
|
||||
labelWidth = length
|
||||
if widths["label"] < length {
|
||||
widths["label"] = length
|
||||
}
|
||||
|
||||
length = len(r.Status)
|
||||
if statusWidth < length {
|
||||
statusWidth = length
|
||||
if widths["status"] < length {
|
||||
widths["status"] = length
|
||||
}
|
||||
}
|
||||
|
||||
sortResults(res)
|
||||
|
||||
for _, r := range res {
|
||||
fmt.Printf("%*s %*s %*s\n",
|
||||
idWidth, r.Id,
|
||||
labelWidth, r.Label,
|
||||
statusWidth, statusColor(r.Status))
|
||||
widths["id"], r.Id,
|
||||
widths["label"], r.Label,
|
||||
widths["status"], statusColor(r.Status))
|
||||
}
|
||||
}
|
||||
|
||||
func PrintLights(lights []lifx.Light) {
|
||||
var length int
|
||||
var widths map[string]int
|
||||
|
||||
widths = make(map[string]int)
|
||||
|
||||
for _, l := range lights {
|
||||
length = len(l.Id)
|
||||
if idWidth < length {
|
||||
idWidth = length
|
||||
if widths["id"] < length {
|
||||
widths["id"] = length
|
||||
}
|
||||
|
||||
length = len(l.Location.Name)
|
||||
if locationWidth < length {
|
||||
locationWidth = length
|
||||
if widths["location"] < length {
|
||||
widths["location"] = length
|
||||
}
|
||||
|
||||
length = len(l.Group.Name)
|
||||
if groupWidth < length {
|
||||
groupWidth = length
|
||||
if widths["group"] < length {
|
||||
widths["group"] = length
|
||||
}
|
||||
|
||||
length = len(l.Label)
|
||||
if labelWidth < length {
|
||||
labelWidth = length
|
||||
if widths["label"] < length {
|
||||
widths["label"] = length
|
||||
}
|
||||
|
||||
length = len(l.LastSeen.Local().Format(time.RFC3339))
|
||||
if lastSeenWidth < length {
|
||||
lastSeenWidth = length
|
||||
if widths["last_seen"] < length {
|
||||
widths["last_seen"] = length
|
||||
}
|
||||
|
||||
length = len(l.Power)
|
||||
if powerWidth < length {
|
||||
powerWidth = length
|
||||
if widths["power"] < length {
|
||||
widths["power"] = length
|
||||
}
|
||||
}
|
||||
|
||||
sortLights(lights)
|
||||
|
||||
fmt.Printf("total %d\n", len(lights))
|
||||
for _, l := range lights {
|
||||
fmt.Printf(
|
||||
"%*s %*s %*s %*s %*s %-*s\n",
|
||||
idWidth, l.Id,
|
||||
locationWidth, l.Location.Name,
|
||||
groupWidth, l.Group.Name,
|
||||
labelWidth, l.Label,
|
||||
lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339),
|
||||
powerWidth, powerColor(l.Power),
|
||||
widths["id"], l.Id,
|
||||
widths["loction"], l.Location.Name,
|
||||
widths["group"], l.Group.Name,
|
||||
widths["label"], l.Label,
|
||||
widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
|
||||
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))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user