lume/cmd/config.go

158 lines
3.2 KiB
Go
Raw Normal View History

package lumecmd
import (
2021-03-14 18:51:55 +00:00
"errors"
"fmt"
"os"
"path"
2021-03-16 03:26:19 +00:00
"strings"
2021-03-15 03:11:12 +00:00
"github.com/BurntSushi/toml"
)
const lumercFile string = ".lumerc"
const lumeConfigFile string = "lume.conf"
2021-04-04 23:34:52 +00:00
const defaultPowerIndicator rune = '●'
2021-03-14 18:51:55 +00:00
type Config struct {
AccessToken string `toml:"access_token"`
OutputFormat string `toml:"output_format"`
Colors map[string][]float32 `toml:"colors"`
2021-03-16 03:26:19 +00:00
userAgent string
2021-04-04 23:34:52 +00:00
Debug bool `toml:"debug"`
Indicator string `toml:"indicator"`
2021-03-14 18:51:55 +00:00
}
2021-03-16 03:26:19 +00:00
var (
DefaultConfig = Config{
userAgent: initUserAgent(),
}
globalConfig *Config = NewConfig()
2021-03-16 03:26:19 +00:00
)
func NewConfig() *Config {
c := new(Config)
c.userAgent = initUserAgent()
c.Debug = false
c.OutputFormat = "simple"
2021-04-04 23:34:52 +00:00
c.Indicator = string(defaultPowerIndicator)
return c
}
func GetConfig() *Config {
return globalConfig
}
2021-03-14 18:51:55 +00:00
// Validate configuration struct
func (c *Config) Validate() error {
var err error
2021-03-14 18:51:55 +00:00
if c.AccessToken == "" {
return errors.New("access_token is not set")
2021-03-14 18:51:55 +00:00
}
2021-04-04 23:34:52 +00:00
if len([]rune(c.Indicator)) != 1 {
return errors.New("indicator must be a single rune")
}
2021-03-14 18:51:55 +00:00
if err = c.validateColors(); err != nil {
return err
}
return err
}
func (c *Config) validateColors() (err error) {
if len(c.Colors) > 0 {
for name, hsb := range c.Colors {
if len(hsb) != 3 {
return fmt.Errorf("color '%s' needs three values", name)
}
h, s, b := hsb[0], hsb[1], hsb[2]
if h < 0 || h > 360 {
return fmt.Errorf("color '%s' hue value must be between 0.0-360.0", name)
}
if s < 0 || b > 1 {
return fmt.Errorf("color '%s' saturation value must be between 0.0-1.0", name)
}
if b < 0 || b > 1 {
return fmt.Errorf("color '%s' brightness value must be between 0.0-1.0", name)
}
}
}
return err
}
func (c *Config) MergeWithEnv() {
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
c.AccessToken = envAccessToken
}
}
2021-03-16 03:26:19 +00:00
func LoadConfig(s string) (*Config, error) {
2021-03-15 03:11:12 +00:00
var err error
var c *Config = GetConfig()
2021-03-16 03:26:19 +00:00
2021-03-15 03:11:12 +00:00
if _, err := toml.Decode(s, &c); err != nil {
err = fmt.Errorf("fatal: failed to parse; %w", err)
}
return c, err
}
2021-03-16 03:26:19 +00:00
func LoadConfigFile(configPath string) (*Config, error) {
2021-03-15 03:11:12 +00:00
var err error
2021-03-16 03:26:19 +00:00
var c *Config = GetConfig()
2021-03-16 03:26:19 +00:00
2021-03-15 03:11:12 +00:00
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, homeDir, cwd string
var err error
// ~/.lumerc
homeDir, err = os.UserHomeDir()
if err == nil {
tryPath = path.Join(homeDir, lumercFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
// ~/.config/lume/lume.conf
homeDir, err = os.UserHomeDir()
if err == nil {
tryPath = path.Join(homeDir, ".config/lume", lumeConfigFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
// ./.lumerc
cwd, err = os.Getwd()
if err == nil {
tryPath = path.Join(cwd, lumercFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
return configPath
}
2021-03-16 03:26:19 +00:00
func initUserAgent() string {
var b strings.Builder
b.WriteString("lume")
b.WriteRune('/')
b.WriteString(Version)
return b.String()
}