Skip to content

Commit

Permalink
created createEnvVar Function and added ignoreDuplicate flag to skip …
Browse files Browse the repository at this point in the history
…over variables already defined
  • Loading branch information
aminalali8 committed May 20, 2024
1 parent 1a859d3 commit 0f88508
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions cmd/variable/action/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package action

import (
"errors"
"fmt"

"bunnyshell.com/cli/pkg/api/variable"
"bunnyshell.com/cli/pkg/config"
Expand All @@ -21,6 +20,7 @@ type BulkImport struct {
func init() {
varFile := ""
secretFile := ""
ignoreDuplicates := false
options := config.GetOptions()
data := BulkImport{
Vars: make(map[string]string),
Expand Down Expand Up @@ -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)
}
}
}
}
Expand All @@ -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",
Expand All @@ -108,8 +98,6 @@ func init() {
util.FlagRequired,
))

// createOptions.UpdateFlagSet(flags)

mainCmd.AddCommand(command)
}

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

0 comments on commit 0f88508

Please sign in to comment.