Skip to content

Commit d754ea6

Browse files
committed
fix: fixed a command issue
1 parent 70de6fb commit d754ea6

File tree

2 files changed

+47
-61
lines changed

2 files changed

+47
-61
lines changed

cmd/run.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,23 +215,19 @@ func ExecuteConfigCommand(ctx context.Context, config config.Config, name string
215215
shellCommand := ""
216216
var shellArgs []string
217217
var appendArgs bool
218-
for _, cmd := range config.Config().Commands {
219-
if cmd.Name == name {
220-
shellCommand = cmd.Command
221-
shellArgs = cmd.Args
222-
appendArgs = cmd.AppendArgs
223-
break
224-
}
218+
if config.Config().Commands == nil || config.Config().Commands[name] == nil {
219+
return errors.Errorf("couldn't find command '%s' in devspace config", name)
225220
}
226221

222+
cmd := config.Config().Commands[name]
223+
shellCommand = strings.TrimSpace(cmd.Command)
224+
shellArgs = cmd.Args
225+
appendArgs = cmd.AppendArgs
226+
227227
extraEnv := map[string]string{}
228228
for k, v := range config.Variables() {
229229
extraEnv[k] = fmt.Sprintf("%v", v)
230230
}
231-
232-
if shellCommand == "" {
233-
return errors.Errorf("couldn't find command '%s' in devspace config", name)
234-
}
235231
if shellArgs == nil {
236232
if appendArgs {
237233
// Append args to shell command
@@ -243,7 +239,7 @@ func ExecuteConfigCommand(ctx context.Context, config config.Config, name string
243239
}
244240

245241
// execute the command in a shell
246-
err := engine.ExecuteSimpleShellCommand(ctx, dir, stdout, stderr, stdin, nil, shellCommand, args...)
242+
err := engine.ExecuteSimpleShellCommand(ctx, dir, stdout, stderr, stdin, extraEnv, shellCommand, args...)
247243
if err != nil {
248244
if status, ok := interp.IsExitStatus(err); ok {
249245
return &exit.ReturnCodeError{
@@ -253,10 +249,12 @@ func ExecuteConfigCommand(ctx context.Context, config config.Config, name string
253249

254250
return errors.Wrap(err, "execute command")
255251
}
252+
253+
return nil
256254
}
257255

258256
shellArgs = append(shellArgs, args...)
259-
return command.CommandWithEnv(ctx, dir, stdout, stderr, stdin, nil, shellCommand, shellArgs...)
257+
return command.CommandWithEnv(ctx, dir, stdout, stderr, stdin, extraEnv, shellCommand, shellArgs...)
260258
}
261259

262260
func getCommands(f factory.Factory) (map[string]*latest.CommandConfig, error) {

pkg/devspace/pipeline/engine/basichandler/handler.go

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,68 +20,56 @@ func NewBasicExecHandler() types.ExecHandler {
2020
type execHandler struct{}
2121

2222
func (e *execHandler) ExecHandler(ctx context.Context, args []string) error {
23-
if len(args) > 0 {
23+
if len(args) == 0 {
2424
// handle some special commands that are not found locally
2525
hc := interp.HandlerCtx(ctx)
2626
_, err := lookPathDir(hc.Dir, hc.Env, args[0])
2727
if err != nil {
28-
err = e.fallbackCommands(ctx, args[0], args[1:])
29-
if err != nil {
30-
return err
28+
logger := log.GetFileLogger("shell")
29+
hc := interp.HandlerCtx(ctx)
30+
switch args[0] {
31+
case "is_equal":
32+
return enginecommands.IsEqual(&hc, args[1:])
33+
case "is_command":
34+
return enginecommands.IsCommand(args[1:])
35+
case "sleep":
36+
return handleError(hc, enginecommands.Sleep(args[1:]))
37+
case "cat":
38+
return handleError(hc, enginecommands.Cat(&hc, args[1:]))
39+
case "kubectl":
40+
path, err := downloader.NewDownloader(commands.NewKubectlCommand(), logger).EnsureCommand(ctx)
41+
if err != nil {
42+
_, _ = fmt.Fprintln(hc.Stderr, err)
43+
return interp.NewExitStatus(127)
44+
}
45+
args[0] = path
46+
case "helm":
47+
path, err := downloader.NewDownloader(commands.NewHelmV3Command(), logger).EnsureCommand(ctx)
48+
if err != nil {
49+
_, _ = fmt.Fprintln(hc.Stderr, err)
50+
return interp.NewExitStatus(127)
51+
}
52+
args[0] = path
53+
case "devspace":
54+
bin, err := os.Executable()
55+
if err != nil {
56+
_, _ = fmt.Fprintln(hc.Stderr, err)
57+
return interp.NewExitStatus(1)
58+
}
59+
args[0] = bin
3160
}
3261
}
3362
}
3463

3564
return interp.DefaultExecHandler(2*time.Second)(ctx, args)
3665
}
3766

38-
func (e *execHandler) executePipelineCommand(ctx context.Context, command string) (bool, error) {
39-
hc := interp.HandlerCtx(ctx)
40-
_, _ = fmt.Fprintln(hc.Stderr, fmt.Errorf("%s: cannot execute the command because it can only be executed within a pipeline step", command))
41-
return true, interp.NewExitStatus(1)
42-
}
43-
44-
func (e *execHandler) fallbackCommands(ctx context.Context, command string, args []string) error {
45-
logger := log.GetFileLogger("shell")
46-
hc := interp.HandlerCtx(ctx)
47-
48-
switch command {
49-
case "is_equal":
50-
return enginecommands.IsEqual(&hc, args)
51-
case "is_command":
52-
return enginecommands.IsCommand(args)
53-
case "sleep":
54-
return handleError(hc, enginecommands.Sleep(args))
55-
case "cat":
56-
return handleError(hc, enginecommands.Cat(&hc, args))
57-
case "kubectl":
58-
path, err := downloader.NewDownloader(commands.NewKubectlCommand(), logger).EnsureCommand(ctx)
59-
if err != nil {
60-
_, _ = fmt.Fprintln(hc.Stderr, err)
61-
return interp.NewExitStatus(127)
62-
}
63-
command = path
64-
case "helm":
65-
path, err := downloader.NewDownloader(commands.NewHelmV3Command(), logger).EnsureCommand(ctx)
66-
if err != nil {
67-
_, _ = fmt.Fprintln(hc.Stderr, err)
68-
return interp.NewExitStatus(127)
69-
}
70-
command = path
71-
case "devspace":
72-
bin, err := os.Executable()
73-
if err != nil {
74-
_, _ = fmt.Fprintln(hc.Stderr, err)
75-
return interp.NewExitStatus(1)
76-
}
77-
command = bin
78-
}
79-
return nil
80-
}
81-
8267
func handleError(hc interp.HandlerContext, err error) error {
8368
if err != nil {
8469
_, _ = fmt.Fprintln(hc.Stderr, err)
70+
if _, ok := interp.IsExitStatus(err); ok {
71+
return err
72+
}
8573
return interp.NewExitStatus(1)
8674
}
8775
return interp.NewExitStatus(0)

0 commit comments

Comments
 (0)