Skip to content

Commit

Permalink
pipeline variables - improve setting variables so that it's possible …
Browse files Browse the repository at this point in the history
…to set any variable including secure
  • Loading branch information
3ximus committed Feb 19, 2024
1 parent b198b2a commit efda7d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
8 changes: 4 additions & 4 deletions api/bb-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,14 @@ func GetPipelineVariables(repository string) <-chan []EnvironmentVariable {
return channel
}

func CreatePipelineVariable(repository string, key string, value string) <-chan EnvironmentVariable {
func CreatePipelineVariable(repository string, key string, value string, secure bool) <-chan EnvironmentVariable {
channel := make(chan EnvironmentVariable)
go func() {
defer close(channel)
body := EnvironmentVariable{
Key: key,
Value: value,
Secured: false,
Secured: secure,
}
content, err := json.Marshal(body)
cobra.CheckErr(err)
Expand All @@ -493,14 +493,14 @@ func CreatePipelineVariable(repository string, key string, value string) <-chan
return channel
}

func UpdatePipelineVariable(repository string, varUUID string, key string, value string) <-chan EnvironmentVariable {
func UpdatePipelineVariable(repository string, varUUID string, key string, value string, secure bool) <-chan EnvironmentVariable {
channel := make(chan EnvironmentVariable)
go func() {
defer close(channel)
body := EnvironmentVariable{
Key: key,
Value: value,
Secured: false,
Secured: secure,
}
content, err := json.Marshal(body)
cobra.CheckErr(err)
Expand Down
64 changes: 34 additions & 30 deletions cmd/pipeline/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bb/api"
"bb/util"
"fmt"
"strings"
"regexp"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -20,27 +20,9 @@ var VariablesCmd = &cobra.Command{
variables := <-api.GetPipelineVariables(repo)

setVars, _ := cmd.Flags().GetStringSlice("set")
if len(setVars) > 0 {
for _, v := range setVars {
keyVal := strings.Split(v, "=")
if len(keyVal) != 2 {
cobra.CheckErr(fmt.Sprintf("Variable \"%s\" must be in the format \"KEY=VALUE\"", v))
}
updated := false
for _, ev := range variables {
if ev.Key == keyVal[0] {
updatedVar := <-api.UpdatePipelineVariable(repo, ev.UUID, keyVal[0], keyVal[1])
util.Printf("\033[1;34mUpdated\033[m \"%s=%s\"\n", updatedVar.Key, updatedVar.Value)
updated = true
break
}
}
if !updated {
createdVar := <-api.CreatePipelineVariable(repo, keyVal[0], keyVal[1])
util.Printf("\033[1;32mCreated\033[m \"%s=%s\"\n", createdVar.Key, createdVar.Value)
}
}
}
upsertVariables(repo, setVars, variables, false)
setSecureVars, _ := cmd.Flags().GetStringSlice("set-secure")
upsertVariables(repo, setSecureVars, variables, true)

deleteVars, _ := cmd.Flags().GetStringSlice("delete")
if len(deleteVars) > 0 {
Expand All @@ -53,10 +35,9 @@ var VariablesCmd = &cobra.Command{
}
}
}

}

if len(setVars) == 0 && len(deleteVars) == 0 {
if len(setVars) == 0 && len(deleteVars) == 0 && len(setSecureVars) == 0 {
for _, variable := range variables {
if variable.Secured {
util.Printf("%s = \033[37m***\033[m", variable.Key)
Expand All @@ -70,11 +51,34 @@ var VariablesCmd = &cobra.Command{
}

func init() {
// TODO make it possible to set secure variables with a special flag. Maybe A=B
// TODO Commas are used to separate variables within the same flag so "-s A=B,X" will not set variable A to the value B,X
// So we must not use StringSlice and use String array instead ?
// TODO It cannot also handle = signs
VariablesCmd.Flags().StringSliceP("set", "s", []string{}, `set one or multiple variables. If the variable doesn't exist one is created.
VariablesCmd.Flags().StringArrayP("set", "s", []string{}, `set one or multiple variables. If the variable doesn't exist one is created.
Variables must be in the format KEY=VALUE`)
VariablesCmd.Flags().StringSliceP("delete", "d", []string{}, "delete one or multiple variables")
VariablesCmd.Flags().StringArrayP("set-secure", "S", []string{}, `set one or multiple secure variables. If the variable doesn't exist one is created.
Variables must be in the format KEY=VALUE`)
VariablesCmd.Flags().StringArrayP("delete", "d", []string{}, "delete one or multiple variables")
}

func upsertVariables(repo string, setVars []string, variables []api.EnvironmentVariable, secure bool) {
varRegex := regexp.MustCompile(`([^=]+)=(.*)`)
if len(setVars) > 0 {
for _, v := range setVars {
keyVal := varRegex.FindStringSubmatch(v)
if len(keyVal) != 3 {
cobra.CheckErr(fmt.Sprintf("Variable \"%s\" must be in the format \"KEY=VALUE\"", v))
}
updated := false
for _, ev := range variables {
if ev.Key == keyVal[1] {
updatedVar := <-api.UpdatePipelineVariable(repo, ev.UUID, keyVal[1], keyVal[2], secure)
util.Printf("\033[1;34mUpdated\033[m \"%s=%s\"\n", updatedVar.Key, updatedVar.Value)
updated = true
break
}
}
if !updated {
createdVar := <-api.CreatePipelineVariable(repo, keyVal[1], keyVal[2], secure)
util.Printf("\033[1;32mCreated\033[m \"%s=%s\"\n", createdVar.Key, createdVar.Value)
}
}
}
}

0 comments on commit efda7d9

Please sign in to comment.