Skip to content

Commit 088997e

Browse files
committed
fix: treat pipeline flag defaults consistently when used as command flags or pipeline flags
Signed-off-by: Russell Centanni <russell.centanni@gmail.com>
1 parent e6293eb commit 088997e

3 files changed

Lines changed: 80 additions & 40 deletions

File tree

cmd/run_pipeline.go

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/loft-sh/devspace/pkg/devspace/hook"
2121
"github.com/loft-sh/devspace/pkg/devspace/kill"
2222
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
23-
"github.com/loft-sh/devspace/pkg/devspace/pipeline"
23+
pipelinepkg "github.com/loft-sh/devspace/pkg/devspace/pipeline"
2424
"github.com/loft-sh/devspace/pkg/devspace/pipeline/types"
2525
"github.com/loft-sh/devspace/pkg/devspace/plugin"
2626
"github.com/loft-sh/devspace/pkg/devspace/upgrade"
@@ -100,51 +100,34 @@ func (cmd *RunPipelineCmd) AddPipelineFlags(f factory.Factory, command *cobra.Co
100100
usage = "Flag " + pipelineFlag.Name
101101
}
102102

103-
var ok bool
104103
if pipelineFlag.Type == "" || pipelineFlag.Type == latest.PipelineFlagTypeBoolean {
105-
val := false
106-
if pipelineFlag.Default != nil {
107-
val, ok = pipelineFlag.Default.(bool)
108-
if !ok {
109-
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a boolean", pipelineFlag.Name, pipelineFlag.Default)
110-
continue
111-
}
104+
val, err := pipelinepkg.GetDefaultValue(pipelineFlag)
105+
if err != nil {
106+
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a boolean", pipelineFlag.Name, pipelineFlag.Default)
112107
}
113108

114-
command.Flags().BoolP(pipelineFlag.Name, pipelineFlag.Short, val, usage)
109+
command.Flags().BoolP(pipelineFlag.Name, pipelineFlag.Short, val.(bool), usage)
115110
} else if pipelineFlag.Type == latest.PipelineFlagTypeString {
116-
val := ""
117-
if pipelineFlag.Default != nil {
118-
val, ok = pipelineFlag.Default.(string)
119-
if !ok {
120-
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a string", pipelineFlag.Name, pipelineFlag.Default)
121-
continue
122-
}
111+
val, err := pipelinepkg.GetDefaultValue(pipelineFlag)
112+
if err != nil {
113+
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a string", pipelineFlag.Name, pipelineFlag.Default)
123114
}
124115

125-
command.Flags().StringP(pipelineFlag.Name, pipelineFlag.Short, val, usage)
116+
command.Flags().StringP(pipelineFlag.Name, pipelineFlag.Short, val.(string), usage)
126117
} else if pipelineFlag.Type == latest.PipelineFlagTypeInteger {
127-
val := 0
128-
if pipelineFlag.Default != nil {
129-
val, ok = pipelineFlag.Default.(int)
130-
if !ok {
131-
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not an integer", pipelineFlag.Name, pipelineFlag.Default)
132-
continue
133-
}
118+
val, err := pipelinepkg.GetDefaultValue(pipelineFlag)
119+
if err != nil {
120+
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not an integer", pipelineFlag.Name, pipelineFlag.Default)
134121
}
135122

136-
command.Flags().IntP(pipelineFlag.Name, pipelineFlag.Short, val, usage)
123+
command.Flags().IntP(pipelineFlag.Name, pipelineFlag.Short, val.(int), usage)
137124
} else if pipelineFlag.Type == latest.PipelineFlagTypeStringArray {
138-
val := []string{}
139-
if pipelineFlag.Default != nil {
140-
val, ok = pipelineFlag.Default.([]string)
141-
if !ok {
142-
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a string array", pipelineFlag.Name, pipelineFlag.Default)
143-
continue
144-
}
125+
val, err := pipelinepkg.GetDefaultValue(pipelineFlag)
126+
if err != nil {
127+
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a string array", pipelineFlag.Name, pipelineFlag.Default)
145128
}
146129

147-
command.Flags().StringSliceP(pipelineFlag.Name, pipelineFlag.Short, val, usage)
130+
command.Flags().StringSliceP(pipelineFlag.Name, pipelineFlag.Short, val.([]string), usage)
148131
}
149132
}
150133
}
@@ -463,7 +446,7 @@ func runPipeline(ctx devspacecontext.Context, args []string, options *CommandOpt
463446
dependencyRegistry := registry.NewDependencyRegistry(ctx.Config().Config().Name, options.DeployOptions.Render)
464447

465448
// get deploy pipeline
466-
pipe := pipeline.NewPipeline(ctx.Config().Config().Name, devPodManager, dependencyRegistry, configPipeline, options.Options)
449+
pipe := pipelinepkg.NewPipeline(ctx.Config().Config().Name, devPodManager, dependencyRegistry, configPipeline, options.Options)
467450
kill.SetStopFunction(func(message string) {
468451
if message != "" {
469452
ctx.Log().WriteString(logrus.FatalLevel, "\n"+ansi.Color("fatal", "red+b")+" "+message+"\n")

pkg/devspace/pipeline/flags.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package pipeline
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
7+
)
8+
9+
func GetDefaultValue(pipelineFlag latest.PipelineFlag) (interface{}, error) {
10+
var ok bool
11+
12+
switch pipelineFlag.Type {
13+
case "", latest.PipelineFlagTypeBoolean:
14+
val := false
15+
if pipelineFlag.Default != nil {
16+
val, ok = pipelineFlag.Default.(bool)
17+
if !ok {
18+
return nil, fmt.Errorf(" default is not a boolean")
19+
}
20+
}
21+
return val, nil
22+
case latest.PipelineFlagTypeString:
23+
val := ""
24+
if pipelineFlag.Default != nil {
25+
val, ok = pipelineFlag.Default.(string)
26+
if !ok {
27+
return nil, fmt.Errorf("default is not a string")
28+
}
29+
}
30+
return val, nil
31+
case latest.PipelineFlagTypeInteger:
32+
val := 0
33+
if pipelineFlag.Default != nil {
34+
val, ok = pipelineFlag.Default.(int)
35+
if !ok {
36+
return nil, fmt.Errorf("default is not an integer")
37+
}
38+
}
39+
return val, nil
40+
case latest.PipelineFlagTypeStringArray:
41+
val := []string{}
42+
if pipelineFlag.Default != nil {
43+
val, ok = pipelineFlag.Default.([]string)
44+
if !ok {
45+
return nil, fmt.Errorf("default is not a string array")
46+
}
47+
}
48+
return val, nil
49+
}
50+
51+
return nil, fmt.Errorf("unsupported flag type")
52+
}

pkg/devspace/pipeline/pipeline.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,21 @@ func (p *pipeline) WaitDev() error {
145145
p.m.Unlock()
146146

147147
// wait for children first
148-
errors := []error{}
148+
errs := []error{}
149149
for _, child := range children {
150150
err := child.WaitDev()
151151
if err != nil {
152-
errors = append(errors, err)
152+
errs = append(errs, err)
153153
}
154154
}
155155

156156
// wait for dev pods to finish
157157
err := p.devPodManager.Wait()
158158
if err != nil {
159-
errors = append(errors, err)
159+
errs = append(errs, err)
160160
}
161161

162-
return utilerrors.NewAggregate(errors)
162+
return utilerrors.NewAggregate(errs)
163163
}
164164

165165
func (p *pipeline) Name() string {
@@ -425,7 +425,12 @@ func (p *pipeline) startNewDependency(ctx devspacecontext.Context, dependency ty
425425
func applyFlags(ctx devspacecontext.Context, pipeline *latest.Pipeline, setFlags []string) (devspacecontext.Context, error) {
426426
newFlags := map[string]string{}
427427
for _, flag := range pipeline.Flags {
428-
newFlags[flag.Name] = fmt.Sprintf("%v", flag.Default)
428+
val, err := GetDefaultValue(flag)
429+
if err != nil {
430+
return nil, err
431+
}
432+
433+
newFlags[flag.Name] = fmt.Sprintf("%v", val)
429434
}
430435
for _, v := range setFlags {
431436
splitted := strings.Split(v, "=")

0 commit comments

Comments
 (0)