Move configuration loading to separate function

This commit is contained in:
Ryan Cavicchioni 2021-01-10 20:56:00 -06:00
parent 9988800431
commit 095c7bd995
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 31 additions and 3 deletions

1
.lumerc.sample Normal file
View File

@ -0,0 +1 @@
AccessToken = "token"

BIN
cmd/lume/__debug_bin Normal file

Binary file not shown.

View File

@ -24,9 +24,9 @@ func main() {
defer windows.SetConsoleMode(stdout, originalMode) defer windows.SetConsoleMode(stdout, originalMode)
var config lumecmd.Config var config lumecmd.Config
homeDir, err := os.UserHomeDir() config = loadConfig()
_, err = toml.DecodeFile(path.Join(homeDir, lumercFile), &config)
if os.IsNotExist(err) { if config.AccessToken == "" {
config.AccessToken = os.Getenv("LIFX_ACCESS_TOKEN") config.AccessToken = os.Getenv("LIFX_ACCESS_TOKEN")
} }
@ -61,3 +61,30 @@ func main() {
} }
os.Exit(exitCode) 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
}