feat: added config implementation using viper
This commit is contained in:
parent
7713c2e55d
commit
481a0782c5
6 changed files with 215 additions and 64 deletions
141
cmd/root.go
141
cmd/root.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
logger "git.towk2.me/towk/makeshift/pkg/log"
|
||||
|
|
@ -20,14 +21,14 @@ var (
|
|||
var rootCmd = cobra.Command{
|
||||
Use: "makeshift",
|
||||
Short: "Extensible file cobbler",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
var (
|
||||
logFile string
|
||||
err error
|
||||
logFile, _ = cmd.Flags().GetString("log-file")
|
||||
configPath, _ = cmd.Flags().GetString("config")
|
||||
err error
|
||||
)
|
||||
|
||||
// initialize the logger
|
||||
logFile, _ = cmd.Flags().GetString("log-file")
|
||||
err = logger.InitWithLogLevel(loglevel, logFile)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to initialize logger")
|
||||
|
|
@ -35,12 +36,13 @@ var rootCmd = cobra.Command{
|
|||
}
|
||||
|
||||
// You can bind cobra and viper in a few locations, but PersistencePreRunE on the root command works well
|
||||
err = initializeConfig(cmd)
|
||||
return initConfig(cmd, configPath)
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// try and set flags using env vars
|
||||
setenv(cmd, "log-file", "MAKESHIFT_LOG_FILE")
|
||||
setenv(cmd, "log-level", "MAKESHIFT_LOG_LEVEL")
|
||||
setenv(cmd, "config", "MAKESHIFT_CONFIG_FILE")
|
||||
if len(args) == 0 {
|
||||
err := cmd.Help()
|
||||
if err != nil {
|
||||
|
|
@ -54,6 +56,7 @@ var rootCmd = cobra.Command{
|
|||
err := logger.LogFile.Close()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to close log file")
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
@ -71,8 +74,83 @@ func init() {
|
|||
initLogger,
|
||||
)
|
||||
// initialize the config a single time
|
||||
rootCmd.PersistentFlags().VarP(&loglevel, "log-level", "l", "Set the log level output")
|
||||
rootCmd.PersistentFlags().VarP(&loglevel, "log-level", "l", "Set the log level output (can be set with MAKESHIFT_LOG_LEVEL)")
|
||||
rootCmd.PersistentFlags().String("log-file", "", "Set the log file path (can be set with MAKESHIFT_LOG_FILE)")
|
||||
rootCmd.PersistentFlags().StringP("config", "c", "", "Set the config file path (can be set with MAKESHIFT_CONFIG_FILE)")
|
||||
}
|
||||
|
||||
func initLogger() {
|
||||
// initialize the logger
|
||||
logfile, _ := rootCmd.PersistentFlags().GetString("log-file")
|
||||
err := logger.InitWithLogLevel(loglevel, logfile)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to initialize logger")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func initConfig(cmd *cobra.Command, path string) error {
|
||||
|
||||
// Dissect the path to separate config name from its directory
|
||||
var (
|
||||
isFlagSet = cmd.Flags().Changed("config")
|
||||
filename = filepath.Base(path)
|
||||
ext = filepath.Ext(filename)
|
||||
directory = filepath.Dir(path)
|
||||
v = viper.New()
|
||||
)
|
||||
|
||||
// The 'config' flag not set, so don't continue
|
||||
if !isFlagSet {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Only use specified YAML file from --config or -c flag
|
||||
v.SetConfigName(strings.TrimSuffix(filename, ext))
|
||||
v.SetConfigType("yaml")
|
||||
v.AddConfigPath(directory)
|
||||
|
||||
// Attempt to read the config file. Return an error if we cannot parse
|
||||
// the config file or if it is not found.
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
if isFlagSet {
|
||||
// It's okay if there isn't a config file when no path is provided
|
||||
// if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||
// return err
|
||||
// }
|
||||
switch err.(type) {
|
||||
case viper.ConfigFileNotFoundError:
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("path", path).
|
||||
Msg("failed to read config")
|
||||
os.Exit(1)
|
||||
default:
|
||||
log.Error().Err(err).Msg("failed to read config")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// When we bind flags to environment variables expect that the
|
||||
// environment variables are prefixed, e.g. a flag like --number
|
||||
// binds to an environment variable STING_NUMBER. This helps
|
||||
// avoid conflicts.
|
||||
v.SetEnvPrefix("MAKESHIFT")
|
||||
|
||||
// Environment variables can't have dashes in them, so bind them to their equivalent
|
||||
// keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
||||
|
||||
// Bind to environment variables
|
||||
// Works great for simple config names, but needs help for names
|
||||
// like --favorite-color which we fix in the bindFlags function
|
||||
v.AutomaticEnv()
|
||||
|
||||
// Bind the current command's flags to viper
|
||||
bindFlags(cmd, v)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setenv(cmd *cobra.Command, varname string, envvar string) {
|
||||
|
|
@ -95,16 +173,6 @@ func setenvp(cmd *cobra.Command, varname string, envvar string) {
|
|||
}
|
||||
}
|
||||
|
||||
func initLogger() {
|
||||
// initialize the logger
|
||||
logfile, _ := rootCmd.PersistentFlags().GetString("log-file")
|
||||
err := logger.InitWithLogLevel(loglevel, logfile)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to initialize logger")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func handleResponseError(res *http.Response, host, query string, err error) {
|
||||
if err != nil {
|
||||
log.Error().Err(err).
|
||||
|
|
@ -147,44 +215,3 @@ func bindFlags(cmd *cobra.Command, v *viper.Viper) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func initializeConfig(cmd *cobra.Command) error {
|
||||
v := viper.New()
|
||||
|
||||
// Set the base name of the config file, without the file extension.
|
||||
v.SetConfigName("makeshift")
|
||||
|
||||
// Set as many paths as you like where viper should look for the
|
||||
// config file. We are only looking in the current working directory.
|
||||
v.AddConfigPath(".")
|
||||
|
||||
// Attempt to read the config file, gracefully ignoring errors
|
||||
// caused by a config file not being found. Return an error
|
||||
// if we cannot parse the config file.
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
// It's okay if there isn't a config file
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// When we bind flags to environment variables expect that the
|
||||
// environment variables are prefixed, e.g. a flag like --number
|
||||
// binds to an environment variable STING_NUMBER. This helps
|
||||
// avoid conflicts.
|
||||
v.SetEnvPrefix("MAKESHIFT")
|
||||
|
||||
// Environment variables can't have dashes in them, so bind them to their equivalent
|
||||
// keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
||||
|
||||
// Bind to environment variables
|
||||
// Works great for simple config names, but needs help for names
|
||||
// like --favorite-color which we fix in the bindFlags function
|
||||
v.AutomaticEnv()
|
||||
|
||||
// Bind the current command's flags to viper
|
||||
bindFlags(cmd, v)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue