forked from cnabio/cnab-go
/
command.go
105 lines (87 loc) · 2.88 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package command
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/deislabs/cnab-go/driver"
)
// Driver relies upon a system command to provide a driver implementation
type Driver struct {
Name string
}
// Run executes the command
func (d *Driver) Run(op *driver.Operation) (driver.OperationResult, error) {
return d.exec(op)
}
// Handles executes the driver with `--handles` and parses the results
func (d *Driver) Handles(dt string) bool {
out, err := exec.Command(d.cliName(), "--handles").CombinedOutput()
if err != nil {
fmt.Printf("%s --handles: %s", d.cliName(), err)
return false
}
types := strings.Split(string(out), ",")
for _, tt := range types {
if dt == strings.TrimSpace(tt) {
return true
}
}
return false
}
func (d *Driver) cliName() string {
return "cnab-" + strings.ToLower(d.Name)
}
func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
// We need to do two things here: We need to make it easier for the
// command to access data, and we need to make it easy for the command
// to pass that data on to the image it invokes. So we do some data
// duplication.
// Construct an environment for the subprocess by cloning our
// environment and adding in all the extra env vars.
pairs := os.Environ()
added := []string{}
for k, v := range op.Environment {
pairs = append(pairs, fmt.Sprintf("%s=%s", k, v))
added = append(added, k)
}
// CNAB_VARS is a list of variables we added to the env. This is to make
// it easier for shell script drivers to clone the env vars.
pairs = append(pairs, fmt.Sprintf("CNAB_VARS=%s", strings.Join(added, ",")))
data, err := json.Marshal(op)
if err != nil {
return driver.OperationResult{}, err
}
args := []string{}
cmd := exec.Command(d.cliName(), args...)
cmd.Dir, err = os.Getwd()
if err != nil {
return driver.OperationResult{}, err
}
cmd.Env = pairs
cmd.Stdin = bytes.NewBuffer(data)
// Make stdout and stderr from driver available immediately
stdout, err := cmd.StdoutPipe()
if err != nil {
return driver.OperationResult{}, fmt.Errorf("Setting up output handling for driver (%s) failed: %v", d.Name, err)
}
go func() {
// Errors not handled here as they only prevent output from the driver being shown, errors in the command execution are handled when command is executed
io.Copy(op.Out, stdout)
}()
stderr, err := cmd.StderrPipe()
if err != nil {
return driver.OperationResult{}, fmt.Errorf("Setting up error output handling for driver (%s) failed: %v", d.Name, err)
}
go func() {
// Errors not handled here as they only prevent output from the driver being shown, errors in the command execution are handled when command is executed
io.Copy(op.Out, stderr)
}()
if err = cmd.Start(); err != nil {
return driver.OperationResult{}, fmt.Errorf("Start of driver (%s) failed: %v", d.Name, err)
}
return driver.OperationResult{}, cmd.Wait()
}