From 92ca9ac773395c10bf194971d4f98e9024f8cc86 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Mon, 18 Nov 2024 21:03:54 +0900 Subject: [PATCH] refactor: change directory from ~/.spaceone to ~/.cfctl Signed-off-by: Youngjin Jo --- cmd/ai.go | 4 ++-- cmd/apiResources.go | 4 ++-- cmd/config.go | 42 +++++++++++++++--------------------------- cmd/exec.go | 2 +- cmd/login.go | 6 +++--- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/cmd/ai.go b/cmd/ai.go index 1e7f192..4dc8192 100644 --- a/cmd/ai.go +++ b/cmd/ai.go @@ -18,8 +18,8 @@ import ( var ( apiToken string - configPath = filepath.Join(os.Getenv("HOME"), ".spaceone", "config") - resourceDir = filepath.Join(os.Getenv("HOME"), ".spaceone", "training_data") // 학습 전용 디렉터리 경로 + configPath = filepath.Join(os.Getenv("HOME"), ".cfctl", "config") + resourceDir = filepath.Join(os.Getenv("HOME"), ".cfctl", "training_data") // 학습 전용 디렉터리 경로 ) // aiCmd represents the ai command diff --git a/cmd/apiResources.go b/cmd/apiResources.go index e9f0e0c..aeb0d43 100644 --- a/cmd/apiResources.go +++ b/cmd/apiResources.go @@ -42,7 +42,7 @@ var apiResourcesCmd = &cobra.Command{ log.Fatalf("Unable to find home directory: %v", err) } - configFile := filepath.Join(home, ".spaceone", "config.yaml") + configFile := filepath.Join(home, ".cfctl", "config.yaml") viper.SetConfigFile(configFile) if err := viper.ReadInConfig(); err != nil { log.Fatalf("Error reading config file: %v", err) @@ -75,7 +75,7 @@ var apiResourcesCmd = &cobra.Command{ } // Load short names configuration - shortNamesFile := filepath.Join(home, ".spaceone", "short_names.yaml") + shortNamesFile := filepath.Join(home, ".cfctl", "short_names.yaml") shortNamesMap := make(map[string]string) if _, err := os.Stat(shortNamesFile); err == nil { file, err := os.Open(shortNamesFile) diff --git a/cmd/config.go b/cmd/config.go index 0cee04b..af80c8c 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -40,7 +40,6 @@ var configInitCmd = &cobra.Command{ Short: "Initialize a new environment configuration", Long: `Initialize a new environment configuration for cfctl by specifying a URL with -u or a local environment name with -l.`, Run: func(cmd *cobra.Command, args []string) { - // Retrieve flags environment, _ := cmd.Flags().GetString("environment") urlStr, _ := cmd.Flags().GetString("url") localEnv, _ := cmd.Flags().GetString("local") @@ -67,25 +66,14 @@ var configInitCmd = &cobra.Command{ envName = environment } - // Ensure environments directory exists - configDir := filepath.Join(getConfigDir(), "environments") + // Ensure ~/.cfctl directory exists + configDir := getConfigDir() if err := os.MkdirAll(configDir, 0755); err != nil { - pterm.Error.WithShowLineNumber(false).Println("Failed to create environments directory:", err) + pterm.Error.WithShowLineNumber(false).Println("Failed to create config directory:", err) return } - envFilePath := filepath.Join(configDir, envName+".yaml") - // Create an empty environment file if it doesn't already exist - if _, err := os.Stat(envFilePath); os.IsNotExist(err) { - file, err := os.Create(envFilePath) - if err != nil { - pterm.Error.WithShowLineNumber(false).Println("Failed to create environment file:", err) - return - } - file.Close() - } - - // Set configuration in config.yaml + // Set configuration directly in config.yaml configPath := filepath.Join(getConfigDir(), "config.yaml") viper.SetConfigFile(configPath) _ = viper.ReadInConfig() @@ -101,8 +89,8 @@ var configInitCmd = &cobra.Command{ if baseURL != "" { viper.Set(fmt.Sprintf("environments.%s.endpoint", envName), baseURL) } - viper.Set(fmt.Sprintf("environments.%s.token", envName), "") // Add token as an empty value - viper.Set(fmt.Sprintf("environments.%s.proxy", envName), true) // Set proxy to true + viper.Set(fmt.Sprintf("environments.%s.token", envName), "") + viper.Set(fmt.Sprintf("environments.%s.proxy", envName), true) // Set the current environment viper.Set("environment", envName) @@ -114,7 +102,7 @@ var configInitCmd = &cobra.Command{ } pterm.Success.WithShowLineNumber(false). - Printfln("Environment '%s' successfully initialized with configuration in '%s/config.yaml'", envName, getConfigDir()) + Printfln("Environment '%s' successfully initialized in '%s/config.yaml'", envName, getConfigDir()) }, } @@ -135,7 +123,7 @@ var envCmd = &cobra.Command{ log.Fatalf("Environment '%s' not found.", switchEnv) } - // Update the environment in ~/.spaceone/config.yaml + // Update the environment in ~/.cfctl/config.yaml configFilePath := filepath.Join(getConfigDir(), "config.yaml") viper.SetConfigFile(configFilePath) @@ -246,10 +234,10 @@ var showCmd = &cobra.Command{ Use: "show", Short: "Display the current cfctl configuration", Run: func(cmd *cobra.Command, args []string) { - // Load the current environment from ~/.spaceone/config.yaml + // Load the current environment from ~/.cfctl/config.yaml currentEnv := getCurrentEnvironment() if currentEnv == "" { - log.Fatal("No environment set in ~/.spaceone/config.yaml") + log.Fatal("No environment set in ~/.cfctl/config.yaml") } // Construct the path to the environment's YAML file @@ -258,7 +246,7 @@ var showCmd = &cobra.Command{ // Check if the environment file exists if _, err := os.Stat(envFilePath); os.IsNotExist(err) { - log.Fatalf("Environment file '%s.yaml' does not exist in ~/.spaceone/environments", currentEnv) + log.Fatalf("Environment file '%s.yaml' does not exist in ~/.cfctl/environments", currentEnv) } // Load and display the configuration from the environment YAML file @@ -379,11 +367,11 @@ Available Services: }, } -// syncCmd syncs the environments in ~/.spaceone/environments with ~/.spaceone/config.yaml +// syncCmd syncs the environments in ~/.cfctl/environments with ~/.cfctl/config.yaml var syncCmd = &cobra.Command{ Use: "sync", Short: "Sync environments from the environments directory to config.yaml", - Long: "Sync all environment files from the ~/.spaceone/environments directory to ~/.spaceone/config.yaml", + Long: "Sync all environment files from the ~/.cfctl/environments directory to ~/.cfctl/config.yaml", Run: func(cmd *cobra.Command, args []string) { // Define paths envDir := filepath.Join(getConfigDir(), "environments") @@ -425,10 +413,10 @@ func getConfigDir() string { if err != nil { log.Fatalf("Unable to find home directory: %v", err) } - return filepath.Join(home, ".spaceone") + return filepath.Join(home, ".cfctl") } -// getCurrentEnvironment reads the current environment from ~/.spaceone/config.yaml +// getCurrentEnvironment reads the current environment from ~/.cfctl/config.yaml func getCurrentEnvironment() string { // Set config file path to ~/.spaceone/config.yaml configPath := filepath.Join(getConfigDir(), "config.yaml") diff --git a/cmd/exec.go b/cmd/exec.go index ca9a2e2..c13e328 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -65,7 +65,7 @@ func init() { } func loadConfig() (*Config, error) { - configPath := fmt.Sprintf("%s/.spaceone/config.yaml", os.Getenv("HOME")) + configPath := fmt.Sprintf("%s/.cfctl/config.yaml", os.Getenv("HOME")) data, err := os.ReadFile(configPath) if err != nil { return nil, fmt.Errorf("could not read config file: %w", err) diff --git a/cmd/login.go b/cmd/login.go index 0c02eae..32af124 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -128,7 +128,7 @@ func loadEnvironmentConfig() { } // Load the main environment file to get the current environment - viper.SetConfigFile(filepath.Join(homeDir, ".spaceone", "config.yaml")) + viper.SetConfigFile(filepath.Join(homeDir, ".cfctl", "config.yaml")) if err := viper.ReadInConfig(); err != nil { pterm.Error.Println("Failed to read config.yaml:", err) exitWithError() @@ -433,7 +433,7 @@ func saveToken(newToken string) { } // Load the main environment file to get the current environment - viper.SetConfigFile(filepath.Join(homeDir, ".spaceone", "config.yaml")) + viper.SetConfigFile(filepath.Join(homeDir, ".cfctl", "config.yaml")) if err := viper.ReadInConfig(); err != nil { pterm.Error.Println("Failed to read environment file:", err) exitWithError() @@ -445,7 +445,7 @@ func saveToken(newToken string) { } // Path to the environment-specific file - envFilePath := filepath.Join(homeDir, ".spaceone", "environments", currentEnvironment+".yaml") + envFilePath := filepath.Join(homeDir, ".cfctl", "environments", currentEnvironment+".yaml") // Read the file line by line, replacing or adding the token line if needed file, err := os.Open(envFilePath)