-
Notifications
You must be signed in to change notification settings - Fork 4
/
yml2env.go
198 lines (161 loc) · 3.72 KB
/
yml2env.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"github.com/EngineerBetter/yml2env/env"
"gopkg.in/yaml.v2"
)
var usage = "yml2env <YAML file> [<command> | --env]"
func main() {
args := os.Args
if isVersionCMD(args) {
fmt.Fprintln(os.Stdout, version())
os.Exit(0)
}
if len(args) < 3 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(1)
}
yamlPath := args[1]
if !fileExists(yamlPath) {
fmt.Fprintln(os.Stderr, yamlPath+" does not exist")
os.Exit(1)
}
bytes := loadYaml(yamlPath)
mapSlice := parseYaml(bytes)
mapSlice = uppercaseKeys(mapSlice)
envVars := os.Environ()
envVars = addToEnv(mapSlice, envVars)
if args[2] == "--eval" {
if len(args) > 3 {
fmt.Fprintln(os.Stderr, usage)
os.Exit(1)
}
printExports(mapSlice)
os.Exit(0)
} else {
err, _ := run(envVars, args[2:])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return false
}
return true
}
func loadYaml(yamlPath string) []byte {
bytes, err := ioutil.ReadFile(yamlPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Could not read "+yamlPath)
}
return bytes
}
func parseYaml(bytes []byte) yaml.MapSlice {
vars := yaml.MapSlice{}
err := yaml.Unmarshal([]byte(bytes), &vars)
if err != nil {
fmt.Fprintln(os.Stderr, "Could not parse YAML")
os.Exit(1)
}
return vars
}
func valueToString(item yaml.MapItem) yaml.MapItem {
if value, ok := item.Value.(bool); ok {
item.Value = strconv.FormatBool(value)
} else if value, ok := item.Value.(int); ok {
item.Value = strconv.Itoa(value)
}
return item
}
func uppercaseKeys(mapSlice yaml.MapSlice) yaml.MapSlice {
for i := 0; i < len(mapSlice); i++ {
item := mapSlice[i]
if key, ok := item.Key.(string); ok {
key := strings.ToUpper(key)
item = valueToString(item)
if value, ok := item.Value.(string); ok {
mapSlice[i] = yaml.MapItem{Key: key, Value: value}
} else {
fmt.Fprintln(os.Stderr, "YAML invalid")
os.Exit(1)
}
} else {
fmt.Fprintln(os.Stderr, "YAML invalid")
os.Exit(1)
}
}
return mapSlice
}
func addToEnv(mapSlice yaml.MapSlice, envVars []string) []string {
for i := 0; i < len(mapSlice); i++ {
item := mapSlice[i]
key, _ := item.Key.(string)
item = valueToString(item)
value, _ := item.Value.(string)
envVars = env.Set(key, value, envVars)
}
return envVars
}
func commandWithEnv(envVars []string, args ...string) *exec.Cmd {
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = envVars
return cmd
}
func run(envVars []string, args []string) (error, int) {
cmd := commandWithEnv(envVars, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return err, -1
}
err = cmd.Wait()
return nil, determineExitCode(cmd, err)
}
func determineExitCode(cmd *exec.Cmd, err error) (exitCode int) {
status := cmd.ProcessState.Sys().(syscall.WaitStatus)
if status.Signaled() {
exitCode = 128 + int(status.Signal())
} else {
exitStatus := status.ExitStatus()
if exitStatus == -1 && err != nil {
exitCode = 254
}
exitCode = exitStatus
}
return
}
func printExports(mapSlice yaml.MapSlice) {
for i := 0; i < len(mapSlice); i++ {
item := mapSlice[i]
key, _ := item.Key.(string)
key = strings.ToUpper(key)
item = valueToString(item)
value, _ := item.Value.(string)
fmt.Printf("export '%s=%s'\n", key, value)
}
}
func version() string {
data, err := os.ReadFile("version")
if err != nil {
fmt.Printf("unable to retrieve version: %s", err)
}
return string(data)
}
func isVersionCMD(args []string) bool {
if len(args) > 1 && (args[1] == "--version" || args[1] == "-v") {
return true
}
return false
}