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"
|
|
|
|
|
2021-02-15 01:02:26 +00:00
|
|
|
"git.kill0.net/chill9/lifx-go"
|
2021-01-16 06:38:10 +00:00
|
|
|
)
|
|
|
|
|
2021-02-03 00:37:26 +00:00
|
|
|
func init() {
|
2021-02-17 05:24:32 +00:00
|
|
|
RegisterCommand(NewCmdHelp())
|
|
|
|
RegisterCommand(NewCmdLs())
|
|
|
|
RegisterCommand(NewCmdPoweroff())
|
|
|
|
RegisterCommand(NewCmdPoweron())
|
|
|
|
RegisterCommand(NewCmdSetColor())
|
|
|
|
RegisterCommand(NewCmdSetState())
|
|
|
|
RegisterCommand(NewCmdSetWhite())
|
|
|
|
RegisterCommand(NewCmdShow())
|
|
|
|
RegisterCommand(NewCmdToggle())
|
2021-02-28 04:42:09 +00:00
|
|
|
RegisterCommand(NewCmdVersion())
|
2021-03-25 14:46:58 +00:00
|
|
|
RegisterCommand(NewCmdBreathe())
|
2021-02-03 00:37:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 03:39:47 +00:00
|
|
|
var Version string
|
2021-03-03 02:41:18 +00:00
|
|
|
var BuildDate string
|
2021-03-11 03:39:47 +00:00
|
|
|
var GitCommit string
|
2021-02-28 04:42:09 +00:00
|
|
|
|
2021-01-18 03:08:13 +00:00
|
|
|
func Main(args []string) (int, error) {
|
2021-03-16 03:26:19 +00:00
|
|
|
var config *Config
|
2021-01-18 03:08:13 +00:00
|
|
|
var err error
|
2021-03-29 02:33:43 +00:00
|
|
|
var i int
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
i = flag.NFlag() + 1
|
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
|
|
|
}
|
|
|
|
|
2021-03-15 03:11:12 +00:00
|
|
|
if config, err = LoadConfigFile(configPath); err != nil {
|
2021-01-31 17:08:24 +00:00
|
|
|
return ExitFailure, err
|
2021-01-16 06:38:10 +00:00
|
|
|
}
|
2021-03-17 00:09:53 +00:00
|
|
|
config.MergeWithEnv()
|
2021-01-16 06:38:10 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-29 02:33:43 +00:00
|
|
|
command := args[i]
|
|
|
|
i++
|
2021-01-16 06:38:10 +00:00
|
|
|
|
2021-02-11 15:16:24 +00:00
|
|
|
c := lifx.NewClient(
|
|
|
|
config.AccessToken,
|
2021-03-16 03:26:19 +00:00
|
|
|
lifx.WithUserAgent(config.userAgent),
|
2021-02-11 15:16:24 +00:00
|
|
|
)
|
2021-01-16 06:38:10 +00:00
|
|
|
|
2021-03-17 00:23:50 +00:00
|
|
|
Context := Context{
|
2021-01-16 06:38:10 +00:00
|
|
|
Client: c,
|
2021-03-16 03:26:19 +00:00
|
|
|
Config: *config,
|
2021-03-29 02:33:43 +00:00
|
|
|
Args: args[i:],
|
2021-01-16 06:38:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-04 05:31:27 +00:00
|
|
|
fs := cmd.Flags
|
|
|
|
if fs != nil {
|
2021-03-29 02:33:43 +00:00
|
|
|
fs.Parse(args[i:])
|
2021-03-17 00:23:50 +00:00
|
|
|
Context.Flags = Flags{FlagSet: fs}
|
2021-03-04 05:31:27 +00:00
|
|
|
}
|
2021-03-17 00:23:50 +00:00
|
|
|
Context.Name = command
|
2021-03-04 05:31:27 +00:00
|
|
|
|
2021-03-17 00:23:50 +00:00
|
|
|
exitCode, err := cmd.Func(Context)
|
2021-01-16 06:38:10 +00:00
|
|
|
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-03-17 00:09:53 +00:00
|
|
|
}
|