Add functions to load configuration

This commit is contained in:
Ryan Cavicchioni 2021-03-14 22:11:12 -05:00
parent e86ce1aeaf
commit 32bce4eaba
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 25 additions and 3 deletions

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"github.com/BurntSushi/toml"
) )
const lumercFile string = ".lumerc" const lumercFile string = ".lumerc"
@ -50,6 +52,28 @@ func (c *Config) validateColors() (err error) {
return err return err
} }
func LoadConfig(s string) (Config, error) {
var err error
var c Config
if _, err := toml.Decode(s, &c); err != nil {
err = fmt.Errorf("fatal: failed to parse; %w", err)
}
return c, err
}
func LoadConfigFile(configPath string) (Config, error) {
var err error
var c Config
if _, err := toml.DecodeFile(configPath, &c); err != nil {
err = fmt.Errorf("fatal: failed to parse %s; %w", configPath, err)
}
return c, err
}
func getConfigPath() string { func getConfigPath() string {
var tryPath, configPath string var tryPath, configPath string

View File

@ -8,7 +8,6 @@ import (
"strings" "strings"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
"github.com/BurntSushi/toml"
) )
var userAgent string var userAgent string
@ -46,8 +45,7 @@ func Main(args []string) (int, error) {
return ExitFailure, err return ExitFailure, err
} }
if _, err := toml.DecodeFile(configPath, &config); err != nil { if config, err = LoadConfigFile(configPath); err != nil {
err = fmt.Errorf("fatal: failed to parse %s; %w", configPath, err)
return ExitFailure, err return ExitFailure, err
} }