Move configuration loading to separate function
This commit is contained in:
parent
9988800431
commit
095c7bd995
1
.lumerc.sample
Normal file
1
.lumerc.sample
Normal file
@ -0,0 +1 @@
|
||||
AccessToken = "token"
|
BIN
cmd/lume/__debug_bin
Normal file
BIN
cmd/lume/__debug_bin
Normal file
Binary file not shown.
@ -24,9 +24,9 @@ func main() {
|
||||
defer windows.SetConsoleMode(stdout, originalMode)
|
||||
|
||||
var config lumecmd.Config
|
||||
homeDir, err := os.UserHomeDir()
|
||||
_, err = toml.DecodeFile(path.Join(homeDir, lumercFile), &config)
|
||||
if os.IsNotExist(err) {
|
||||
config = loadConfig()
|
||||
|
||||
if config.AccessToken == "" {
|
||||
config.AccessToken = os.Getenv("LIFX_ACCESS_TOKEN")
|
||||
}
|
||||
|
||||
@ -61,3 +61,30 @@ func main() {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user