-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (58 loc) · 1.3 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
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"io/ioutil"
"k8s.io/kubernetes/third_party/golang/expansion"
"os"
"os/exec"
"strings"
)
func loadEnv(secretsDir string) {
files, _ := ioutil.ReadDir(secretsDir)
for _, f := range files {
filePath := secretsDir + "/" + f.Name()
content, _ := ioutil.ReadFile(filePath)
envValue := strings.TrimSpace(string(content))
envName := strings.ToUpper(strings.Replace(f.Name(), "-", "_", -1))
os.Setenv(envName, envValue)
// env = append(env, fmt.Sprintf("%s=%s", envName, content_str))
fmt.Printf("Setting env variable: %s from %s\n", envName, filePath)
}
}
func expandOsArgs() []string {
args := []string{}
if len(os.Args) != 0 {
for _, cmd := range os.Args {
args = append(args, expansion.Expand(cmd, os.Getenv))
}
}
return args
}
func runCmd() error {
args := expandOsArgs()
cmdCmd := args[1]
cmdArgs := []string{}
if len(args) > 2 {
cmdArgs = args[2:]
}
cmd := exec.Command(cmdCmd, cmdArgs...)
env := os.Environ()
cmd.Env = env
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
return err
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Need at least 1 argument - command name")
os.Exit(1)
}
dir := "/secrets"
loadEnv(dir)
err := runCmd()
if err != nil {
fmt.Printf("Error: %v\n", err)
}
}