11package task
22
33import (
4- "errors"
54 "fmt"
65 "os"
76 "os/exec"
7+
8+ "github.com/pkg/errors"
89)
910
1011// ExecutionTask encodes a Target with additional execution-time information.
@@ -70,15 +71,20 @@ func (t *Target) Execute(dir string, env map[string]string, shutdown bool, inher
7071 command = t .Up
7172 }
7273
73- return execute (dir , env , command , inheritEnv )
74+ c , err := prepare (dir , env , command , inheritEnv )
75+ if err != nil {
76+ return errors .Wrap (err , "failed to prepare command for execution" )
77+ }
78+
79+ return c .Run ()
7480}
7581
76- func execute (dir string , env map [string ]string , command []string , inheritEnv bool ) (err error ) {
82+ func prepare (dir string , env map [string ]string , command []string , inheritEnv bool ) (cmd * exec. Cmd , err error ) {
7783 if len (command ) == 0 {
78- return errors .New ("attempt to execute target with empty command" )
84+ return nil , errors .New ("attempt to execute target with empty command" )
7985 }
8086
81- cmd : = exec .Command (command [0 ])
87+ cmd = exec .Command (command [0 ])
8288 if len (command ) > 1 {
8389 cmd .Args = append (cmd .Args , command [1 :]... )
8490 }
@@ -91,9 +97,9 @@ func execute(dir string, env map[string]string, command []string, inheritEnv boo
9197 cmdEnv = os .Environ ()
9298 }
9399 for k , v := range env {
94- cmdEnv = append (cmd . Env , fmt .Sprintf ("%s=%s" , k , v ))
100+ cmdEnv = append (cmdEnv , fmt .Sprintf ("%s=%s" , k , v ))
95101 }
96102 cmd .Env = cmdEnv
97103
98- return cmd . Run ()
104+ return cmd , nil
99105}
0 commit comments