-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcinful.go
97 lines (85 loc) · 1.63 KB
/
cinful.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
package cinful
import (
_ "embed"
"encoding/json"
"fmt"
"os"
)
//go:embed vendors.json
var b []byte
func init() {
err := json.Unmarshal(b, &vendors)
if err != nil {
panic(err)
}
}
var vendors []Vendor
type Vendor struct {
Name string `json:"name,omitempty"`
Constant string `json:"constant,omitempty"`
Env any `json:"env,omitempty"`
PR any `json:"pr,omitempty"`
Val string `json:"val,omitempty"`
}
func (v *Vendor) String() string {
vfmt := "Name: %s\nConst: %s\nEnv: %v\nVal: %v"
return fmt.Sprintf(vfmt, v.Name, v.Constant, v.Env, v.Val)
}
func PrintVendors() {
for _, v := range vendors {
fmt.Printf("%#v\n", v)
}
}
func Info() *Vendor {
// check vendor first, for more details
for _, v := range vendors {
switch vt := v.Env.(type) {
// normally just a string
case string:
val := os.Getenv(vt)
if val != "" {
v.Val = val
return &v
}
// a list of strings
case []interface {}:
for _, ev := range vt {
val := os.Getenv(ev.(string))
if val != "" {
v.Env = ev.(string)
v.Val = val
return &v
}
}
// an ENV var with a specific value
case map[string]interface {}:
for ek, ev := range vt {
val := os.Getenv(ek)
if val == ev.(string) {
v.Env = ek
v.Val, _ = ev.(string)
return &v
}
}
}
}
// check some common ENV vars
for _, ce := range commonEnv {
val := os.Getenv(ce)
if val != "" {
return &Vendor{
Name: "Common Var",
Constant: "COMMON",
Env: ce,
Val: val,
}
}
}
return nil
}
var commonEnv = []string{
"CI",
"CONTINUOUS_INTEGRATION",
"BUILD_NUMBER",
"RUN_ID",
}