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"`
Colors map[string][]float32 `toml:"colors"`
userAgent string
Debug bool `toml:"debug"`
}
var (
DefaultConfig = Config{
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
func (c *Config) Validate() error {
var err error
@ -69,7 +83,7 @@ func (c *Config) MergeWithEnv() {
func LoadConfig(s string) (*Config, error) {
var err error
var c *Config = &Config{}
var c *Config = GetConfig()
*c = DefaultConfig
@ -83,7 +97,7 @@ func LoadConfig(s string) (*Config, error) {
func LoadConfigFile(configPath string) (*Config, error) {
var err error
var c *Config = &Config{}
var c *Config = GetConfig()
*c = DefaultConfig

View File

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

View File

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

View File

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