Merge environment variables into Config

This commit is contained in:
Ryan Cavicchioni 2021-03-16 19:09:53 -05:00
parent d5db68dbfa
commit 454bfbf61d
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 9 additions and 17 deletions

View File

@ -60,6 +60,13 @@ func (c *Config) validateColors() (err error) {
return err return err
} }
func (c *Config) MergeWithEnv() {
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
c.AccessToken = envAccessToken
}
}
func LoadConfig(s string) (*Config, error) { func LoadConfig(s string) (*Config, error) {
var err error var err error
var c *Config = &Config{} var c *Config = &Config{}
@ -70,11 +77,6 @@ func LoadConfig(s string) (*Config, error) {
err = fmt.Errorf("fatal: failed to parse; %w", err) err = fmt.Errorf("fatal: failed to parse; %w", err)
} }
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
c.AccessToken = envAccessToken
}
return c, err return c, err
} }
@ -89,11 +91,6 @@ func LoadConfigFile(configPath string) (*Config, error) {
err = fmt.Errorf("fatal: failed to parse %s; %w", configPath, err) err = fmt.Errorf("fatal: failed to parse %s; %w", configPath, err)
} }
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
c.AccessToken = envAccessToken
}
return c, err return c, err
} }

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"os"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -43,11 +42,7 @@ func Main(args []string) (int, error) {
if config, err = LoadConfigFile(configPath); err != nil { if config, err = LoadConfigFile(configPath); err != nil {
return ExitFailure, err return ExitFailure, err
} }
config.MergeWithEnv()
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
config.AccessToken = envAccessToken
}
if err = config.Validate(); err != nil { if err = config.Validate(); err != nil {
return ExitFailure, fmt.Errorf("fatal: %s", err) return ExitFailure, fmt.Errorf("fatal: %s", err)