@@ -3,8 +3,13 @@ package cmd
33import (
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
207262func getCommands (f factory.Factory ) (map [string ]* latest.CommandConfig , error ) {
0 commit comments