From 32bce4eaba4b2cde8f048e83855294fa6a749211 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Sun, 14 Mar 2021 22:11:12 -0500 Subject: [PATCH] Add functions to load configuration --- cmd/config.go | 24 ++++++++++++++++++++++++ cmd/main.go | 4 +--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 04b05d0..ee85515 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -5,6 +5,8 @@ import ( "fmt" "os" "path" + + "github.com/BurntSushi/toml" ) const lumercFile string = ".lumerc" @@ -50,6 +52,28 @@ func (c *Config) validateColors() (err error) { 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 { var tryPath, configPath string diff --git a/cmd/main.go b/cmd/main.go index a49dee8..f8cb4a7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -8,7 +8,6 @@ import ( "strings" "git.kill0.net/chill9/lifx-go" - "github.com/BurntSushi/toml" ) var userAgent string @@ -46,8 +45,7 @@ func Main(args []string) (int, error) { return ExitFailure, err } - if _, err := toml.DecodeFile(configPath, &config); err != nil { - err = fmt.Errorf("fatal: failed to parse %s; %w", configPath, err) + if config, err = LoadConfigFile(configPath); err != nil { return ExitFailure, err }