feat: replace config system with viper based system
allows for config settings to be easily set through ENV variables, config files, or CLI flags adds flag to run migration on web server start (fixes #29)
This commit is contained in:
@ -2,7 +2,11 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const TaskcafeConfDirEnvName = "TASKCAFE_CONFIG_DIR"
|
||||
@ -26,6 +30,7 @@ var commandError error
|
||||
var configDir string
|
||||
var verbose bool
|
||||
var noColor bool
|
||||
var cfgFile string
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "taskcafe",
|
||||
@ -33,6 +38,36 @@ var rootCmd = &cobra.Command{
|
||||
Version: version,
|
||||
}
|
||||
|
||||
var migration http.FileSystem
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file path")
|
||||
migration = http.Dir("./migrations")
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Search config in home directory with name ".cobra" (without extension).
|
||||
viper.AddConfigPath("./conf")
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath("/etc/taskcafe")
|
||||
viper.SetConfigName("taskcafe")
|
||||
}
|
||||
|
||||
viper.SetEnvPrefix("TASKCAFE")
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv()
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
rootCmd.SetVersionTemplate(versionTemplate)
|
||||
rootCmd.AddCommand(newWebCmd(), newMigrateCmd(), newTokenCmd())
|
||||
|
Reference in New Issue
Block a user