Move more configuration related code
This commit is contained in:
		@@ -1,7 +1,6 @@
 | 
				
			|||||||
package lumecmd
 | 
					package lumecmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
					 | 
				
			||||||
	"flag"
 | 
						"flag"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
@@ -14,12 +13,6 @@ const (
 | 
				
			|||||||
	ExitFailure
 | 
						ExitFailure
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Config struct {
 | 
					 | 
				
			||||||
	AccessToken  string               `toml:"access_token"`
 | 
					 | 
				
			||||||
	OutputFormat string               `toml:"output_format"`
 | 
					 | 
				
			||||||
	Colors       map[string][]float32 `toml:"colors"`
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type CmdArgs struct {
 | 
					type CmdArgs struct {
 | 
				
			||||||
	Flags  Flags
 | 
						Flags  Flags
 | 
				
			||||||
	Args   []string
 | 
						Args   []string
 | 
				
			||||||
@@ -96,38 +89,3 @@ func GetCommand(name string) (Command, bool) {
 | 
				
			|||||||
	cmd, ok := commandRegistry[name]
 | 
						cmd, ok := commandRegistry[name]
 | 
				
			||||||
	return cmd, ok
 | 
						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,55 @@
 | 
				
			|||||||
package lumecmd
 | 
					package lumecmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const lumercFile string = ".lumerc"
 | 
					const lumercFile string = ".lumerc"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Config struct {
 | 
				
			||||||
 | 
						AccessToken  string               `toml:"access_token"`
 | 
				
			||||||
 | 
						OutputFormat string               `toml:"output_format"`
 | 
				
			||||||
 | 
						Colors       map[string][]float32 `toml:"colors"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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 getConfigPath() string {
 | 
					func getConfigPath() string {
 | 
				
			||||||
	var tryPath, configPath string
 | 
						var tryPath, configPath string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user