|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "os" |
5 | 6 |
|
| 7 | + "github.com/major-technology/cli/configs" |
6 | 8 | "github.com/spf13/cobra" |
7 | 9 | "github.com/spf13/viper" |
8 | 10 | ) |
@@ -39,14 +41,33 @@ func init() { |
39 | 41 | } |
40 | 42 |
|
41 | 43 | func initConfig() { |
42 | | - if cfgFile != "" { |
43 | | - viper.SetConfigFile(cfgFile) |
| 44 | + // Use embedded config based on defaultConfig variable |
| 45 | + var configData []byte |
| 46 | + if defaultConfig == "configs/prod.json" { |
| 47 | + configData = configs.ProdConfig |
44 | 48 | } else { |
45 | | - home, err := os.UserHomeDir() |
| 49 | + configData = configs.LocalConfig |
| 50 | + } |
| 51 | + |
| 52 | + // Parse embedded config into viper |
| 53 | + var config map[string]interface{} |
| 54 | + if err := json.Unmarshal(configData, &config); err != nil { |
46 | 55 | cobra.CheckErr(err) |
47 | | - viper.AddConfigPath(home) |
48 | | - viper.SetConfigType("yaml") |
49 | | - viper.SetConfigName(".cli") |
50 | 56 | } |
51 | | - cobra.CheckErr(viper.ReadInConfig()) |
| 57 | + |
| 58 | + for key, value := range config { |
| 59 | + viper.Set(key, value) |
| 60 | + } |
| 61 | + |
| 62 | + // Allow user to override with custom config file if specified |
| 63 | + if cfgFile != "" && cfgFile != defaultConfig { |
| 64 | + if _, err := os.Stat(cfgFile); err == nil { |
| 65 | + viper.SetConfigFile(cfgFile) |
| 66 | + // Don't exit on error, just use embedded defaults |
| 67 | + _ = viper.MergeInConfig() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + viper.SetEnvPrefix("MAJOR") |
| 72 | + viper.AutomaticEnv() |
52 | 73 | } |
0 commit comments