-
Notifications
You must be signed in to change notification settings - Fork 683
/
helpers.go
65 lines (54 loc) · 1.15 KB
/
helpers.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
package entrypoint
import (
"context"
"encoding/json"
"os"
"strings"
"github.com/datawire/dlib/dexec"
)
func envbool(name string) bool {
return os.Getenv(name) != ""
}
func env(name, defaultValue string) string {
value := os.Getenv(name)
if value != "" {
return value
} else {
return defaultValue
}
}
func ensureDir(dirname string) error {
err := os.MkdirAll(dirname, 0700)
if err != nil && os.IsExist(err) {
err = nil
}
return err
}
func cidsForLabel(ctx context.Context, label string) ([]string, error) {
bs, err := dexec.CommandContext(ctx, "docker", "ps", "-q", "-f", "label="+label).CombinedOutput()
if err != nil {
return nil, err
}
return strings.Fields(string(bs)), nil
}
func subcommand(ctx context.Context, command string, args ...string) *dexec.Cmd {
cmd := dexec.CommandContext(ctx, command, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func convert(in interface{}, out interface{}) error {
if out == nil {
return nil
}
jsonBytes, err := json.Marshal(in)
if err != nil {
return err
}
err = json.Unmarshal(jsonBytes, out)
if err != nil {
return err
}
return nil
}