Skip to content

Commit 22e04fd

Browse files
committed
feat: add pipeline flags
1 parent d3e06af commit 22e04fd

16 files changed

Lines changed: 204 additions & 59 deletions

File tree

cmd/build.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package cmd
22

33
import (
44
"github.com/loft-sh/devspace/cmd/flags"
5+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
56
"github.com/loft-sh/devspace/pkg/util/factory"
67
"github.com/spf13/cobra"
78
)
89

910
// NewBuildCmd creates a new devspace build command
10-
func NewBuildCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
11+
func NewBuildCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
1112
cmd := &RunPipelineCmd{
1213
GlobalFlags: globalFlags,
1314
Pipeline: "build",
@@ -23,10 +24,14 @@ func NewBuildCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Comma
2324
Builds all defined images and pushes them
2425
#######################################################`,
2526
RunE: func(cobraCmd *cobra.Command, args []string) error {
26-
return cmd.Run(cobraCmd, args, f, "build", "buildCommand")
27+
return cmd.Run(cobraCmd, args, f, "buildCommand")
2728
},
2829
}
2930

30-
cmd.AddFlags(buildCmd)
31+
var pipeline *latest.Pipeline
32+
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
33+
pipeline = rawConfig.Config.Pipelines["build"]
34+
}
35+
cmd.AddPipelineFlags(f, buildCmd, pipeline)
3136
return buildCmd
3237
}

cmd/deploy.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package cmd
22

33
import (
44
"github.com/loft-sh/devspace/cmd/flags"
5+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
56
"github.com/loft-sh/devspace/pkg/util/factory"
67
"github.com/spf13/cobra"
78
)
89

910
// NewDeployCmd creates a new deploy command
10-
func NewDeployCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
11+
func NewDeployCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
1112
cmd := &RunPipelineCmd{
1213
GlobalFlags: globalFlags,
1314
SkipPushLocalKubernetes: true,
@@ -29,10 +30,14 @@ devspace deploy --kube-context=deploy-context
2930
#######################################################`,
3031
Args: cobra.NoArgs,
3132
RunE: func(cobraCmd *cobra.Command, args []string) error {
32-
return cmd.Run(cobraCmd, args, f, "deploy", "deployCommand")
33+
return cmd.Run(cobraCmd, args, f, "deployCommand")
3334
},
3435
}
3536

36-
cmd.AddFlags(deployCmd)
37+
var pipeline *latest.Pipeline
38+
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
39+
pipeline = rawConfig.Config.Pipelines["deploy"]
40+
}
41+
cmd.AddPipelineFlags(f, deployCmd, pipeline)
3742
return deployCmd
3843
}

cmd/dev.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package cmd
22

33
import (
44
"github.com/loft-sh/devspace/cmd/flags"
5+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
56
"github.com/loft-sh/devspace/pkg/util/factory"
67
"github.com/spf13/cobra"
78
)
89

910
// NewDevCmd creates a new devspace dev command
10-
func NewDevCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
11+
func NewDevCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
1112
cmd := &RunPipelineCmd{
1213
GlobalFlags: globalFlags,
1314
SkipPushLocalKubernetes: true,
@@ -24,10 +25,14 @@ Starts your project in development mode
2425
#######################################################`,
2526
Args: cobra.NoArgs,
2627
RunE: func(cobraCmd *cobra.Command, args []string) error {
27-
return cmd.Run(cobraCmd, args, f, "dev", "devCommand")
28+
return cmd.Run(cobraCmd, args, f, "devCommand")
2829
},
2930
}
3031

31-
cmd.AddFlags(devCmd)
32+
var pipeline *latest.Pipeline
33+
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
34+
pipeline = rawConfig.Config.Pipelines["dev"]
35+
}
36+
cmd.AddPipelineFlags(f, devCmd, pipeline)
3237
return devCmd
3338
}

cmd/purge.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package cmd
22

33
import (
4+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
45
"github.com/loft-sh/devspace/pkg/util/factory"
56

67
"github.com/loft-sh/devspace/cmd/flags"
78
"github.com/spf13/cobra"
89
)
910

1011
// NewPurgeCmd creates a new purge command
11-
func NewPurgeCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
12+
func NewPurgeCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
1213
cmd := &RunPipelineCmd{
1314
GlobalFlags: globalFlags,
1415
Pipeline: "purge",
@@ -27,10 +28,14 @@ devspace purge
2728
#######################################################`,
2829
Args: cobra.NoArgs,
2930
RunE: func(cobraCmd *cobra.Command, args []string) error {
30-
return cmd.Run(cobraCmd, args, f, "purge", "purgeCommand")
31+
return cmd.Run(cobraCmd, args, f, "purgeCommand")
3132
},
3233
}
3334

