2023-09-15 17:10:57 -04:00
|
|
|
/*
|
|
|
|
Package config provides types and methods for managing this application's
|
|
|
|
config options and flags
|
|
|
|
*/
|
2023-09-15 16:13:05 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
API APIConfig
|
|
|
|
Web WebConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type APIConfig struct {
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
|
|
|
type WebConfig struct {
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2023-09-15 17:10:57 -04:00
|
|
|
func UnmarshalConfig() (cfg Config, err error) {
|
2023-09-15 16:13:05 -04:00
|
|
|
return cfg, viper.Unmarshal(&cfg)
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:10:14 -04:00
|
|
|
// RegisterInt registers a new cobra int flag and associated viper config option
|
|
|
|
func RegisterInt(cmd *cobra.Command, isPersistent bool, viperName, cobraName, cobraDescription string, cobraDefault int) error {
|
|
|
|
if isPersistent {
|
|
|
|
cmd.PersistentFlags().Int(cobraName, cobraDefault, cobraDescription)
|
|
|
|
return viper.BindPFlag(viperName, cmd.PersistentFlags().Lookup(cobraName))
|
|
|
|
} else {
|
|
|
|
cmd.Flags().Int(cobraName, cobraDefault, cobraDescription)
|
|
|
|
return viper.BindPFlag(viperName, cmd.Flags().Lookup(cobraName))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterString registers a new cobra string flag and associated viper config option
|
|
|
|
func RegisterString(cmd *cobra.Command, isPersistent bool, viperName, cobraName, cobraDescription string, cobraDefault string) error {
|
|
|
|
if isPersistent {
|
|
|
|
cmd.PersistentFlags().String(cobraName, cobraDefault, cobraDescription)
|
|
|
|
return viper.BindPFlag(viperName, cmd.PersistentFlags().Lookup(cobraName))
|
|
|
|
} else {
|
|
|
|
cmd.Flags().String(cobraName, cobraDefault, cobraDescription)
|
|
|
|
return viper.BindPFlag(viperName, cmd.Flags().Lookup(cobraName))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 16:13:05 -04:00
|
|
|
// RegisterFlags adds persistent flags necessary to the application to the specified *cobra.Command
|
|
|
|
func RegisterFlags(cmd *cobra.Command) error {
|
|
|
|
// api.port ; --api-port
|
|
|
|
cmd.PersistentFlags().Int("api-port", 3182, "API server port")
|
|
|
|
if err := viper.BindPFlag("api.port", cmd.PersistentFlags().Lookup("api-port")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// web.port ; --web-port
|
|
|
|
cmd.PersistentFlags().Int("web-port", 3183, "Webserver port")
|
|
|
|
if err := viper.BindPFlag("web.port", cmd.PersistentFlags().Lookup("web-port")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|