-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
72 lines (66 loc) · 1.34 KB
/
main.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
package main
import (
"bufio"
"flag"
"io/ioutil"
"log"
"os"
"os/exec"
)
// exitOnErr111 exits on err
func exitOnErr111(err error) {
if err != nil {
log.Println(err)
os.Exit(111)
}
}
func main() {
// custom help message
flag.Usage = func() {
printHelp()
}
// checking parameters and files
flag.Parse()
inParams := flag.Args()
exitOnErr111(checkInParams(inParams))
envDir := inParams[0]
envFiles, err := ioutil.ReadDir(envDir)
exitOnErr111(err)
exitOnErr111(checkEnvFiles(envFiles))
// populating env
currEnv := os.Environ()
for _, envFile := range envFiles {
envVarName := envFile.Name()
if envFile.Size() == 0 {
if varInEnv(currEnv, envVarName) {
os.Unsetenv(envVarName)
}
} else {
f, err := os.Open(envDir + "/" + envFile.Name())
if err != nil {
f.Close()
exitOnErr111(err)
}
r := bufio.NewReader(f)
envVarValue, err := readSingleLine(r)
if err != nil {
f.Close()
exitOnErr111(err)
}
os.Setenv(envVarName, cleanValue(envVarValue))
f.Close()
}
}
// running child command
childCommand := inParams[1]
childCommandParams := inParams[2:]
cmd := exec.Command(childCommand, childCommandParams...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Println(err)
}
// exit with childs command exit code
os.Exit(cmd.ProcessState.ExitCode())
}