34-
cmd.AddFlags(purgeCmd)
35+
var pipeline *latest.Pipeline
36+
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
37+
pipeline = rawConfig.Config.Pipelines["purge"]
38+
}
39+
cmd.AddPipelineFlags(f, purgeCmd, pipeline)
3540
return purgeCmd
3641
}

cmd/render.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
45
"os"
56

67
"github.com/loft-sh/devspace/cmd/flags"
@@ -9,7 +10,7 @@ import (
910
)
1011

1112
// NewRenderCmd creates a new devspace render command
12-
func NewRenderCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
13+
func NewRenderCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
1314
cmd := &RunPipelineCmd{
1415
GlobalFlags: globalFlags,
1516
SkipPushLocalKubernetes: true,
@@ -31,10 +32,14 @@ deployment.
3132
#######################################################`,
3233
RunE: func(cobraCmd *cobra.Command, args []string) error {
3334
f.GetLog().Warnf("This command is deprecated, please use 'devspace deploy --render' instead")
34-
return cmd.Run(cobraCmd, args, f, "render", "renderCommand")
35+
return cmd.Run(cobraCmd, args, f, "renderCommand")
3536
},
3637
}
3738

38-
cmd.AddFlags(renderCmd)
39+
var pipeline *latest.Pipeline
40+
if rawConfig != nil && rawConfig.Config != nil && rawConfig.Config.Pipelines != nil {
41+
pipeline = rawConfig.Config.Pipelines["deploy"]
42+
}
43+
cmd.AddPipelineFlags(f, renderCmd, pipeline)
3944
return renderCmd
4045
}

cmd/root.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ Additional run commands:
246246
rootCmd.AddCommand(NewInitCmd(f))
247247
rootCmd.AddCommand(NewRestartCmd(f, globalFlags))
248248
rootCmd.AddCommand(NewSyncCmd(f, globalFlags))
249-
rootCmd.AddCommand(NewRenderCmd(f, globalFlags))
249+
rootCmd.AddCommand(NewRenderCmd(f, globalFlags, rawConfig))
250250
rootCmd.AddCommand(NewUpgradeCmd())
251251
rootCmd.AddCommand(NewEnterCmd(f, globalFlags))
252252
rootCmd.AddCommand(NewAnalyzeCmd(f, globalFlags))
@@ -256,14 +256,14 @@ Additional run commands:
256256
rootCmd.AddCommand(NewRunCmd(f, globalFlags, rawConfig))
257257
rootCmd.AddCommand(NewAttachCmd(f, globalFlags))
258258
rootCmd.AddCommand(NewPrintCmd(f, globalFlags))
259-
rootCmd.AddCommand(NewRunPipelineCmd(f, globalFlags))
259+
rootCmd.AddCommand(NewRunPipelineCmd(f, globalFlags, rawConfig))
260260
rootCmd.AddCommand(NewCompletionCmd())
261261

262262
// check overwrite commands
263-
rootCmd.AddCommand(NewDevCmd(f, globalFlags))
264-
rootCmd.AddCommand(NewDeployCmd(f, globalFlags))
265-
rootCmd.AddCommand(NewBuildCmd(f, globalFlags))
266-
rootCmd.AddCommand(NewPurgeCmd(f, globalFlags))
263+
rootCmd.AddCommand(NewDevCmd(f, globalFlags, rawConfig))
264+
rootCmd.AddCommand(NewDeployCmd(f, globalFlags, rawConfig))
265+
rootCmd.AddCommand(NewBuildCmd(f, globalFlags, rawConfig))
266+
rootCmd.AddCommand(NewPurgeCmd(f, globalFlags, rawConfig))
267267

268268
// Add plugin commands
269269
plugin.AddPluginCommands(rootCmd, plugins, "")
@@ -326,7 +326,7 @@ type RawConfig struct {
326326
RawConfig map[string]interface{}
327327
Resolver variable.Resolver
328328

329-
CommandsConfig *latest.Config
329+
Config *latest.Config
330330

331331
resolvedMutex sync.Mutex
332332
resolved map[string]string
@@ -345,8 +345,8 @@ func (r *RawConfig) Parse(
345345
r.Resolver = resolver
346346

347347
// try parsing commands
348-
latestConfig, beforeConversion, err := loader.NewCommandsParser().Parse(ctx, originalRawConfig, rawConfig, resolver, log)
349-
r.CommandsConfig = latestConfig
348+
latestConfig, beforeConversion, err := loader.NewCommandsPipelinesParser().Parse(ctx, originalRawConfig, rawConfig, resolver, log)
349+
r.Config = latestConfig
350350
return latestConfig, beforeConversion, err
351351
}
352352

cmd/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ devspace --dependency my-dependency run any-command --any-command-flag
7575
return cmd.RunRun(f, args)
7676
}
7777

