-
Notifications
You must be signed in to change notification settings - Fork 682
/
helpers.go
143 lines (121 loc) · 3.03 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package entrypoint
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
amb "github.com/datawire/ambassador/v2/pkg/api/getambassador.io/v2"
"github.com/datawire/dlib/dexec"
"github.com/datawire/dlib/dlog"
)
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) {
if !fileExists(dirname) {
err := os.MkdirAll(dirname, 0700)
if err != nil {
panic(err)
}
}
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func sh(ctx context.Context, command string, args ...string) string {
cmd := dexec.CommandContext(ctx, command, args...)
out, err := cmd.CombinedOutput()
panicExecError(fmt.Sprintf("error executing command %s %v", command, args), err)
return string(out)
}
func cidsForLabel(ctx context.Context, label string) []string {
return strings.Fields(sh(ctx, "docker", "ps", "-q", "-f", fmt.Sprintf("label=%s", label)))
}
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 panicExecError(prefix string, err error) {
if err == nil {
return
}
msg := fmt.Sprintf("%s: %v", prefix, err)
if exerr, ok := err.(*dexec.ExitError); ok {
if exerr.Success() {
return
}
msg = fmt.Sprintf("%s\n%s", msg, string(exerr.Stderr))
}
panic(msg)
}
func logExecError(ctx context.Context, prefix string, err error) {
if err == nil {
return
}
msg := fmt.Sprintf("%s: %v", prefix, err)
if exerr, ok := err.(*dexec.ExitError); ok {
if exerr.Success() {
return
}
dlog.Errorf(ctx, "%s\n%s", msg, string(exerr.Stderr))
} else {
// This means we didn't even start the subcommand, so this is a programming error, not a
// runtime error and we want to panic in this case.
panic(msg)
}
}
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
}
// Should we pay attention to a given AmbassadorID set?
//
// XXX Yes, amb.AmbassadorID is a singular name for a plural type. Sigh.
func include(id amb.AmbassadorID) bool {
// We always pay attention to the "_automatic_" ID -- it gives us a
// to easily always include certain configuration resources for Edge
// Stack.
if len(id) == 1 && id[0] == "_automatic_" {
return true
}
// It's not "_automatic_", so we have to actually do the work. Grab
// our AmbassadorID...
me := GetAmbassadorId()
// ...force an empty AmbassadorID to "default", per the documentation...
if len(id) == 0 {
id = amb.AmbassadorID{"default"}
}
// ...and then see if our AmbassadorID is in the list.
for _, name := range id {
if me == name {
return true
}
}
return false
}