Skip to content

Commit

Permalink
Merge pull request #59 from yjinjo/v0
Browse files Browse the repository at this point in the history
Add notification when a user didn't set up the config
  • Loading branch information
yjinjo authored Nov 29, 2024
2 parents fff1ffb + 696d023 commit bc710c9
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,36 @@ func loadConfig() (*Config, error) {
configFile := filepath.Join(home, ".cfctl", "config.yaml")
cacheConfigFile := filepath.Join(home, ".cfctl", "cache", "config.yaml")

var currentEnv string
var endpoint string
var token string

// Try to read main config first
mainV := viper.New()
mainV.SetConfigFile(configFile)
mainConfigErr := mainV.ReadInConfig()

if mainConfigErr == nil {
// Main config exists, try to get environment
currentEnv = mainV.GetString("environment")
if currentEnv != "" {
envConfig := mainV.Sub(fmt.Sprintf("environments.%s", currentEnv))
if envConfig != nil {
endpoint = envConfig.GetString("endpoint")
token = envConfig.GetString("token")
}
if mainConfigErr != nil {
pterm.Error.Printf("No valid configuration found.\n")
pterm.Info.Println("Please run 'cfctl config init' to set up your configuration.")
pterm.Info.Println("After initialization, run 'cfctl login' to authenticate.")
return nil, fmt.Errorf("failed to read config file")
}

var currentEnv string
var endpoint string
var token string

// Main config exists, try to get environment
currentEnv = mainV.GetString("environment")
if currentEnv != "" {
envConfig := mainV.Sub(fmt.Sprintf("environments.%s", currentEnv))
if envConfig != nil {
endpoint = envConfig.GetString("endpoint")
token = envConfig.GetString("token")
}
}

// If main config doesn't have what we need, try cache config
if endpoint == "" || token == "" {
cacheV := viper.New()

cacheV.SetConfigFile(cacheConfigFile)
if err := cacheV.ReadInConfig(); err == nil {
// If no current environment set, try to get it from cache config
Expand Down

0 comments on commit bc710c9

Please sign in to comment.