diff --git a/commands/pipeline/run/pipeline_run.go b/commands/pipeline/run/pipeline_run.go index 60ac6a73e..af9a33633 100644 --- a/commands/pipeline/run/pipeline_run.go +++ b/commands/pipeline/run/pipeline_run.go @@ -2,6 +2,8 @@ package run import ( "fmt" + "regexp" + "strings" "github.com/profclems/glab/commands/cmdutils" "github.com/profclems/glab/internal/git" @@ -12,6 +14,10 @@ import ( "github.com/xanzy/go-gitlab" ) +const keyValuePair = ".+:.+" + +var re = regexp.MustCompile(keyValuePair) + func getDefaultBranch(f *cmdutils.Factory) string { repo, err := f.BaseRepo() if err != nil { @@ -57,8 +63,7 @@ func NewCmdRun(f *cmdutils.Factory) *cobra.Command { return err } - // TODO: support setting pipeline variables via cli. - v := []*gitlab.PipelineVariable{ + pipelineVars := []*gitlab.PipelineVariable{ { Key: "GLAB_CLI_KEY", Value: "GLAB_CLI_VAL", @@ -66,8 +71,22 @@ func NewCmdRun(f *cmdutils.Factory) *cobra.Command { }, } + if customPipelineVars, _ := cmd.Flags().GetStringSlice("variables"); len(customPipelineVars) > 0 { + for _, v := range customPipelineVars { + if !re.MatchString(v) { + return fmt.Errorf("Bad pipeline variable : \"%s\" should be of format KEY:VALUE", v) + } + s := strings.Split(v, ":") + pipelineVars = append(pipelineVars, &gitlab.PipelineVariable{ + Key: s[0], + Value: s[1], + VariableType: "env_var", + }) + } + } + c := &gitlab.CreatePipelineOptions{ - Variables: v, + Variables: pipelineVars, } if m, _ := cmd.Flags().GetString("branch"); m != "" { @@ -86,6 +105,7 @@ func NewCmdRun(f *cmdutils.Factory) *cobra.Command { }, } pipelineRunCmd.Flags().StringP("branch", "b", "", "Create pipeline on branch/ref ") + pipelineRunCmd.Flags().StringSliceP("variables", "", []string{}, "Pass variables to pipeline") return pipelineRunCmd }