From 06fbaefd29a749a45cfeb78c892997a307e1d139 Mon Sep 17 00:00:00 2001 From: Youngjin Jo <youngjinjo@megazone.com> Date: Wed, 11 Dec 2024 18:12:00 +0900 Subject: [PATCH 1/2] feat: add plugin subcommand and remove proxy field when local mode Signed-off-by: Youngjin Jo <youngjinjo@megazone.com> --- cmd/other/setting.go | 53 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/cmd/other/setting.go b/cmd/other/setting.go index 893cd4b..80fb7e3 100644 --- a/cmd/other/setting.go +++ b/cmd/other/setting.go @@ -60,7 +60,7 @@ var settingInitURLCmd = &cobra.Command{ return } if !appFlag && !userFlag { - pterm.Error.Println("You must specify either --app or --user flag.") + pterm.Error.Println("You must specify either --app, --user, or --plugin flag.") cmd.Help() return } @@ -130,14 +130,21 @@ var settingInitLocalCmd = &cobra.Command{ userFlag, _ := cmd.Flags().GetBool("user") devFlag, _ := cmd.Flags().GetBool("dev") stgFlag, _ := cmd.Flags().GetBool("stg") + pluginFlag, _ := cmd.Flags().GetBool("plugin") if localEnv == "" { pterm.Error.Println("The --name flag is required.") cmd.Help() return } + // Plugin flag takes precedence over other flags + if pluginFlag { + // For plugin, we don't need other flags + initializePluginSetting(localEnv) + return + } if !appFlag && !userFlag { - pterm.Error.Println("You must specify either --app or --user flag.") + pterm.Error.Println("You must specify either --app, --user, or --plugin flag.") cmd.Help() return } @@ -208,6 +215,44 @@ var settingInitLocalCmd = &cobra.Command{ }, } +func initializePluginSetting(pluginName string) { + settingDir := GetSettingDir() + if err := os.MkdirAll(settingDir, 0755); err != nil { + pterm.Error.Printf("Failed to create setting directory: %v\n", err) + return + } + + mainSettingPath := filepath.Join(settingDir, "setting.toml") + if _, err := os.Stat(mainSettingPath); os.IsNotExist(err) { + initialSetting := []byte("environments = {}\n") + if err := os.WriteFile(mainSettingPath, initialSetting, 0644); err != nil { + pterm.Error.Printf("Failed to create setting file: %v\n", err) + return + } + } + + v := viper.New() + v.SetConfigFile(mainSettingPath) + v.SetConfigType("toml") + + if err := v.ReadInConfig(); err != nil && !os.IsNotExist(err) { + pterm.Error.Printf("Error reading setting: %v\n", err) + return + } + + // Set environment configuration + v.Set(fmt.Sprintf("environments.%s.endpoint", pluginName), "grpc://localhost:50051") + v.Set(fmt.Sprintf("environments.%s.token", pluginName), "NO TOKEN") + v.Set("environment", pluginName) + + if err := v.WriteConfig(); err != nil { + pterm.Error.Printf("Failed to write setting: %v\n", err) + return + } + + pterm.Success.Printf("Plugin environment '%s' successfully initialized.\n", pluginName) +} + func updateLocalSetting(envName, settingType, settingPath string) { v := viper.New() v.SetConfigFile(settingPath) @@ -228,9 +273,6 @@ func updateLocalSetting(envName, settingType, settingPath string) { v.Set(fmt.Sprintf("environments.%s.endpoint", envName), "grpc://localhost:50051") if settingType == "app" { v.Set(fmt.Sprintf("environments.%s.token", envName), "") - v.Set(fmt.Sprintf("environments.%s.proxy", envName), true) - } else if settingType == "user" { - v.Set(fmt.Sprintf("environments.%s.proxy", envName), false) } // Write configuration @@ -1163,6 +1205,7 @@ func init() { settingInitLocalCmd.Flags().Bool("user", false, "Initialize as user-specific configuration") settingInitLocalCmd.Flags().Bool("dev", false, "Initialize as development environment") settingInitLocalCmd.Flags().Bool("stg", false, "Initialize as staging environment") + settingInitLocalCmd.Flags().Bool("plugin", false, "Initialize as plugin configuration") envCmd.Flags().StringP("switch", "s", "", "Switch to a different environment") envCmd.Flags().StringP("remove", "r", "", "Remove an environment") From e6811baca0cb54e58d4a55eeb6a3d8be4e9e6f80 Mon Sep 17 00:00:00 2001 From: Youngjin Jo <youngjinjo@megazone.com> Date: Wed, 11 Dec 2024 18:43:30 +0900 Subject: [PATCH 2/2] refactor: set 'NO TOKEN' for local env Signed-off-by: Youngjin Jo <youngjinjo@megazone.com> --- cmd/other/setting.go | 102 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 81 insertions(+), 21 deletions(-) diff --git a/cmd/other/setting.go b/cmd/other/setting.go index 80fb7e3..bf1f494 100644 --- a/cmd/other/setting.go +++ b/cmd/other/setting.go @@ -80,12 +80,36 @@ var settingInitURLCmd = &cobra.Command{ // Initialize setting.toml if it doesn't exist mainSettingPath := filepath.Join(settingDir, "setting.toml") - if _, err := os.Stat(mainSettingPath); os.IsNotExist(err) { - // Initial TOML structure - initialSetting := []byte("environments = {}\n") - if err := os.WriteFile(mainSettingPath, initialSetting, 0644); err != nil { - pterm.Error.Printf("Failed to create setting file: %v\n", err) - return + + // Check if environment already exists + v := viper.New() + v.SetConfigFile(mainSettingPath) + v.SetConfigType("toml") + + if err := v.ReadInConfig(); err == nil { + // File exists and can be read + envSuffix := map[bool]string{true: "app", false: "user"}[appFlag] + fullEnvName := fmt.Sprintf("%s-%s", envName, envSuffix) + + if envConfig := v.GetStringMap(fmt.Sprintf("environments.%s", fullEnvName)); envConfig != nil { + // Environment exists, ask for confirmation + confirmBox := pterm.DefaultBox.WithTitle("Environment Already Exists"). + WithTitleTopCenter(). + WithRightPadding(4). + WithLeftPadding(4). + WithBoxStyle(pterm.NewStyle(pterm.FgYellow)) + + confirmBox.Println(fmt.Sprintf("Environment '%s' already exists.\nDo you want to overwrite it?", fullEnvName)) + + fmt.Print("\nEnter (y/N): ") + var response string + fmt.Scanln(&response) + response = strings.ToLower(strings.TrimSpace(response)) + + if response != "y" { + pterm.Info.Printf("Operation cancelled. Environment '%s' remains unchanged.\n", fullEnvName) + return + } } } @@ -123,7 +147,9 @@ var settingInitLocalCmd = &cobra.Command{ Args: cobra.NoArgs, Example: ` cfctl setting init local -n [domain] --app --dev or - cfctl setting init local -n [domain] --user --stg`, + cfctl setting init local -n [domain] --user --stg, + or + cfctl setting init local -n [plugin_name] --plugin`, Run: func(cmd *cobra.Command, args []string) { localEnv, _ := cmd.Flags().GetString("name") appFlag, _ := cmd.Flags().GetBool("app") @@ -132,29 +158,60 @@ var settingInitLocalCmd = &cobra.Command{ stgFlag, _ := cmd.Flags().GetBool("stg") pluginFlag, _ := cmd.Flags().GetBool("plugin") + // Step 1: Check name flag if localEnv == "" { pterm.Error.Println("The --name flag is required.") + // Show only name flag in help + cmd.SetUsageFunc(func(cmd *cobra.Command) error { + fmt.Printf("\nUsage:\n cfctl setting init local [flags]\n\n") + fmt.Printf("Flags:\n") + fmt.Printf(" -n, --name string Local environment name for the environment\n") + fmt.Printf(" -h, --help help for local\n") + return nil + }) cmd.Help() return } - // Plugin flag takes precedence over other flags - if pluginFlag { - // For plugin, we don't need other flags - initializePluginSetting(localEnv) - return - } - if !appFlag && !userFlag { + + // Step 2: Check type flags (app/user/plugin) + if !appFlag && !userFlag && !pluginFlag { pterm.Error.Println("You must specify either --app, --user, or --plugin flag.") + // Show only type flags in help + cmd.SetUsageFunc(func(cmd *cobra.Command) error { + fmt.Printf("\nUsage:\n cfctl setting init local --name %s [flags]\n\n", localEnv) + fmt.Printf("Flags:\n") + fmt.Printf(" --app Initialize as application configuration\n") + fmt.Printf(" --user Initialize as user-specific configuration\n") + fmt.Printf(" --plugin Initialize as plugin configuration\n") + return nil + }) cmd.Help() return } - if !devFlag && !stgFlag { + + // Step 3: For app/user configs, check environment type + if (appFlag || userFlag) && !devFlag && !stgFlag { pterm.Error.Println("You must specify either --dev or --stg flag.") + // Show only environment flags in help + cmd.SetUsageFunc(func(cmd *cobra.Command) error { + fmt.Printf("\nUsage:\n cfctl setting init local --name %s --%s [flags]\n\n", + localEnv, map[bool]string{true: "app", false: "user"}[appFlag]) + fmt.Printf("Flags:\n") + fmt.Printf(" --dev Initialize as development environment\n") + fmt.Printf(" --stg Initialize as staging environment\n") + return nil + }) cmd.Help() return } - // Create setting directory if it doesn't exist + // Plugin flag takes precedence + if pluginFlag { + initializePluginSetting(localEnv) + return + } + + // Rest of the existing implementation... settingDir := GetSettingDir() if err := os.MkdirAll(settingDir, 0755); err != nil { pterm.Error.Printf("Failed to create setting directory: %v\n", err) @@ -216,6 +273,9 @@ var settingInitLocalCmd = &cobra.Command{ } func initializePluginSetting(pluginName string) { + // Add 'local-' prefix to plugin name + envName := fmt.Sprintf("local-%s", pluginName) + settingDir := GetSettingDir() if err := os.MkdirAll(settingDir, 0755); err != nil { pterm.Error.Printf("Failed to create setting directory: %v\n", err) @@ -240,17 +300,17 @@ func initializePluginSetting(pluginName string) { return } - // Set environment configuration - v.Set(fmt.Sprintf("environments.%s.endpoint", pluginName), "grpc://localhost:50051") - v.Set(fmt.Sprintf("environments.%s.token", pluginName), "NO TOKEN") - v.Set("environment", pluginName) + // Set environment configuration using the prefixed name + v.Set(fmt.Sprintf("environments.%s.endpoint", envName), "grpc://localhost:50051") + v.Set(fmt.Sprintf("environments.%s.token", envName), "NO TOKEN") + v.Set("environment", envName) if err := v.WriteConfig(); err != nil { pterm.Error.Printf("Failed to write setting: %v\n", err) return } - pterm.Success.Printf("Plugin environment '%s' successfully initialized.\n", pluginName) + pterm.Success.Printf("Plugin environment '%s' successfully initialized.\n", envName) } func updateLocalSetting(envName, settingType, settingPath string) {