Compare commits

..

2 Commits

Author SHA1 Message Date
9338631de9 Add debug output support 2021-03-29 17:22:22 -05:00
a4638db773 Make configuration global, add helper functions 2021-03-29 17:21:12 -05:00
4 changed files with 33 additions and 3 deletions

View File

@ -17,14 +17,28 @@ type Config struct {
OutputFormat string `toml:"output_format"` OutputFormat string `toml:"output_format"`
Colors map[string][]float32 `toml:"colors"` Colors map[string][]float32 `toml:"colors"`
userAgent string userAgent string
Debug bool `toml:"debug"`
} }
var ( var (
DefaultConfig = Config{ DefaultConfig = Config{
userAgent: initUserAgent(), userAgent: initUserAgent(),
} }
globalConfig *Config = NewConfig()
) )
func NewConfig() *Config {
c := new(Config)
c.userAgent = initUserAgent()
c.Debug = false
c.OutputFormat = "simple"
return c
}
func GetConfig() *Config {
return globalConfig
}
// Validate configuration struct // Validate configuration struct
func (c *Config) Validate() error { func (c *Config) Validate() error {
var err error var err error
@ -69,7 +83,7 @@ func (c *Config) MergeWithEnv() {
func LoadConfig(s string) (*Config, error) { func LoadConfig(s string) (*Config, error) {
var err error var err error
var c *Config = &Config{} var c *Config = GetConfig()
*c = DefaultConfig *c = DefaultConfig
@ -83,7 +97,7 @@ func LoadConfig(s string) (*Config, error) {
func LoadConfigFile(configPath string) (*Config, error) { func LoadConfigFile(configPath string) (*Config, error) {
var err error var err error
var c *Config = &Config{} var c *Config = GetConfig()
*c = DefaultConfig *c = DefaultConfig

View File

@ -35,6 +35,9 @@ func LsCmd(ctx Context) (int, error) {
} }
lights, err := c.ListLights(selector) lights, err := c.ListLights(selector)
Debugf("%+v\n", lights)
if err != nil { if err != nil {
return ExitFailure, err return ExitFailure, err
} }

View File

@ -20,14 +20,18 @@ func init() {
RegisterCommand(NewCmdToggle()) RegisterCommand(NewCmdToggle())
RegisterCommand(NewCmdVersion()) RegisterCommand(NewCmdVersion())
RegisterCommand(NewCmdBreathe()) RegisterCommand(NewCmdBreathe())
flag.BoolVar(&debugFlag, "debug", false, "debug mode")
flag.BoolVar(&debugFlag, "d", false, "debug mode")
} }
var Version string var Version string
var BuildDate string var BuildDate string
var GitCommit string var GitCommit string
var debugFlag bool
func Main(args []string) (int, error) { func Main(args []string) (int, error) {
var config *Config var config *Config = GetConfig()
var err error var err error
var i int var i int
@ -53,12 +57,15 @@ func Main(args []string) (int, error) {
return ExitFailure, fmt.Errorf("fatal: %s", err) return ExitFailure, fmt.Errorf("fatal: %s", err)
} }
config.Debug = debugFlag
command := args[i] command := args[i]
i++ i++
c := lifx.NewClient( c := lifx.NewClient(
config.AccessToken, config.AccessToken,
lifx.WithUserAgent(config.userAgent), lifx.WithUserAgent(config.userAgent),
lifx.WithDebug(debugFlag),
) )
Context := Context{ Context := Context{

View File

@ -60,3 +60,9 @@ func YesNo(v bool) string {
} }
return "no" return "no"
} }
func Debugf(format string, a ...interface{}) {
if GetConfig().Debug {
fmt.Printf(format, a...)
}
}