-
Notifications
You must be signed in to change notification settings - Fork 685
/
helpers.go
96 lines (80 loc) · 1.98 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
package entrypoint
import (
"context"
"encoding/json"
"os"
"strings"
amb "github.com/datawire/ambassador/v2/pkg/api/getambassador.io/v3alpha1"
"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
}
// 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
}