Skip to content

Commit 3a96d46

Browse files
authored
Merge pull request #2031 from FabianKramm/master
refactor: rename variables and forbid command overwrite
2 parents 3cb6559 + 77e6c84 commit 3a96d46

32 files changed

Lines changed: 419 additions & 333 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/overwrite_command.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

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 & 21 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,33 +256,21 @@ 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(replaceCommand("dev", rawConfig, f, globalFlags, NewDevCmd))
264-
rootCmd.AddCommand(replaceCommand("deploy", rawConfig, f, globalFlags, NewDeployCmd))
265-
rootCmd.AddCommand(replaceCommand("build", rawConfig, f, globalFlags, NewBuildCmd))
266-
rootCmd.AddCommand(replaceCommand("purge", rawConfig, f, globalFlags, NewPurgeCmd))
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, "")
270270
variable.AddPredefinedVars(plugins)
271271
return rootCmd
272272
}
273273

274-
func replaceCommand(command string, rawConfig *RawConfig, f factory.Factory, globalFlags *flags.GlobalFlags, fallback func(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command) *cobra.Command {
275-
if rawConfig != nil && rawConfig.CommandsConfig != nil && rawConfig.Resolver != nil && rawConfig.CommandsConfig.Commands != nil {
276-
// get command
277-
overwriteCommand, ok := rawConfig.CommandsConfig.Commands[command]
278-
if ok && !overwriteCommand.DisableReplace {
279-
return NewOverwriteCmd(f, globalFlags, overwriteCommand, rawConfig.Resolver.ResolvedVariables())
280-
}
281-
}
282-
283-
return fallback(f, globalFlags)
284-
}
285-
286274
func disableKlog() {
287275
flagSet := &flag.FlagSet{}
288276
klog.InitFlags(flagSet)
@@ -338,7 +326,7 @@ type RawConfig struct {
338326
RawConfig map[string]interface{}
339327
Resolver variable.Resolver
340328

341-
CommandsConfig *latest.Config
329+
Config *latest.Config
342330

343331
resolvedMutex sync.Mutex
344332
resolved map[string]string
@@ -357,8 +345,8 @@ func (r *RawConfig) Parse(
357345
r.Resolver = resolver
358346

359347
// try parsing commands
360-
latestConfig, beforeConversion, err := loader.NewCommandsParser().Parse(ctx, originalRawConfig, rawConfig, resolver, log)
361-
r.CommandsConfig = latestConfig
348+
latestConfig, beforeConversion, err := loader.NewCommandsPipelinesParser().Parse(ctx, originalRawConfig, rawConfig, resolver, log)
349+
r.Config = latestConfig
362350
return latestConfig, beforeConversion, err
363351
}
364352

cmd/run.go

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ 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 {
80-
runCmd.AddCommand(NewOverwriteCmd(f, globalFlags, cmd, rawConfig.Resolver.ResolvedVariables()))
78+
if rawConfig != nil && rawConfig.Config != nil {
79+
for _, cmd := range rawConfig.Config.Commands {
80+
runCmd.AddCommand(NewSpecificRunCommand(f, globalFlags, cmd, rawConfig.Resolver.ResolvedVariables()))
8181
}
8282
}
8383
runCmd.Flags().StringVar(&cmd.Dependency, "dependency", "", "Run a command from a specific dependency")
@@ -308,3 +308,60 @@ func ExecuteCommand(ctx context.Context, cmd *latest.CommandConfig, variables ma
308308
shellArgs = append(shellArgs, args...)
309309
return command.CommandWithEnv(ctx, dir, stdout, stderr, stdin, extraEnv, shellCommand, shellArgs...)
310310
}
311+
312+
// RunCommandCmd holds the cmd flags of an run command
313+
type RunCommandCmd struct {
314+
*flags.GlobalFlags
315+
316+
Command *latest.CommandConfig
317+
Variables map[string]interface{}
318+
319+
Stdout io.Writer
320+
Stderr io.Writer
321+
}
322+
323+
// NewSpecificRunCommand creates a new run command
324+
func NewSpecificRunCommand(f factory.Factory, globalFlags *flags.GlobalFlags, command *latest.CommandConfig, variables map[string]interface{}) *cobra.Command {
325+
cmd := &RunCommandCmd{
326+
GlobalFlags: globalFlags,
327+
Command: command,
328+
Variables: variables,
329+
Stdout: os.Stdout,
330+
Stderr: os.Stderr,
331+
}
332+
333+
description := command.Description
334+
longDescription := command.Description
335+
if description == "" {
336+
description = "Runs command: " + command.Name
337+
longDescription = description
338+
}
339+
if len(description) > 64 {
340+
if len(description) > 64 {
341+
description = description[:61] + "..."
342+
}
343+
}
344+
345+
runCmd := &cobra.Command{
346+
Use: command.Name,
347+
Short: description,
348+
Long: longDescription,
349+
Args: cobra.ArbitraryArgs,
350+
RunE: func(cobraCmd *cobra.Command, _ []string) error {
351+
args, err := ParseArgs(cobraCmd, cmd.GlobalFlags, f.GetLog())
352+
if err != nil {
353+
return err
354+
}
355+
356+
plugin.SetPluginCommand(cobraCmd, args)
357+
return cmd.Run(f, args)
358+
},
359+
}
360+
runCmd.DisableFlagParsing = true
361+
return runCmd
362+
}
363+
364+
func (cmd *RunCommandCmd) Run(f factory.Factory, args []string) error {
365+
devCtx := devspacecontext.NewContext(context.Background(), cmd.Variables, f.GetLog())
366+
return executeCommandWithAfter(devCtx.Context(), cmd.Command, args, cmd.Variables, devCtx.WorkingDir(), cmd.Stdout, cmd.Stderr, os.Stdin, devCtx.Log())
367+
}

0 commit comments

Comments
 (0)