From 696d023a738b094a6dfbe84f9ffb667c35af13b4 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Fri, 29 Nov 2024 09:26:37 +0900 Subject: [PATCH] minor: add notififcation when user didn't set up config Signed-off-by: Youngjin Jo --- cmd/root.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 58fb1a0..20d5fb7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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