-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
55 lines (44 loc) · 904 Bytes
/
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
package switchboard
import (
"bufio"
"bytes"
"io"
"log"
"regexp"
)
var (
isTag = regexp.MustCompile(`^([A-Z_]+)\:\ (.*)$`)
)
type Command struct {
Name string
Command string
Driver Driver
Image string
Inline string
}
func (command *Command) Execute(env []string, stdin io.Reader) (int64, Tags, io.Reader, error) {
var stdout, stderr bytes.Buffer
status, err := command.Driver.Execute(command, env, &Streams{stdin, &stdout, &stderr})
if err != nil {
return -1, nil, nil, err
}
err = LogStderr(&stderr)
if err != nil {
return -1, nil, nil, err
}
tags, rest, err := ParseTags(&stdout)
if err != nil {
return -1, nil, nil, err
}
return status, tags, rest, nil
}
func LogStderr(stderr io.Reader) error {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
log.Print(scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}