Compare commits
	
		
			4 Commits
		
	
	
		
			804ec99021
			...
			dfa5d41a6d
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						dfa5d41a6d
	
				 | 
					
					
						|||
| 
						
						
							
						
						32bce4eaba
	
				 | 
					
					
						|||
| 
						
						
							
						
						e86ce1aeaf
	
				 | 
					
					
						|||
| 
						
						
							
						
						b3a6dfbe07
	
				 | 
					
					
						
@@ -1,7 +1,6 @@
 | 
			
		||||
package lumecmd
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"flag"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strconv"
 | 
			
		||||
@@ -14,12 +13,6 @@ 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
 | 
			
		||||
@@ -96,38 +89,3 @@ 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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,102 @@
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
@@ -30,3 +120,12 @@ func getConfigPath() string {
 | 
			
		||||
 | 
			
		||||
	return configPath
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func initUserAgent() string {
 | 
			
		||||
	var b strings.Builder
 | 
			
		||||
 | 
			
		||||
	b.WriteString("lume")
 | 
			
		||||
	b.WriteRune('/')
 | 
			
		||||
	b.WriteString(Version)
 | 
			
		||||
	return b.String()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										24
									
								
								cmd/main.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								cmd/main.go
									
									
									
									
									
								
							@@ -5,17 +5,11 @@ 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())
 | 
			
		||||
@@ -33,7 +27,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 {
 | 
			
		||||
@@ -46,8 +40,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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -66,12 +59,12 @@ func Main(args []string) (int, error) {
 | 
			
		||||
 | 
			
		||||
	c := lifx.NewClient(
 | 
			
		||||
		config.AccessToken,
 | 
			
		||||
		lifx.WithUserAgent(userAgent),
 | 
			
		||||
		lifx.WithUserAgent(config.userAgent),
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	cmdArgs := CmdArgs{
 | 
			
		||||
		Client: c,
 | 
			
		||||
		Config: config,
 | 
			
		||||
		Config: *config,
 | 
			
		||||
		Args:   args[2:],
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -95,12 +88,3 @@ 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()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								lume.code-workspace
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								lume.code-workspace
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
{
 | 
			
		||||
	"folders": [
 | 
			
		||||
		{
 | 
			
		||||
			"path": "."
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			"name": "lifx-go",
 | 
			
		||||
			"path": "..\\lifx-go"
 | 
			
		||||
		}
 | 
			
		||||
	],
 | 
			
		||||
	"settings": {}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user