From 1a859d3548a446cf57a2dea32bc229585c403897 Mon Sep 17 00:00:00 2001 From: Amin Al Ali Al Darwish Date: Thu, 9 May 2024 15:20:40 +0400 Subject: [PATCH 1/5] Added Import functionality to variables command --- cmd/variable/action/import.go | 134 ++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 cmd/variable/action/import.go diff --git a/cmd/variable/action/import.go b/cmd/variable/action/import.go new file mode 100644 index 0000000..25dda48 --- /dev/null +++ b/cmd/variable/action/import.go @@ -0,0 +1,134 @@ +package action + +import ( + "errors" + "fmt" + + "bunnyshell.com/cli/pkg/api/variable" + "bunnyshell.com/cli/pkg/config" + "bunnyshell.com/cli/pkg/config/enum" + "bunnyshell.com/cli/pkg/lib" + "bunnyshell.com/cli/pkg/util" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +type BulkImport struct { + Vars map[string]string `json:"vars" yaml:"vars"` + Secrets map[string]string `json:"secrets" yaml:"secrets"` +} + +func init() { + varFile := "" + secretFile := "" + options := config.GetOptions() + data := BulkImport{ + Vars: make(map[string]string), + Secrets: make(map[string]string), + } + + command := &cobra.Command{ + Use: "import", + + ValidArgsFunction: cobra.NoFileCompletions, + + PreRunE: func(cmd *cobra.Command, args []string) error { + //todo add this to preRunE + if varFile == "" && secretFile == "" { + return errors.New("must provide a either a var or secret file") + } + + if varFile != "" { + if err := readFile(varFile, &data.Vars); err != nil { + return err + } + } + + if secretFile != "" { + if err := readFile(secretFile, &data.Secrets); err != nil { + return err + } + } + + return nil + }, + + RunE: func(cmd *cobra.Command, args []string) error { + settings := config.GetSettings() + + if len(data.Vars) > 0 { + for key, value := range data.Vars { + createOptions := variable.NewCreateOptions() + createOptions.Environment = settings.Profile.Context.Environment + createOptions.Name = key + createOptions.Value = value + + model, err := variable.Create(createOptions) + if err != nil { + // Log the error or notify that the particular variable couldn't be created + fmt.Printf("Error creating variable %s: %v\n", key, err) + } else { + // Successfully created model, output the results immediately + lib.FormatCommandData(cmd, model) + } + } + } + + if len(data.Secrets) > 0 { + for key, value := range data.Secrets { + createOptions := variable.NewCreateOptions() + createOptions.Environment = settings.Profile.Context.Environment + createOptions.Name = key + createOptions.Value = value + createOptions.IsSecret = enum.BoolTrue + model, err := variable.Create(createOptions) + if err != nil { + // Log the error or notify that the particular variable couldn't be created + fmt.Printf("Error creating variable %s: %v\n", key, err) + } else { + // Successfully created model, output the results immediately + lib.FormatCommandData(cmd, model) + } + } + } + + return nil + }, + } + + flags := command.Flags() + + flags.StringVar(&varFile, "vars-file", varFile, "File to import variables from") + flags.StringVar(&secretFile, "secrets-file", secretFile, "File to import secrets from") + + flags.AddFlag(options.Environment.AddFlagWithExtraHelp( + "environment", + "Environment for the variable", + "Environments contain multiple variables", + util.FlagRequired, + )) + + // createOptions.UpdateFlagSet(flags) + + mainCmd.AddCommand(command) +} + +func readFile(fileName string, data *map[string]string) error { + viper := viper.New() + viper.SetConfigFile(fileName) + viper.SetConfigType("env") + + if err := viper.ReadInConfig(); err != nil { + // @review update go:1.20 errors.join + // return fmt.Errorf("%w: %s", ErrConfigLoad, err.Error()) + return err + } + + if err := viper.Unmarshal(&data); err != nil { + // @review update go:1.20 errors.join + // return fmt.Errorf("%w: %s", ErrConfigLoad, err.Error()) + return err + } + + return nil +} From 0f88508cb41c9cd38852103b6ac01d8b8864ed27 Mon Sep 17 00:00:00 2001 From: Amin Al Ali Al Darwish Date: Mon, 20 May 2024 23:44:06 +0300 Subject: [PATCH 2/5] created createEnvVar Function and added ignoreDuplicate flag to skip over variables already defined --- cmd/variable/action/import.go | 61 ++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/cmd/variable/action/import.go b/cmd/variable/action/import.go index 25dda48..ba92d3b 100644 --- a/cmd/variable/action/import.go +++ b/cmd/variable/action/import.go @@ -2,7 +2,6 @@ package action import ( "errors" - "fmt" "bunnyshell.com/cli/pkg/api/variable" "bunnyshell.com/cli/pkg/config" @@ -21,6 +20,7 @@ type BulkImport struct { func init() { varFile := "" secretFile := "" + ignoreDuplicates := false options := config.GetOptions() data := BulkImport{ Vars: make(map[string]string), @@ -54,40 +54,29 @@ func init() { }, RunE: func(cmd *cobra.Command, args []string) error { - settings := config.GetSettings() if len(data.Vars) > 0 { for key, value := range data.Vars { - createOptions := variable.NewCreateOptions() - createOptions.Environment = settings.Profile.Context.Environment - createOptions.Name = key - createOptions.Value = value - - model, err := variable.Create(createOptions) + err := createEnvVar(cmd, key, value, false) if err != nil { - // Log the error or notify that the particular variable couldn't be created - fmt.Printf("Error creating variable %s: %v\n", key, err) - } else { - // Successfully created model, output the results immediately - lib.FormatCommandData(cmd, model) + if ignoreDuplicates && err.Error() == "An Environment Variable with this name already exists in this environment." { + continue + } else { + return lib.FormatCommandError(cmd, err) + } } } } if len(data.Secrets) > 0 { for key, value := range data.Secrets { - createOptions := variable.NewCreateOptions() - createOptions.Environment = settings.Profile.Context.Environment - createOptions.Name = key - createOptions.Value = value - createOptions.IsSecret = enum.BoolTrue - model, err := variable.Create(createOptions) + err := createEnvVar(cmd, key, value, true) if err != nil { - // Log the error or notify that the particular variable couldn't be created - fmt.Printf("Error creating variable %s: %v\n", key, err) - } else { - // Successfully created model, output the results immediately - lib.FormatCommandData(cmd, model) + if ignoreDuplicates && err.Error() == "An Environment Variable with this name already exists in this environment." { + continue + } else { + return lib.FormatCommandError(cmd, err) + } } } } @@ -100,6 +89,7 @@ func init() { flags.StringVar(&varFile, "vars-file", varFile, "File to import variables from") flags.StringVar(&secretFile, "secrets-file", secretFile, "File to import secrets from") + flags.BoolVarP(&ignoreDuplicates, "ignore-duplicates", "ign-dp", false, "Skip variables that already exist in the environment") flags.AddFlag(options.Environment.AddFlagWithExtraHelp( "environment", @@ -108,8 +98,6 @@ func init() { util.FlagRequired, )) - // createOptions.UpdateFlagSet(flags) - mainCmd.AddCommand(command) } @@ -132,3 +120,24 @@ func readFile(fileName string, data *map[string]string) error { return nil } + +func createEnvVar(cmd *cobra.Command, name string, value string, isSecret bool) error { + settings := config.GetSettings() + createOptions := variable.NewCreateOptions() + createOptions.Environment = settings.Profile.Context.Environment + createOptions.Name = name + createOptions.Value = value + if isSecret { + createOptions.IsSecret = enum.BoolTrue + } + + model, err := variable.Create(createOptions) + + if err != nil { + return err + } else { + lib.FormatCommandData(cmd, model) + } + + return nil +} \ No newline at end of file From a495485aa0b17ce3945d99b9a6c438fd1f3e4a41 Mon Sep 17 00:00:00 2001 From: Amin Al Ali Al Darwish Date: Tue, 21 May 2024 00:05:06 +0300 Subject: [PATCH 3/5] tested the ignoreDuplicates flag and refactored the code a bit --- cmd/variable/action/import.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/variable/action/import.go b/cmd/variable/action/import.go index ba92d3b..acb1f07 100644 --- a/cmd/variable/action/import.go +++ b/cmd/variable/action/import.go @@ -2,6 +2,7 @@ package action import ( "errors" + "fmt" "bunnyshell.com/cli/pkg/api/variable" "bunnyshell.com/cli/pkg/config" @@ -59,7 +60,7 @@ func init() { for key, value := range data.Vars { err := createEnvVar(cmd, key, value, false) if err != nil { - if ignoreDuplicates && err.Error() == "An Environment Variable with this name already exists in this environment." { + if ignoreDuplicates && err.Error() == "An error occurred: name: An Environment Variable with this name already exists in this environment." { continue } else { return lib.FormatCommandError(cmd, err) @@ -72,7 +73,7 @@ func init() { for key, value := range data.Secrets { err := createEnvVar(cmd, key, value, true) if err != nil { - if ignoreDuplicates && err.Error() == "An Environment Variable with this name already exists in this environment." { + if ignoreDuplicates && err.Error() == "An error occurred: name: An Environment Variable with this name already exists in this environment." { continue } else { return lib.FormatCommandError(cmd, err) @@ -89,7 +90,7 @@ func init() { flags.StringVar(&varFile, "vars-file", varFile, "File to import variables from") flags.StringVar(&secretFile, "secrets-file", secretFile, "File to import secrets from") - flags.BoolVarP(&ignoreDuplicates, "ignore-duplicates", "ign-dp", false, "Skip variables that already exist in the environment") + flags.BoolVarP(&ignoreDuplicates, "ignore-duplicates", "", false, "Skip variables that already exist in the environment") flags.AddFlag(options.Environment.AddFlagWithExtraHelp( "environment", @@ -140,4 +141,4 @@ func createEnvVar(cmd *cobra.Command, name string, value string, isSecret bool) } return nil -} \ No newline at end of file +} From 65dab76d90314f021aaf9a5b2250e83fbc24e171 Mon Sep 17 00:00:00 2001 From: Amin Al Ali Al Darwish Date: Tue, 21 May 2024 00:07:26 +0300 Subject: [PATCH 4/5] removed fmt from imports --- cmd/variable/action/import.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/variable/action/import.go b/cmd/variable/action/import.go index acb1f07..ecc8df7 100644 --- a/cmd/variable/action/import.go +++ b/cmd/variable/action/import.go @@ -2,7 +2,6 @@ package action import ( "errors" - "fmt" "bunnyshell.com/cli/pkg/api/variable" "bunnyshell.com/cli/pkg/config" From 2dd8a9ca3cb7d0568a1090a32336a589698e7777 Mon Sep 17 00:00:00 2001 From: Amin Al Ali Al Darwish Date: Tue, 21 May 2024 00:23:21 +0300 Subject: [PATCH 5/5] updated post checking if isSecret defaults to false|null --- cmd/variable/action/import.go | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/cmd/variable/action/import.go b/cmd/variable/action/import.go index ecc8df7..986149f 100644 --- a/cmd/variable/action/import.go +++ b/cmd/variable/action/import.go @@ -57,26 +57,16 @@ func init() { if len(data.Vars) > 0 { for key, value := range data.Vars { - err := createEnvVar(cmd, key, value, false) - if err != nil { - if ignoreDuplicates && err.Error() == "An error occurred: name: An Environment Variable with this name already exists in this environment." { - continue - } else { - return lib.FormatCommandError(cmd, err) - } + if err := createEnvVar(cmd, key, value, false, ignoreDuplicates); err != nil { + return lib.FormatCommandError(cmd, err) } } } if len(data.Secrets) > 0 { for key, value := range data.Secrets { - err := createEnvVar(cmd, key, value, true) - if err != nil { - if ignoreDuplicates && err.Error() == "An error occurred: name: An Environment Variable with this name already exists in this environment." { - continue - } else { - return lib.FormatCommandError(cmd, err) - } + if err := createEnvVar(cmd, key, value, true, ignoreDuplicates); err != nil { + return lib.FormatCommandError(cmd, err) } } } @@ -121,23 +111,27 @@ func readFile(fileName string, data *map[string]string) error { return nil } -func createEnvVar(cmd *cobra.Command, name string, value string, isSecret bool) error { +func createEnvVar(cmd *cobra.Command, name string, value string, isSecret bool, ignoreDuplicates bool) error { settings := config.GetSettings() createOptions := variable.NewCreateOptions() createOptions.Environment = settings.Profile.Context.Environment createOptions.Name = name createOptions.Value = value - if isSecret { + if isSecret { createOptions.IsSecret = enum.BoolTrue } model, err := variable.Create(createOptions) - if err != nil { - return err - } else { + if err == nil { lib.FormatCommandData(cmd, model) + + return nil + } + + if ignoreDuplicates && err.Error() == "An error occurred: name: An Environment Variable with this name already exists in this environment." { + return nil } - return nil + return err }