Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notification when a user didn't set up the config #59

Merged
merged 1 commit into from
Nov 29, 2024
Merged
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
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
Loading