Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
cli
major
35 changes: 28 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cmd

import (
"encoding/json"
"os"

"github.com/major-technology/cli/configs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -39,14 +41,33 @@ func init() {
}

func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
// Use embedded config based on defaultConfig variable
var configData []byte
if defaultConfig == "configs/prod.json" {
configData = configs.ProdConfig
} else {
home, err := os.UserHomeDir()
configData = configs.LocalConfig
}

// Parse embedded config into viper
var config map[string]interface{}
if err := json.Unmarshal(configData, &config); err != nil {
cobra.CheckErr(err)
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".cli")
}
cobra.CheckErr(viper.ReadInConfig())

for key, value := range config {
viper.Set(key, value)
}

// Allow user to override with custom config file if specified
if cfgFile != "" && cfgFile != defaultConfig {
if _, err := os.Stat(cfgFile); err == nil {
viper.SetConfigFile(cfgFile)
// Don't exit on error, just use embedded defaults
_ = viper.MergeInConfig()
}
}

viper.SetEnvPrefix("MAJOR")
viper.AutomaticEnv()
}
11 changes: 11 additions & 0 deletions configs/configs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package configs

import (
_ "embed"
)

//go:embed prod.json
var ProdConfig []byte

//go:embed local.json
var LocalConfig []byte