Skip to content

Commit d3e06af

Browse files
committed
refactor: rename variables and forbid command overwrite
1 parent ee7ee8a commit d3e06af

22 files changed

Lines changed: 222 additions & 281 deletions

File tree

cmd/overwrite_command.go

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

cmd/root.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,17 @@ Additional run commands:
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))
264+
rootCmd.AddCommand(NewDeployCmd(f, globalFlags))
265+
rootCmd.AddCommand(NewBuildCmd(f, globalFlags))
266+
rootCmd.AddCommand(NewPurgeCmd(f, globalFlags))
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)

cmd/run.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ devspace --dependency my-dependency run any-command --any-command-flag
7777

7878
if rawConfig != nil && rawConfig.CommandsConfig != nil {
7979
for _, cmd := range rawConfig.CommandsConfig.Commands {
80-
runCmd.AddCommand(NewOverwriteCmd(f, globalFlags, cmd, rawConfig.Resolver.ResolvedVariables()))
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+
}

cmd/run_pipeline.go

Lines changed: 19 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"github.com/loft-sh/devspace/cmd/flags"
77
"github.com/loft-sh/devspace/pkg/devspace/build"
8-
config2 "github.com/loft-sh/devspace/pkg/devspace/config"
98
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
109
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
1110
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
@@ -27,8 +26,6 @@ import (
2726
"github.com/loft-sh/devspace/pkg/util/interrupt"
2827
"github.com/loft-sh/devspace/pkg/util/log"
2928
"github.com/loft-sh/devspace/pkg/util/message"
30-
"github.com/loft-sh/devspace/pkg/util/ptr"
31-
"github.com/loft-sh/devspace/pkg/util/survey"
3229
"github.com/pkg/errors"
3330
"github.com/sirupsen/logrus"
3431
"github.com/spf13/cobra"
@@ -62,8 +59,6 @@ type RunPipelineCmd struct {
6259
ForceDeploy bool
6360
SkipDeploy bool
6461

65-
Terminal bool
66-
6762
ShowUI bool
6863

6964
// used for testing to allow interruption
@@ -91,7 +86,6 @@ func (cmd *RunPipelineCmd) AddFlags(command *cobra.Command) {
9186
command.Flags().BoolVar(&cmd.SkipPush, "skip-push", cmd.SkipPush, "Skips image pushing, useful for minikube deployment")
9287
command.Flags().BoolVar(&cmd.SkipPushLocalKubernetes, "skip-push-local-kube", cmd.SkipPushLocalKubernetes, "Skips image pushing, if a local kubernetes environment is detected")
9388

94-
command.Flags().BoolVar(&cmd.Terminal, "terminal", cmd.Terminal, "Open a terminal instead of showing logs")
9589
command.Flags().BoolVar(&cmd.ShowUI, "show-ui", cmd.ShowUI, "Shows the ui server")
9690
}
9791

@@ -110,14 +104,8 @@ func NewRunPipelineCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra
110104
#######################################################
111105
Execute a pipeline
112106
#######################################################`,
113-
Args: cobra.MaximumNArgs(1),
107+
Args: cobra.ArbitraryArgs,
114108
RunE: func(cobraCmd *cobra.Command, args []string) error {
115-
if len(args) == 0 && cmd.Pipeline == "" {
116-
return fmt.Errorf("please specify a pipeline through --pipeline or argument")
117-
} else if len(args) == 1 && cmd.Pipeline == "" {
118-
cmd.Pipeline = args[0]
119-
}
120-
121109
return cmd.Run(cobraCmd, args, f, "run-pipeline", "runPipelineCommand")
122110
},
123111
}
@@ -132,6 +120,20 @@ func (cmd *RunPipelineCmd) RunDefault(f factory.Factory) error {
132120

133121
// Run executes the command logic
134122
func (cmd *RunPipelineCmd) Run(cobraCmd *cobra.Command, args []string, f factory.Factory, commandName, hookName string) error {
123+
dashArgs := []string{}
124+
if cobraCmd.ArgsLenAtDash() > -1 {
125+
dashArgs = args[cobraCmd.ArgsLenAtDash():]
126+
args = args[:cobraCmd.ArgsLenAtDash()]
127+
}
128+
129+
if len(args) > 1 {
130+
return fmt.Errorf("please specify only 1 pipeline to execute. E.g. devspace run-pipeline my-pipe -- other args")
131+
} else if len(args) == 0 && cmd.Pipeline == "" {
132+
return fmt.Errorf("please specify a pipeline through --pipeline or argument")
133+
} else if len(args) == 1 && cmd.Pipeline == "" {
134+
cmd.Pipeline = args[0]
135+
}
136+
135137
if cmd.log == nil {
136138
cmd.log = f.GetLog()
137139
}
@@ -157,15 +159,15 @@ func (cmd *RunPipelineCmd) Run(cobraCmd *cobra.Command, args []string, f factory
157159
}
158160

159161
// set command in context
160-
cmd.Ctx = values.WithCommand(cmd.Ctx, commandName)
162+
cmd.Ctx = values.WithFlags(cmd.Ctx, cobraCmd.Flags())
161163
options := cmd.BuildOptions(cmd.ToConfigOptions())
162164
ctx, err := initialize(cmd.Ctx, f, false, options, cmd.log)
163165
if err != nil {
164166
return err
165167
}
166168

167169
return runWithHooks(ctx, hookName, func() error {
168-
return runPipeline(ctx, options)
170+
return runPipeline(ctx, dashArgs, options)
169171
})
170172
}
171173

@@ -176,7 +178,6 @@ type CommandOptions struct {
176178
ConfigOptions *loader.ConfigOptions
177179

178180
Pipeline string
179-
Terminal bool
180181
ShowUI bool
181182
UIPort int
182183
}
@@ -245,12 +246,6 @@ func initialize(ctx context.Context, f factory.Factory, allowFailingKubeClient b
245246
// add root name to context
246247
ctx = values.WithRootName(ctx, configInterface.Config().Name)
247248

248-
// adjust config
249-
err = adjustConfig(configInterface, options, logger)
250-
if err != nil {
251-
return nil, err
252-
}
253-
254249
// create devspace context
255250
devCtx := devspacecontext.NewContext(ctx, configInterface.Variables(), logger).
256251
WithConfig(configInterface).
@@ -295,50 +290,6 @@ func updateLastKubeContext(ctx devspacecontext.Context) error {
295290
return nil
296291
}
297292

298-
func adjustConfig(conf config2.Config, options *CommandOptions, log log.Logger) error {
299-
// check if terminal is enabled
300-
c := conf.Config()
301-
if options.Terminal {
302-
if len(c.Dev) == 0 {
303-
return errors.New("No dev config available in DevSpace config")
304-
}
305-
306-
devNames := make([]string, 0, len(c.Dev))
307-
for k := range c.Dev {
308-
devNames = append(devNames, k)
309-
}
310-
311-
// if only one image exists, use it, otherwise show image picker
312-
devName := ""
313-
if len(devNames) == 1 {
314-
devName = devNames[0]
315-
} else {
316-
var err error
317-
devName, err = log.Question(&survey.QuestionOptions{
318-
Question: "Where do you want to open a terminal to?",
319-
Options: devNames,
320-
})
321-
if err != nil {
322-
return err
323-
}
324-
}
325-
326-
// adjust dev config
327-
for k := range c.Dev {
328-
if k == devName {
329-
if c.Dev[devName].Terminal == nil {
330-
c.Dev[devName].Terminal = &latest.Terminal{}
331-
}
332-
c.Dev[devName].Terminal.Enabled = ptr.Bool(true)
333-
} else {
334-
c.Dev[devName].Terminal = nil
335-
}
336-
}
337-
}
338-
339-
return nil
340-
}
341-
342293
func runWithHooks(ctx devspacecontext.Context, command string, fn func() error) (err error) {
343294
err = hook.ExecuteHooks(ctx, nil, command+":before:execute")
344295
if err != nil {
@@ -405,13 +356,12 @@ func (cmd *RunPipelineCmd) BuildOptions(configOptions *loader.ConfigOptions) *Co
405356
},
406357
},
407358
ConfigOptions: configOptions,
408-
Terminal: cmd.Terminal,
409359
Pipeline: cmd.Pipeline,
410360
ShowUI: cmd.ShowUI,
411361
}
412362
}
413363

414-
func runPipeline(ctx devspacecontext.Context, options *CommandOptions) error {
364+
func runPipeline(ctx devspacecontext.Context, args []string, options *CommandOptions) error {
415365
var configPipeline *latest.Pipeline
416366
if ctx.Config().Config().Pipelines != nil && ctx.Config().Config().Pipelines[options.Pipeline] != nil {
417367
configPipeline = ctx.Config().Config().Pipelines[options.Pipeline]
@@ -459,7 +409,7 @@ func runPipeline(ctx devspacecontext.Context, options *CommandOptions) error {
459409
defer stderrWriter.Close()
460410

461411
// start pipeline
462-
err = pipe.Run(ctx.WithLogger(log.NewStreamLoggerWithFormat(stdoutWriter, stderrWriter, ctx.Log().GetLevel(), log.TimeFormat)))
412+
err = pipe.Run(ctx.WithLogger(log.NewStreamLoggerWithFormat(stdoutWriter, stderrWriter, ctx.Log().GetLevel(), log.TimeFormat)), args)
463413
if err != nil {
464414
return err
465415
}

e2e/tests/deploy/testdata/helm_concurrent_new/devspace.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ deployments:
1010
pipelines:
1111
deploy:
1212
run: |-
13-
echo '{"helm": {"values": {"containers": [{"image":"alpine"}]}}}' > ${devspace.tempFolder}/deployment.yaml
13+
echo '{"helm": {"values": {"containers": [{"image":"alpine"}]}}}' > ${DEVSPACE_TMPDIR}/deployment.yaml
1414
15-
create_deployments test1 test2 test3 test4 --from-file test1:${devspace.tempFolder}/deployment.yaml \
15+
create_deployments test1 test2 test3 test4 --from-file test1:${DEVSPACE_TMPDIR}/deployment.yaml \
1616
--from test2:base \
1717
--set test3:helm.values.containers[0].image=alpine \
1818
--set-string test4:helm.values.containers[0].image=alpine \

e2e/tests/imports/testdata/local/devspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pipelines:
2020
deploy:
2121
run: |-
2222
echo ${devspace.name} > name.txt
23-
echo ${devspace.tempFolder} > temp.txt
23+
echo ${DEVSPACE_TMPDIR} > temp.txt
2424
2525
run_dependency_pipelines --all > dependency.txt
2626

e2e/tests/imports/testdata/local/import3.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ pipelines:
1212
run: |-
1313
echo ${IMPORT3}
1414
echo ${devspace.name} > dependency-name.txt
15-
echo ${devspace.tempFolder} > dependency-temp.txt
15+
echo ${DEVSPACE_TMPDIR} > dependency-temp.txt

0 commit comments

Comments
 (0)