Skip to content

Commit

Permalink
feat: add local config
Browse files Browse the repository at this point in the history
Signed-off-by: Youngjin Jo <[email protected]>
  • Loading branch information
yjinjo committed Nov 27, 2024
1 parent f381cda commit 7aed298
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions cmd/other/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var configInitLocalCmd = &cobra.Command{
Long: `Specify a local environment name to initialize the configuration.`,
Args: cobra.NoArgs,
Example: ` cfctl config init local -n local-cloudone --app
or
or
cfctl config init local -n local-cloudone --user`,
Run: func(cmd *cobra.Command, args []string) {
localEnv, _ := cmd.Flags().GetString("name")
Expand All @@ -147,17 +147,81 @@ var configInitLocalCmd = &cobra.Command{
return
}

// Create config directory if it doesn't exist
configDir := GetConfigDir()
if err := os.MkdirAll(configDir, 0755); err != nil {
pterm.Error.Printf("Failed to create config directory: %v\n", err)
return
}

// Initialize config.yaml if it doesn't exist
mainConfigPath := filepath.Join(configDir, "config.yaml")
if _, err := os.Stat(mainConfigPath); os.IsNotExist(err) {
initialConfig := []byte("environments:\n")
if err := os.WriteFile(mainConfigPath, initialConfig, 0644); err != nil {
pterm.Error.Printf("Failed to create config file: %v\n", err)
return
}
}

var envName string
if appFlag {
envName = fmt.Sprintf("%s-app", localEnv)
updateConfig(envName, "", "app")
updateLocalConfig(envName, "app", mainConfigPath)
} else {
envName = fmt.Sprintf("%s-user", localEnv)
updateConfig(envName, "", "user")
updateLocalConfig(envName, "user", filepath.Join(configDir, "cache", "config.yaml"))
}

// Update the current environment in the main config
mainV := viper.New()
mainV.SetConfigFile(mainConfigPath)

// Read the config file
if err := mainV.ReadInConfig(); err != nil {
pterm.Error.Printf("Failed to read config file: %v\n", err)
return
}

// Set the new environment as current
mainV.Set("environment", envName)

if err := mainV.WriteConfig(); err != nil {
pterm.Error.Printf("Failed to update current environment: %v\n", err)
return
}

pterm.Success.Printf("Switched to '%s' environment.\n", envName)
},
}

func updateLocalConfig(envName, configType, configPath string) {
v := viper.New()
v.SetConfigFile(configPath)

// Ensure directory exists
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
pterm.Error.Printf("Failed to create directory: %v\n", err)
return
}

// Read existing config or create new
if err := v.ReadInConfig(); err != nil && !os.IsNotExist(err) {
pterm.Error.Printf("Error reading config: %v\n", err)
return
}

// Set environment configuration
v.Set(fmt.Sprintf("environments.%s.endpoint", envName), "grpc://localhost:50051")
v.Set(fmt.Sprintf("environments.%s.token", envName), "")

// Write configuration
if err := v.WriteConfig(); err != nil {
pterm.Error.Printf("Failed to write config: %v\n", err)
return
}
}

// envCmd manages environment switching and listing
var envCmd = &cobra.Command{
Use: "environment",
Expand Down

0 comments on commit 7aed298

Please sign in to comment.