2020-10-23 03:54:50 +13:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Root struct {
|
2022-01-17 09:03:37 +13:00
|
|
|
Debug bool
|
|
|
|
Logs bool
|
|
|
|
Config string
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (Root) Init(cmd *cobra.Command) error {
|
|
|
|
cmd.PersistentFlags().BoolP("debug", "d", false, "enable debug mode")
|
|
|
|
if err := viper.BindPFlag("debug", cmd.PersistentFlags().Lookup("debug")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.PersistentFlags().BoolP("logs", "l", false, "save logs to file")
|
|
|
|
if err := viper.BindPFlag("logs", cmd.PersistentFlags().Lookup("logs")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-17 09:03:37 +13:00
|
|
|
cmd.PersistentFlags().StringP("config", "c", "", "configuration file path")
|
2020-10-23 03:54:50 +13:00
|
|
|
if err := viper.BindPFlag("config", cmd.PersistentFlags().Lookup("config")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Root) Set() {
|
|
|
|
s.Logs = viper.GetBool("logs")
|
|
|
|
s.Debug = viper.GetBool("debug")
|
2022-01-17 09:03:37 +13:00
|
|
|
s.Config = viper.GetString("config")
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|