Skip to content

Commit 70de6fb

Browse files
committed
feat: add run_command & refactor commands
1 parent 3744b31 commit 70de6fb

File tree

16 files changed

+117
-103
lines changed

16 files changed

+117
-103
lines changed

cmd/list/commands.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func (cmd *commandsCmd) RunListProfiles(f factory.Factory, cobraCmd *cobra.Comma
7070

7171
rows := [][]string{}
7272
for _, command := range commands {
73+
if command.Internal {
74+
continue
75+
}
76+
7377
rows = append(rows, []string{
7478
command.Name,
7579
command.Command,

cmd/list/vars.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"sort"
88

99
"github.com/loft-sh/devspace/cmd/flags"
10-
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
1110
"github.com/loft-sh/devspace/pkg/util/factory"
1211
"github.com/loft-sh/devspace/pkg/util/log"
1312
"github.com/loft-sh/devspace/pkg/util/message"
@@ -61,7 +60,7 @@ func (cmd *varsCmd) RunListVars(f factory.Factory, cobraCmd *cobra.Command, args
6160
}
6261

6362
// Fill variables config
64-
config, err := configLoader.LoadWithParser(context.Background(), nil, nil, loader.NewWithCommandsParser(), cmd.ToConfigOptions(), logger)
63+
config, err := configLoader.Load(context.Background(), nil, cmd.ToConfigOptions(), logger)
6564
if err != nil {
6665
return err
6766
}

cmd/run.go

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"github.com/loft-sh/devspace/pkg/devspace/config"
67
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
8+
"github.com/loft-sh/devspace/pkg/devspace/pipeline/engine"
9+
"github.com/loft-sh/devspace/pkg/util/command"
10+
"github.com/loft-sh/devspace/pkg/util/exit"
711
"io"
12+
"mvdan.cc/sh/v3/interp"
813
"os"
914
"strings"
1015

@@ -168,10 +173,10 @@ func (cmd *RunCmd) RunRun(f factory.Factory, args []string) error {
168173
if err != nil {
169174
return err
170175
}
171-
commands := commandsInterface.Config().Commands
172176

173177
// create context
174-
ctx := devspacecontext.NewContext(context.Background(), f.GetLog())
178+
ctx := devspacecontext.NewContext(context.Background(), f.GetLog()).
179+
WithConfig(commandsInterface)
175180

176181
// check if we should execute a dependency command
177182
if cmd.Dependency != "" {
@@ -191,7 +196,8 @@ func (cmd *RunCmd) RunRun(f factory.Factory, args []string) error {
191196
return fmt.Errorf("couldn't find dependency %s", cmd.Dependency)
192197
}
193198

194-
return dep.Command(ctx.Context, args[0], args[1:])
199+
ctx = ctx.AsDependency(dep)
200+
return ExecuteConfigCommand(ctx.Context, ctx.Config, args[0], args[1:], ctx.WorkingDir, cmd.Stdout, cmd.Stderr, os.Stdin)
195201
}
196202

197203
// Save variables
@@ -201,7 +207,56 @@ func (cmd *RunCmd) RunRun(f factory.Factory, args []string) error {
201207
}
202208

203209
// Execute command
204-
return dependency.ExecuteCommand(ctx.Context, commands, args[0], args[1:], ctx.WorkingDir, cmd.Stdout, cmd.Stderr, os.Stdin)
210+
return ExecuteConfigCommand(ctx.Context, ctx.Config, args[0], args[1:], ctx.WorkingDir, cmd.Stdout, cmd.Stderr, os.Stdin)
211+
}
212+
213+
// ExecuteConfigCommand executes a command from the config
214+
func ExecuteConfigCommand(ctx context.Context, config config.Config, name string, args []string, dir string, stdout io.Writer, stderr io.Writer, stdin io.Reader) error {
215+
shellCommand := ""
216+
var shellArgs []string
217+
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+
}
225+
}
226+
227+
extraEnv := map[string]string{}
228+
for k, v := range config.Variables() {
229+
extraEnv[k] = fmt.Sprintf("%v", v)
230+
}
231+
232+
if shellCommand == "" {
233+
return errors.Errorf("couldn't find command '%s' in devspace config", name)
234+
}
235+
if shellArgs == nil {
236+
if appendArgs {
237+
// Append args to shell command
238+
for _, arg := range args {
239+
arg = strings.Replace(arg, "'", "'\"'\"'", -1)
240+
241+
shellCommand += " '" + arg + "'"
242+
}
243+
}
244+
245+
// execute the command in a shell
246+
err := engine.ExecuteSimpleShellCommand(ctx, dir, stdout, stderr, stdin, nil, shellCommand, args...)
247+
if err != nil {
248+
if status, ok := interp.IsExitStatus(err); ok {
249+
return &exit.ReturnCodeError{
250+
ExitCode: int(status),
251+
}
252+
}
253+
254+
return errors.Wrap(err, "execute command")
255+
}
256+
}
257+
258+
shellArgs = append(shellArgs, args...)
259+
return command.CommandWithEnv(ctx, dir, stdout, stderr, stdin, nil, shellCommand, shellArgs...)
205260
}
206261

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

examples/pipelines/devspace.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ dependencies:
66
source:
77
path: dep2.yaml
88

9+
commands:
10+
test2:
11+
command: |
12+
build_images tes
13+
test:
14+
command: |
15+
run_command test2 abc
16+
917
pipelines:
1018
helper:
1119
steps:
@@ -14,7 +22,7 @@ pipelines:
1422
dev:
1523
steps:
1624
- run: |-
17-
sleep 30
25+
SWAG=fabi run_command test abc
1826
#run_dependency_pipelines --all --pipeline dev
1927
2028
#export TEST=$(run_pipelines helper)

pkg/devspace/command/command.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1 @@
11
package command
2-
3-
import (
4-
"context"
5-
"github.com/loft-sh/devspace/pkg/devspace/pipeline/engine"
6-
"io"
7-
"strings"
8-
9-
"github.com/loft-sh/devspace/pkg/util/command"
10-
11-
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
12-
13-
"github.com/pkg/errors"
14-
)
15-
16-
// ExecuteCommand executes a command from the config
17-
func ExecuteCommand(ctx context.Context, commands map[string]*latest.CommandConfig, name string, args []string, dir string, stdout io.Writer, stderr io.Writer, stdin io.Reader) error {
18-
shellCommand := ""
19-
var shellArgs []string
20-
var appendArgs bool
21-
for _, cmd := range commands {
22-
if cmd.Name == name {
23-
shellCommand = cmd.Command
24-
shellArgs = cmd.Args
25-
appendArgs = cmd.AppendArgs
26-
break
27-
}
28-
}
29-
30-
if shellCommand == "" {
31-
return errors.Errorf("couldn't find command '%s' in devspace config", name)
32-
}
33-
34-
if shellArgs == nil {
35-
if appendArgs {
36-
// Append args to shell command
37-
for _, arg := range args {
38-
arg = strings.Replace(arg, "'", "'\"'\"'", -1)
39-
40-
shellCommand += " '" + arg + "'"
41-
}
42-
}
43-
44-
// execute the command in a shell
45-
return engine.ExecuteSimpleShellCommand(ctx, dir, stdout, stderr, stdin, nil, shellCommand, args...)
46-
}
47-
48-
shellArgs = append(shellArgs, args...)
49-
return command.Command(ctx, dir, stdout, stderr, stdin, shellCommand, shellArgs...)
50-
}

pkg/devspace/config/loader/parser.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ type defaultParser struct{}
2323

2424
func (d *defaultParser) Parse(ctx context.Context, originalRawConfig map[string]interface{}, rawConfig map[string]interface{}, resolver variable.Resolver, log log.Logger) (*latest.Config, map[string]interface{}, error) {
2525
// delete the commands, since we don't need it in a normal scenario
26-
delete(rawConfig, "commands")
27-
28-
return fillVariablesAndParse(ctx, resolver, rawConfig, log)
29-
}
30-
31-
func NewWithCommandsParser() Parser {
32-
return &withCommandsParser{}
33-
}
34-
35-
type withCommandsParser struct{}
36-
37-
func (d *withCommandsParser) Parse(ctx context.Context, originalRawConfig map[string]interface{}, rawConfig map[string]interface{}, resolver variable.Resolver, log log.Logger) (*latest.Config, map[string]interface{}, error) {
3826
return fillVariablesAndParse(ctx, resolver, rawConfig, log)
3927
}
4028

@@ -111,9 +99,9 @@ func fillVariablesAndParse(ctx context.Context, resolver variable.Resolver, prep
11199
return latestConfig, preparedConfigInterface.(map[string]interface{}), nil
112100
}
113101

114-
func Convert(prepatedConfig map[string]interface{}, log log.Logger) (*latest.Config, error) {
102+
func Convert(preparedConfig map[string]interface{}, log log.Logger) (*latest.Config, error) {
115103
// Now convert the whole config to latest
116-
latestConfig, err := versions.Parse(prepatedConfig, log)
104+
latestConfig, err := versions.Parse(preparedConfig, log)
117105
if err != nil {
118106
return nil, errors.Wrap(err, "convert config")
119107
}

pkg/devspace/config/loader/variable/runtime/runtime_variable.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var Locations = []string{
2828
"/dev/replacePods/*/replaceImage",
2929
"/dev/terminal/imageSelector",
3030
"/pipelines/**",
31+
"/commands/**",
3132
}
3233

3334
// NewRuntimeVariable creates a new variable that is loaded during runtime

pkg/devspace/dependency/dependency.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package dependency
22

33
import (
4-
"context"
54
"github.com/loft-sh/devspace/pkg/devspace/config"
65
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
76
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
87
"github.com/loft-sh/devspace/pkg/devspace/dependency/types"
98
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
10-
"os"
119
)
1210

1311
// Dependency holds the dependency config and has an id
@@ -41,10 +39,6 @@ func (d *Dependency) DependencyConfig() *latest.DependencyConfig { return d.depe
4139

4240
func (d *Dependency) Children() []types.Dependency { return d.children }
4341

44-
func (d *Dependency) Command(ctx context.Context, command string, args []string) error {
45-
return ExecuteCommand(ctx, d.localConfig.Config().Commands, command, args, d.absolutePath, os.Stdout, os.Stderr, os.Stdin)
46-
}
47-
4842
func skipDependency(name string, skipDependencies []string) bool {
4943
for _, sd := range skipDependencies {
5044
if sd == name {

pkg/devspace/dependency/manager.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@ package dependency
22

33
import (
44
"bytes"
5-
"context"
65
"github.com/loft-sh/devspace/pkg/devspace/build"
7-
"github.com/loft-sh/devspace/pkg/devspace/command"
86
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
9-
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
107
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
118
"github.com/loft-sh/devspace/pkg/devspace/dependency/types"
129
"github.com/loft-sh/devspace/pkg/devspace/hook"
1310
"github.com/loft-sh/devspace/pkg/devspace/plugin"
14-
"github.com/loft-sh/devspace/pkg/util/exit"
1511
"github.com/loft-sh/devspace/pkg/util/log"
1612
"github.com/pkg/errors"
1713
"github.com/sirupsen/logrus"
18-
"io"
19-
"mvdan.cc/sh/v3/interp"
2014
"strings"
2115
)
2216

@@ -53,22 +47,6 @@ func (m *manager) ResolveAll(ctx *devspacecontext.Context, options ResolveOption
5347
return dependencies, nil
5448
}
5549

56-
// ExecuteCommand executes a given command from the available commands
57-
func ExecuteCommand(ctx context.Context, commands map[string]*latest.CommandConfig, cmd string, args []string, dir string, stdout io.Writer, stderr io.Writer, stdin io.Reader) error {
58-
err := command.ExecuteCommand(ctx, commands, cmd, args, dir, stdout, stderr, stdin)
59-
if err != nil {
60-
if status, ok := interp.IsExitStatus(err); ok {
61-
return &exit.ReturnCodeError{
62-
ExitCode: int(status),
63-
}
64-
}
65-
66-
return errors.Wrap(err, "execute command")
67-
}
68-
69-
return nil
70-
}
71-
7250
// BuildOptions has all options for building all dependencies
7351
type BuildOptions struct {
7452
BuildOptions build.Options

pkg/devspace/dependency/resolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (r *resolver) resolveDependency(ctx *devspacecontext.Context, dependencyCon
203203
return err
204204
}
205205

206-
dConfigWrapper, err = configLoader.LoadWithParser(ctx.Context, nil, client, loader.NewWithCommandsParser(), cloned, ctx.Log)
206+
dConfigWrapper, err = configLoader.Load(ctx.Context, client, cloned, ctx.Log)
207207
if err != nil {
208208
return errors.Wrap(err, fmt.Sprintf("loading config for dependency %s", dependencyName))
209209
}

0 commit comments

Comments
 (0)