1
1
package task
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
6
5
"os"
7
6
"os/exec"
7
+
8
+ "github.com/pkg/errors"
8
9
)
9
10
10
11
// 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
70
71
command = t .Up
71
72
}
72
73
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 ()
74
80
}
75
81
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 ) {
77
83
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" )
79
85
}
80
86
81
- cmd : = exec .Command (command [0 ])
87
+ cmd = exec .Command (command [0 ])
82
88
if len (command ) > 1 {
83
89
cmd .Args = append (cmd .Args , command [1 :]... )
84
90
}
@@ -91,9 +97,9 @@ func execute(dir string, env map[string]string, command []string, inheritEnv boo
91
97
cmdEnv = os .Environ ()
92
98
}
93
99
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 ))
95
101
}
96
102
cmd .Env = cmdEnv
97
103
98
- return cmd . Run ()
104
+ return cmd , nil
99
105
}
0 commit comments