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 [config] init, environment, show, and fzf feature #11

Merged
merged 1 commit into from
Nov 6, 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
60 changes: 52 additions & 8 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"path/filepath"
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand All @@ -35,6 +36,10 @@ var initCmd = &cobra.Command{
environment, _ := cmd.Flags().GetString("environment")
importFile, _ := cmd.Flags().GetString("import-file")

if environment == "" {
log.Fatalf("Environment name must be provided")
}

if importFile != "" {
viper.SetConfigFile(importFile)
err := viper.ReadInConfig()
Expand All @@ -59,6 +64,9 @@ var envCmd = &cobra.Command{
Use: "environment",
Short: "Manage and switch environments",
Run: func(cmd *cobra.Command, args []string) {
// Update the global config file with the current list of environments
updateGlobalConfig()

switchEnv, _ := cmd.Flags().GetString("switch")
if switchEnv != "" {
configPath := filepath.Join(getConfigDir(), "environments", switchEnv+".yml")
Expand Down Expand Up @@ -92,13 +100,14 @@ var envCmd = &cobra.Command{
log.Fatalf("Unable to list environments: %v", err)
}

pterm.Println("Available Environments:\n")
for _, entry := range entries {
name := entry.Name()
name = name[:len(name)-len(filepath.Ext(name))] // Remove ".yaml" extension
name = name[:len(name)-len(filepath.Ext(name))] // Remove ".yml" extension
if name == currentEnv {
pterm.Info.Println(name + " (current)")
pterm.FgGreen.Printf(" > %s (current)\n", name)
} else {
pterm.Println(name)
pterm.Printf(" %s\n", name)
}
}
},
Expand All @@ -119,10 +128,10 @@ var showCmd = &cobra.Command{
log.Fatalf("Error formatting output as JSON: %v", err)
}
fmt.Println(string(data))
case "yaml":
case "yml":
data, err := yaml.Marshal(configData)
if err != nil {
log.Fatalf("Error formatting output as YAML: %v", err)
log.Fatalf("Error formatting output as yml: %v", err)
}
fmt.Println(string(data))
default:
Expand All @@ -131,6 +140,37 @@ var showCmd = &cobra.Command{
},
}

// updateGlobalConfig updates the ~/.spaceone/config file with all available environments
func updateGlobalConfig() {
envDir := filepath.Join(getConfigDir(), "environments")
entries, err := os.ReadDir(envDir)
if err != nil {
log.Fatalf("Unable to list environments: %v", err)
}

configPath := filepath.Join(getConfigDir(), "config")
file, err := os.Create(configPath)
if err != nil {
log.Fatalf("Failed to open config file: %v", err)
}
defer file.Close()

var configContent strings.Builder
for _, entry := range entries {
name := entry.Name()
name = name[:len(name)-len(filepath.Ext(name))] // Remove ".yml" extension
configContent.WriteString(fmt.Sprintf("[%s]\n", name))
configContent.WriteString(fmt.Sprintf("cfctl environments -s %s\n\n", name))
}

_, err = file.WriteString(configContent.String())
if err != nil {
log.Fatalf("Failed to write to config file: %v", err)
}

pterm.Info.Println("Updated global config file with available environments.")
}

func init() {
rootCmd.AddCommand(configCmd)

Expand All @@ -139,15 +179,19 @@ func init() {
configCmd.AddCommand(envCmd)
configCmd.AddCommand(showCmd)

// Defining flags
// Defining flags for initCmd
initCmd.Flags().StringP("environment", "e", "", "Name of the environment (required)")
initCmd.Flags().StringP("import-file", "f", "", "Path to an import configuration file")
initCmd.MarkFlagRequired("environment")

// Defining flags for envCmd
envCmd.Flags().StringP("switch", "s", "", "Switch to a different environment")
showCmd.Flags().StringP("output", "o", "yaml", "Output format (yaml/json)")
envCmd.Flags().StringP("remove", "r", "", "Remove an environment")

// Defining flags for showCmd
showCmd.Flags().StringP("output", "o", "yml", "Output format (yml/json)")

viper.SetConfigType("yaml")
viper.SetConfigType("yml")
}

// getConfigDir returns the directory for SpaceONE configuration
Expand Down
Loading