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"
|
|
|
|
|
|
|
|
lifx "git.kill0.net/chill9/lume"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
c := lifx.NewClient(config.AccessToken)
|
|
|
|
|
|
|
|
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}
|
|
|
|
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
|
|
|
|
}
|