2021-01-16 06:38:10 +00:00
|
|
|
package lumecmd
|
|
|
|
|
|
|
|
import (
|
2021-01-18 03:08:13 +00:00
|
|
|
"errors"
|
2021-01-16 06:38:10 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-02-14 00:42:47 +00:00
|
|
|
"strings"
|
2021-01-16 06:38:10 +00:00
|
|
|
|
2021-02-15 01:02:26 +00:00
|
|
|
"git.kill0.net/chill9/lifx-go"
|
2021-01-16 06:38:10 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
)
|
|
|
|
|
2021-02-14 00:42:47 +00:00
|
|
|
var userAgent string
|
2021-02-11 04:16:15 +00:00
|
|
|
|
2021-02-03 00:37:26 +00:00
|
|
|
func init() {
|
2021-02-14 00:42:47 +00:00
|
|
|
userAgent = initUserAgent()
|
|
|
|
|
2021-02-03 00:37:26 +00:00
|
|
|
RegisterCommand("help", Command{
|
|
|
|
Func: HelpCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("help", flag.ExitOnError)
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "<command>",
|
|
|
|
Short: "Show help for a command",
|
|
|
|
})
|
|
|
|
RegisterCommand("ls", Command{
|
|
|
|
Func: LsCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("ls", flag.ExitOnError)
|
|
|
|
|
|
|
|
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
|
|
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "[--selector=<selector>]",
|
|
|
|
Short: "List the lights",
|
|
|
|
})
|
|
|
|
RegisterCommand("poweroff", Command{
|
|
|
|
Func: PoweroffCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("poweroff", 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")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "[--selector <selector>] [--duration <sec>]",
|
|
|
|
Short: "Power on",
|
|
|
|
})
|
|
|
|
RegisterCommand("poweron", Command{
|
|
|
|
Func: PoweronCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("poweron", 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")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "[--selector <selector>] [--duration <sec>]",
|
|
|
|
Short: "Power on",
|
|
|
|
})
|
|
|
|
RegisterCommand("set-color", Command{
|
|
|
|
Func: SetColorCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("set-color", flag.ExitOnError)
|
|
|
|
|
|
|
|
selector := fs.String("selector", "all", "the selector")
|
|
|
|
fs.StringVar(selector, "s", "all", "the selector")
|
|
|
|
|
|
|
|
power := fs.String("power", defaultPower, "power state")
|
|
|
|
fs.StringVar(power, "p", defaultPower, "power state")
|
|
|
|
|
|
|
|
hue := fs.String("hue", defaultHue, "hue level")
|
|
|
|
fs.StringVar(hue, "H", defaultHue, "hue level")
|
|
|
|
|
|
|
|
saturation := fs.String("saturation", defaultSaturation, "saturation level")
|
|
|
|
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
|
|
|
|
|
|
|
|
rgb := fs.String("rgb", defaultRGB, "RGB value")
|
|
|
|
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
|
|
|
|
|
|
|
|
name := fs.String("name", defaultName, "named color")
|
|
|
|
fs.StringVar(name, "n", defaultName, "named color")
|
|
|
|
|
|
|
|
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
|
|
|
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
|
|
|
|
|
|
|
duration := fs.Float64("duration", defaultDuration, "duration state")
|
|
|
|
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
|
|
|
|
|
|
|
fast := fs.Bool("fast", defaultFast, "fast state")
|
|
|
|
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
|
|
|
|
Short: "Set the color",
|
|
|
|
})
|
|
|
|
RegisterCommand("set-state", Command{
|
|
|
|
Func: SetStateCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("set-state", flag.ExitOnError)
|
|
|
|
|
|
|
|
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
|
|
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
|
|
|
|
|
|
power := fs.String("power", defaultPower, "power state")
|
|
|
|
fs.StringVar(power, "p", defaultPower, "power state")
|
|
|
|
|
|
|
|
color := fs.String("color", defaultColor, "color state")
|
|
|
|
fs.StringVar(color, "c", defaultColor, "color state")
|
|
|
|
|
|
|
|
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
|
|
|
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
|
|
|
|
|
|
|
duration := fs.Float64("duration", defaultDuration, "duration state")
|
|
|
|
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
|
|
|
|
|
|
|
infrared := fs.String("infrared", defaultInfrared, "infrared state")
|
|
|
|
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
|
|
|
|
|
|
|
|
fast := fs.Bool("fast", defaultFast, "fast state")
|
|
|
|
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
|
|
|
|
Short: "Set various state attributes",
|
|
|
|
})
|
|
|
|
RegisterCommand("set-white", Command{
|
|
|
|
Func: SetWhiteCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("set-white", flag.ExitOnError)
|
|
|
|
|
|
|
|
selector := fs.String("selector", "all", "the selector")
|
|
|
|
fs.StringVar(selector, "s", "all", "the selector")
|
|
|
|
|
|
|
|
power := fs.String("power", defaultPower, "power state")
|
|
|
|
fs.StringVar(power, "p", defaultPower, "power state")
|
|
|
|
|
|
|
|
kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level")
|
|
|
|
fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level")
|
|
|
|
|
|
|
|
name := fs.String("name", defaultWhiteName, "named white level")
|
|
|
|
fs.StringVar(name, "n", defaultWhiteName, "named white level")
|
|
|
|
|
|
|
|
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
|
|
|
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
|
|
|
|
|
|
|
duration := fs.Float64("duration", defaultDuration, "duration state")
|
|
|
|
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
|
|
|
|
|
|
|
infrared := fs.String("infrared", defaultInfrared, "infrared state")
|
|
|
|
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
|
|
|
|
|
|
|
|
fast := fs.Bool("fast", defaultFast, "fast state")
|
|
|
|
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
|
|
|
|
Short: "Set the white level",
|
|
|
|
})
|
2021-02-15 05:06:12 +00:00
|
|
|
RegisterCommand("show", Command{
|
|
|
|
Func: ShowCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("show", flag.ExitOnError)
|
|
|
|
|
|
|
|
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
|
|
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "[--selector=<selector>]",
|
|
|
|
Short: "Show details about the lights",
|
|
|
|
})
|
2021-02-03 00:37:26 +00:00
|
|
|
RegisterCommand("toggle", Command{
|
|
|
|
Func: ToggleCmd,
|
|
|
|
Flags: func() *flag.FlagSet {
|
|
|
|
fs := flag.NewFlagSet("toggle", 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")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}(),
|
|
|
|
Use: "[--selector <selector>] [--duration <sec>]",
|
|
|
|
Short: "Toggle the power on/off",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-16 06:38:10 +00:00
|
|
|
const lumercFile string = ".lumerc"
|
|
|
|
|
2021-01-18 03:08:13 +00:00
|
|
|
func Main(args []string) (int, error) {
|
2021-01-16 06:38:10 +00:00
|
|
|
var config Config
|
2021-01-18 03:08:13 +00:00
|
|
|
var err error
|
2021-01-16 06:38:10 +00:00
|
|
|
|
2021-02-01 02:28:56 +00:00
|
|
|
if len(args) == 1 {
|
|
|
|
args = append(args, "help")
|
|
|
|
}
|
|
|
|
|
2021-01-16 06:38:10 +00:00
|
|
|
configPath := getConfigPath()
|
|
|
|
if configPath == "" {
|
2021-01-18 03:08:13 +00:00
|
|
|
err = errors.New("fatal: ~/.lumerc was not found")
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, err
|
2021-01-16 06:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := toml.DecodeFile(configPath, &config); err != nil {
|
2021-01-25 01:03:07 +00:00
|
|
|
err = fmt.Errorf("fatal: failed to parse %s; %w", configPath, err)
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, err
|
2021-01-16 06:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
|
|
|
|
if envAccessToken != "" {
|
|
|
|
config.AccessToken = envAccessToken
|
|
|
|
}
|
|
|
|
|
2021-01-21 05:09:37 +00:00
|
|
|
if err = config.Validate(); err != nil {
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, fmt.Errorf("fatal: %s", err)
|
2021-01-16 06:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2021-02-01 02:28:56 +00:00
|
|
|
command := args[1]
|
2021-01-16 06:38:10 +00:00
|
|
|
|
2021-02-11 15:16:24 +00:00
|
|
|
c := lifx.NewClient(
|
|
|
|
config.AccessToken,
|
|
|
|
lifx.WithUserAgent(userAgent),
|
|
|
|
)
|
2021-01-16 06:38:10 +00:00
|
|
|
|
|
|
|
cmdArgs := CmdArgs{
|
|
|
|
Client: c,
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd, ok := GetCommand(command)
|
|
|
|
if !ok {
|
2021-01-18 03:08:13 +00:00
|
|
|
err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command)
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, err
|
2021-01-16 06:38:10 +00:00
|
|
|
}
|
|
|
|
fs := cmd.Flags
|
|
|
|
fs.Parse(args[2:])
|
|
|
|
|
|
|
|
cmdArgs.Flags = Flags{FlagSet: fs}
|
2021-02-07 23:31:19 +00:00
|
|
|
cmdArgs.Name = command
|
2021-01-16 06:38:10 +00:00
|
|
|
exitCode, err := cmd.Func(cmdArgs)
|
|
|
|
if err != nil {
|
2021-01-18 03:08:13 +00:00
|
|
|
err = fmt.Errorf("fatal: %s", err)
|
2021-01-16 06:38:10 +00:00
|
|
|
}
|
2021-01-18 03:08:13 +00:00
|
|
|
|
|
|
|
return exitCode, err
|
2021-01-16 06:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-02-14 00:42:47 +00:00
|
|
|
|
|
|
|
func initUserAgent() string {
|
|
|
|
var b strings.Builder
|
|
|
|
|
|
|
|
b.WriteString("lume")
|
|
|
|
b.WriteRune('/')
|
|
|
|
b.WriteString(Version)
|
|
|
|
return b.String()
|
|
|
|
}
|