78-
if rawConfig != nil && rawConfig.CommandsConfig != nil {
79-
for _, cmd := range rawConfig.CommandsConfig.Commands {
78+
if rawConfig != nil && rawConfig.Config != nil {
79+
for _, cmd := range rawConfig.Config.Commands {
8080
runCmd.AddCommand(NewSpecificRunCommand(f, globalFlags, cmd, rawConfig.Resolver.ResolvedVariables()))
8181
}
8282
}

cmd/run_pipeline.go

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type RunPipelineCmd struct {
6767
log log.Logger
6868
}
6969

70-
func (cmd *RunPipelineCmd) AddFlags(command *cobra.Command) {
70+
func (cmd *RunPipelineCmd) AddPipelineFlags(f factory.Factory, command *cobra.Command, pipeline *latest.Pipeline) {
7171
command.Flags().StringSliceVar(&cmd.SkipDependency, "skip-dependency", cmd.SkipDependency, "Skips the following dependencies for deployment")
7272
command.Flags().StringSliceVar(&cmd.Dependency, "dependency", cmd.Dependency, "Deploys only the specified named dependencies")
7373

@@ -87,41 +87,134 @@ func (cmd *RunPipelineCmd) AddFlags(command *cobra.Command) {
8787
command.Flags().BoolVar(&cmd.SkipPushLocalKubernetes, "skip-push-local-kube", cmd.SkipPushLocalKubernetes, "Skips image pushing, if a local kubernetes environment is detected")
8888

8989
command.Flags().BoolVar(&cmd.ShowUI, "show-ui", cmd.ShowUI, "Shows the ui server")
90+
91+
if pipeline != nil {
92+
for _, pipelineFlag := range pipeline.Flags {
93+
if pipelineFlag.Name == "" {
94+
continue
95+
}
96+
97+
usage := pipelineFlag.Description
98+
if usage == "" {
99+
usage = "Flag " + pipelineFlag.Name
100+
}
101+
102+
var ok bool
103+
if pipelineFlag.Type == "" || pipelineFlag.Type == latest.PipelineFlagTypeBoolean {
104+
val := false
105+
if pipelineFlag.Default != nil {
106+
val, ok = pipelineFlag.Default.(bool)
107+
if !ok {
108+
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a boolean", pipelineFlag.Name, pipelineFlag.Default)
109+
continue
110+
}
111+
}
112+
113+
command.Flags().BoolP(pipelineFlag.Name, pipelineFlag.Short, val, usage)
114+
} else if pipelineFlag.Type == latest.PipelineFlagTypeString {
115+
val := ""
116+
if pipelineFlag.Default != nil {
117+
val, ok = pipelineFlag.Default.(string)
118+
if !ok {
119+
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a string", pipelineFlag.Name, pipelineFlag.Default)
120+
continue
121+
}
122+
}
123+
124+
command.Flags().StringP(pipelineFlag.Name, pipelineFlag.Short, val, usage)
125+
} else if pipelineFlag.Type == latest.PipelineFlagTypeInteger {
126+
val := 0
127+
if pipelineFlag.Default != nil {
128+
val, ok = pipelineFlag.Default.(int)
129+
if !ok {
130+
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not an integer", pipelineFlag.Name, pipelineFlag.Default)
131+
continue
132+
}
133+
}
134+
135+
command.Flags().IntP(pipelineFlag.Name, pipelineFlag.Short, val, usage)
136+
} else if pipelineFlag.Type == latest.PipelineFlagTypeStringArray {
137+
val := []string{}
138+
if pipelineFlag.Default != nil {
139+
val, ok = pipelineFlag.Default.([]string)
140+
if !ok {
141+
f.GetLog().Errorf("Error parsing default value for flag %s: %#v is not a string array", pipelineFlag.Name, pipelineFlag.Default)
142+
continue
143+
}
144+
}
145+
146+
command.Flags().StringSliceP(pipelineFlag.Name, pipelineFlag.Short, val, usage)
147+
}
148+
}
149+
}
90150
}
91151

92152
// NewRunPipelineCmd creates a new devspace run-pipeline command
93-
func NewRunPipelineCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
153+
func NewRunPipelineCmd(f factory.Factory, globalFlags *flags.GlobalFlags, rawConfig *RawConfig) *cobra.Command {
94154
cmd := &RunPipelineCmd{
95155
GlobalFlags: globalFlags,
96156
SkipPushLocalKubernetes: true,
97157
}
98158
runPipelineCmd := &cobra.Command{
99159
Use: "run-pipeline",
100-
Short: "Starts the development mode",
160+
Short: "Starts a DevSpace pipeline",
101161
Long: `
102162
#######################################################
103163
############## devspace run-pipeline ##################
104164
#######################################################
105-
Execute a pipeline
165+
Execute a pipeline:
166+
devspace run-pipeline my-pipeline
167+
devspace run-pipeline dev
106168
#######################################################`,
107169
Args: cobra.ArbitraryArgs,
108170
RunE: func(cobraCmd *cobra.Command, args []string) error {
109-
return cmd.Run(cobraCmd, args, f, "run-pipeline", "runPipelineCommand")
171+
return cmd.Run(cobraCmd, args, f, "runPipelineCommand")
110172
},
111173
}
112174

113-
cmd.AddFlags(runPipelineCmd)
175+
if rawConfig != nil && rawConfig.Config != nil {
176+
for _, pipe := range rawConfig.Config.Pipelines {
177+
runPipelineCmd.AddCommand(NewSpecificPipelineCmd(f, globalFlags, pipe))
178+
}
179+
}
180+
cmd.AddPipelineFlags(f, runPipelineCmd, nil)
114181
return runPipelineCmd
115182
}
116183

184+
// NewSpecificPipelineCmd creates a new devspace render command
185+
func NewSpecificPipelineCmd(f factory.Factory, globalFlags *flags.GlobalFlags, pipeline *latest.Pipeline) *cobra.Command {
186+
cmd := &RunPipelineCmd{
187+
GlobalFlags: globalFlags,
188+
SkipPushLocalKubernetes: true,
189+
Pipeline: pipeline.Name,
190+
}
191+
192+
specificPipelineCmd := &cobra.Command{
193+
Use: pipeline.Name,
194+
Short: "Executes pipeline " + pipeline.Name,
195+
Long: `
196+
#######################################################
197+
######### devspace run-pipeline ` + pipeline.Name + ` ##########
198+
#######################################################
199+
Executes pipeline ` + pipeline.Name + `
200+
#######################################################`,
201+
RunE: func(cobraCmd *cobra.Command, args []string) error {
202+
return cmd.Run(cobraCmd, args, f, "runPipelineCommand")
203+
},
204+
}
205+
206+
cmd.AddPipelineFlags(f, specificPipelineCmd, pipeline)
207+
return specificPipelineCmd
208+
}
209+
117210
func (cmd *RunPipelineCmd) RunDefault(f factory.Factory) error {
118-
return cmd.Run(nil, nil, f, "run-pipeline", "runPipelineCommand")
211+
return cmd.Run(nil, nil, f, "runPipelineCommand")
119212
}
120213

121214
// Run executes the command logic
122-
func (cmd *RunPipelineCmd) Run(cobraCmd *cobra.Command, args []string, f factory.Factory, commandName, hookName string) error {
215+
func (cmd *RunPipelineCmd) Run(cobraCmd *cobra.Command, args []string, f factory.Factory, hookName string) error {
123216
dashArgs := []string{}
124-
if cobraCmd.ArgsLenAtDash() > -1 {
217+
if cobraCmd != nil && cobraCmd.ArgsLenAtDash() > -1 {
125218
dashArgs = args[cobraCmd.ArgsLenAtDash():]
126219
args = args[:cobraCmd.ArgsLenAtDash()]
127220
}
@@ -159,7 +252,9 @@ func (cmd *RunPipelineCmd) Run(cobraCmd *cobra.Command, args []string, f factory
159252
}
160253

161254
// set command in context
162-
cmd.Ctx = values.WithFlags(cmd.Ctx, cobraCmd.Flags())
255+
if cobraCmd != nil {
256+
cmd.Ctx = values.WithFlags(cmd.Ctx, cobraCmd.Flags())
257+
}
163258
options := cmd.BuildOptions(cmd.ToConfigOptions())
164259
ctx, err := initialize(cmd.Ctx, f, false, options, cmd.log)
165260
if err != nil {

0 commit comments

Comments
 (0)