Skip to content

Commit 8ab05c8

Browse files
authored
Merge pull request #11 from major-technology/1
Fix config missing
2 parents 57029ad + 227d1e2 commit 8ab05c8

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
22
cli
3+
major

cmd/root.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"os"
56

7+
"github.com/major-technology/cli/configs"
68
"github.com/spf13/cobra"
79
"github.com/spf13/viper"
810
)
@@ -39,14 +41,33 @@ func init() {
3941
}
4042

4143
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
4448
} 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 {
4655
cobra.CheckErr(err)
47-
viper.AddConfigPath(home)
48-
viper.SetConfigType("yaml")
49-
viper.SetConfigName(".cli")
5056
}
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()
5273
}

configs/configs.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package configs
2+
3+
import (
4+
_ "embed"
5+
)
6+
7+
//go:embed prod.json
8+
var ProdConfig []byte
9+
10+
//go:embed local.json
11+
var LocalConfig []byte

0 commit comments

Comments
 (0)