feat(cli): add configuration-file persistant flag

This commit is contained in:
DataHearth 2022-02-26 15:45:10 +01:00
parent efb709d31b
commit ea80957f90
2 changed files with 17 additions and 4 deletions

View File

@ -85,6 +85,9 @@ func init() {
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(loadCmd)
rootCmd.AddCommand(saveCmd)
rootCmd.PersistentFlags().StringP("configuration-file", "c", "", "location of configuration file")
viper.BindPFlag("configuration-file", rootCmd.PersistentFlags().Lookup("configuration-file"))
loadCmd.PersistentFlags().Bool("disable-files", false, "files will be ignored")
loadCmd.PersistentFlags().Bool("disable-folders", false, "folders will be ignored")

View File

@ -3,6 +3,8 @@ package mapper
import (
"fmt"
"os"
"path"
"strings"
"github.com/pterm/pterm"
"github.com/spf13/viper"
@ -54,11 +56,19 @@ func InitConfig() {
os.Exit(1)
}
if c := os.Getenv("CONFIG_MAPPER_CFG"); c != "" {
viper.AddConfigPath(c)
} else {
viper.AddConfigPath(h)
if c := viper.GetString("configuration-file"); c != "" {
if strings.Contains(c, ".yml") {
c = path.Dir(c)
}
viper.AddConfigPath(path.Dir(c))
}
if c := os.Getenv("CONFIG_MAPPER_CFG"); c != "" {
if strings.Contains(c, ".yml") {
c = path.Dir(c)
}
viper.AddConfigPath(c)
}
viper.AddConfigPath(h)
viper.SetConfigType("yml")
viper.SetConfigName(".config-mapper")