From dfa5d41a6d9b69a77b94e44dbc461bc8bd965794 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Mon, 15 Mar 2021 22:26:19 -0500 Subject: [PATCH] Add a default configuration --- cmd/config.go | 40 ++++++++++++++++++++++++++++++++++++---- cmd/main.go | 22 ++++------------------ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index ee85515..cb708bc 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "strings" "github.com/BurntSushi/toml" ) @@ -15,8 +16,15 @@ type Config struct { AccessToken string `toml:"access_token"` OutputFormat string `toml:"output_format"` Colors map[string][]float32 `toml:"colors"` + userAgent string } +var ( + DefaultConfig = Config{ + userAgent: initUserAgent(), + } +) + // Validate configuration struct func (c *Config) Validate() error { var err error @@ -52,25 +60,40 @@ func (c *Config) validateColors() (err error) { return err } -func LoadConfig(s string) (Config, error) { +func LoadConfig(s string) (*Config, error) { var err error - var c Config + var c *Config = &Config{} + + *c = DefaultConfig if _, err := toml.Decode(s, &c); err != nil { err = fmt.Errorf("fatal: failed to parse; %w", err) } + envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN") + if envAccessToken != "" { + c.AccessToken = envAccessToken + } + return c, err } -func LoadConfigFile(configPath string) (Config, error) { +func LoadConfigFile(configPath string) (*Config, error) { var err error - var c Config + + var c *Config = &Config{} + + *c = DefaultConfig if _, err := toml.DecodeFile(configPath, &c); err != nil { 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 } @@ -97,3 +120,12 @@ func getConfigPath() string { return configPath } + +func initUserAgent() string { + var b strings.Builder + + b.WriteString("lume") + b.WriteRune('/') + b.WriteString(Version) + return b.String() +} diff --git a/cmd/main.go b/cmd/main.go index f8cb4a7..547f12d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,16 +5,11 @@ import ( "flag" "fmt" "os" - "strings" "git.kill0.net/chill9/lifx-go" ) -var userAgent string - func init() { - userAgent = initUserAgent() - RegisterCommand(NewCmdHelp()) RegisterCommand(NewCmdLs()) RegisterCommand(NewCmdPoweroff()) @@ -32,7 +27,7 @@ var BuildDate string var GitCommit string func Main(args []string) (int, error) { - var config Config + var config *Config var err error if len(args) == 1 { @@ -64,12 +59,12 @@ func Main(args []string) (int, error) { c := lifx.NewClient( config.AccessToken, - lifx.WithUserAgent(userAgent), + lifx.WithUserAgent(config.userAgent), ) cmdArgs := CmdArgs{ Client: c, - Config: config, + Config: *config, Args: args[2:], } @@ -92,13 +87,4 @@ func Main(args []string) (int, error) { } return exitCode, err -} - -func initUserAgent() string { - var b strings.Builder - - b.WriteString("lume") - b.WriteRune('/') - b.WriteString(Version) - return b.String() -} +} \ No newline at end of file