Skip to content

Commit

Permalink
refactor: change directory from ~/.spaceone to ~/.cfctl
Browse files Browse the repository at this point in the history
Signed-off-by: Youngjin Jo <[email protected]>
  • Loading branch information
yjinjo committed Nov 18, 2024
1 parent 2d191bb commit 92ca9ac
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 35 deletions.
4 changes: 2 additions & 2 deletions cmd/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/apiResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 15 additions & 27 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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())
},
}

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 92ca9ac

Please sign in to comment.