Compare commits

..

No commits in common. "dfa5d41a6d9b69a77b94e44dbc461bc8bd965794" and "804ec99021c94342330d4fc26c3b05da25de7c68" have entirely different histories.

4 changed files with 63 additions and 116 deletions

View File

@ -1,6 +1,7 @@
package lumecmd
import (
"errors"
"flag"
"fmt"
"strconv"
@ -13,6 +14,12 @@ const (
ExitFailure
)
type Config struct {
AccessToken string `toml:"access_token"`
OutputFormat string `toml:"output_format"`
Colors map[string][]float32 `toml:"colors"`
}
type CmdArgs struct {
Flags Flags
Args []string
@ -89,3 +96,38 @@ func GetCommand(name string) (Command, bool) {
cmd, ok := commandRegistry[name]
return cmd, ok
}
// Validate configuration struct
func (c *Config) Validate() error {
var err error
if c.AccessToken == "" {
err = errors.New("access_token is not set")
}
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
}

View File

@ -1,102 +1,12 @@
package lumecmd
import (
"errors"
"fmt"
"os"
"path"
"strings"
"github.com/BurntSushi/toml"
)
const lumercFile string = ".lumerc"
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
if c.AccessToken == "" {
err = errors.New("access_token is not set")
}
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 LoadConfig(s string) (*Config, error) {
var err error
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) {
var err error
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
}
func getConfigPath() string {
var tryPath, configPath string
@ -120,12 +30,3 @@ func getConfigPath() string {
return configPath
}
func initUserAgent() string {
var b strings.Builder
b.WriteString("lume")
b.WriteRune('/')
b.WriteString(Version)
return b.String()
}

View File

@ -5,11 +5,17 @@ import (
"flag"
"fmt"
"os"
"strings"
"git.kill0.net/chill9/lifx-go"
"github.com/BurntSushi/toml"
)
var userAgent string
func init() {
userAgent = initUserAgent()
RegisterCommand(NewCmdHelp())
RegisterCommand(NewCmdLs())
RegisterCommand(NewCmdPoweroff())
@ -27,7 +33,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 {
@ -40,7 +46,8 @@ func Main(args []string) (int, error) {
return ExitFailure, err
}
if config, err = LoadConfigFile(configPath); err != nil {
if _, err := toml.DecodeFile(configPath, &config); err != nil {
err = fmt.Errorf("fatal: failed to parse %s; %w", configPath, err)
return ExitFailure, err
}
@ -59,12 +66,12 @@ func Main(args []string) (int, error) {
c := lifx.NewClient(
config.AccessToken,
lifx.WithUserAgent(config.userAgent),
lifx.WithUserAgent(userAgent),
)
cmdArgs := CmdArgs{
Client: c,
Config: *config,
Config: config,
Args: args[2:],
}
@ -87,4 +94,13 @@ 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()
}

View File

@ -1,12 +0,0 @@
{
"folders": [
{
"path": "."
},
{
"name": "lifx-go",
"path": "..\\lifx-go"
}
],
"settings": {}
}