Add a default configuration

This commit is contained in:
Ryan Cavicchioni 2021-03-15 22:26:19 -05:00
parent 32bce4eaba
commit dfa5d41a6d
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 40 additions and 22 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
) )
@ -15,8 +16,15 @@ type Config struct {
AccessToken string `toml:"access_token"` AccessToken string `toml:"access_token"`
OutputFormat string `toml:"output_format"` OutputFormat string `toml:"output_format"`
Colors map[string][]float32 `toml:"colors"` Colors map[string][]float32 `toml:"colors"`
userAgent string
} }
var (
DefaultConfig = Config{
userAgent: initUserAgent(),
}
)
// Validate configuration struct // Validate configuration struct
func (c *Config) Validate() error { func (c *Config) Validate() error {
var err error var err error
@ -52,25 +60,40 @@ func (c *Config) validateColors() (err error) {
return err return err
} }
func LoadConfig(s string) (Config, error) { func LoadConfig(s string) (*Config, error) {
var err error var err error
var c Config var c *Config = &Config{}
*c = DefaultConfig
if _, err := toml.Decode(s, &c); err != nil { if _, err := toml.Decode(s, &c); err != nil {
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
} }
func LoadConfigFile(configPath string) (Config, error) { func LoadConfigFile(configPath string) (*Config, error) {
var err error var err error
var c Config
var c *Config = &Config{}
*c = DefaultConfig
if _, err := toml.DecodeFile(configPath, &c); err != nil { if _, err := toml.DecodeFile(configPath, &c); err != nil {
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
} }
@ -97,3 +120,12 @@ func getConfigPath() string {
return configPath return configPath
} }
func initUserAgent() string {
var b strings.Builder
b.WriteString("lume")
b.WriteRune('/')
b.WriteString(Version)
return b.String()
}

View File

@ -5,16 +5,11 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"strings"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
var userAgent string
func init() { func init() {
userAgent = initUserAgent()
RegisterCommand(NewCmdHelp()) RegisterCommand(NewCmdHelp())
RegisterCommand(NewCmdLs()) RegisterCommand(NewCmdLs())
RegisterCommand(NewCmdPoweroff()) RegisterCommand(NewCmdPoweroff())
@ -32,7 +27,7 @@ var BuildDate string
var GitCommit string var GitCommit string
func Main(args []string) (int, error) { func Main(args []string) (int, error) {
var config Config var config *Config
var err error var err error
if len(args) == 1 { if len(args) == 1 {
@ -64,12 +59,12 @@ func Main(args []string) (int, error) {
c := lifx.NewClient( c := lifx.NewClient(
config.AccessToken, config.AccessToken,
lifx.WithUserAgent(userAgent), lifx.WithUserAgent(config.userAgent),
) )
cmdArgs := CmdArgs{ cmdArgs := CmdArgs{
Client: c, Client: c,
Config: config, Config: *config,
Args: args[2:], Args: args[2:],
} }
@ -92,13 +87,4 @@ func Main(args []string) (int, error) {
} }
return exitCode, err return exitCode, err
} }
func initUserAgent() string {
var b strings.Builder
b.WriteString("lume")
b.WriteRune('/')
b.WriteString(Version)
return b.String()
}