Move lume main into the lumecmd package

This commit is contained in:
Ryan Cavicchioni 2021-01-16 00:38:10 -06:00
parent 84c1be5054
commit 4c6c3744c9
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 90 additions and 80 deletions

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"
) )
const lumercFile string = ".lumerc"
func main() { func main() {
var config lumecmd.Config lumecmd.Main(os.Args)
configPath := getConfigPath()
if configPath == "" {
fmt.Println("fatal: ~/.lumerc was not found")
os.Exit(1)
}
if _, err := toml.DecodeFile(configPath, &config); err != nil {
fmt.Printf("fatal: failed to parse %s\n", configPath)
fmt.Println(err)
os.Exit(1)
}
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
config.AccessToken = envAccessToken
}
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{FlagSet: fs}
exitCode, err := cmd.Func(cmdArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: %s\n", err)
}
os.Exit(exitCode)
}
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
} }

89
cmd/main.go Normal file
View File

@ -0,0 +1,89 @@
package lumecmd
import (
"flag"
"fmt"
"os"
"path"
lifx "git.kill0.net/chill9/lume"
"github.com/BurntSushi/toml"
)
const lumercFile string = ".lumerc"
func Main(args []string) {
var config Config
configPath := getConfigPath()
if configPath == "" {
fmt.Println("fatal: ~/.lumerc was not found")
os.Exit(1)
}
if _, err := toml.DecodeFile(configPath, &config); err != nil {
fmt.Printf("fatal: failed to parse %s\n", configPath)
fmt.Println(err)
os.Exit(1)
}
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
config.AccessToken = envAccessToken
}
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 := CmdArgs{
Client: c,
Config: config,
}
cmd, ok := 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(args[2:])
cmdArgs.Flags = Flags{FlagSet: fs}
exitCode, err := cmd.Func(cmdArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: %s\n", err)
}
os.Exit(exitCode)
}
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
}