-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
51 lines (46 loc) · 1.46 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
package main
import (
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"gopkg.in/alecthomas/kingpin.v2"
)
func main() {
log.SetFlags(log.Ltime | log.Lshortfile)
app := kingpin.New(filepath.Base(os.Args[0]), `
A simple tool that reads all env variables and writes each one to a file.
Useful in environments where the only way to add configuratios is through env variables and
need to use docker images which accept configs only through files.
`)
app.HelpFlag.Short('h')
dest := app.Flag("dir", "destination to which it should write the files").
Short('d').Required().ExistingDir()
kingpin.MustParse(app.Parse(os.Args[1:]))
for _, env := range os.Environ() {
d := strings.SplitN(env, "=", 2)
// Any underscore in a variable with a special suffix should be converted into a dot.
sfxs := []string{"_sh", "_xml", "_conf"}
for _, sfx := range sfxs {
if strings.HasSuffix(d[0], sfx) {
d[0] = strings.TrimSuffix(d[0], sfx) + strings.Replace(sfx, "_", ".", 1)
}
}
destPath := filepath.Join(*dest, d[0])
expanded := os.ExpandEnv(d[1])
err := ioutil.WriteFile(destPath, []byte(expanded), 0644)
if err != nil {
log.Printf("could not write env var:%v to:%v \n", d[0], destPath)
}
log.Printf("wrote env var:%v to:%v \n", d[0], destPath)
if os.Getenv("DEBUG") != "" {
log.Printf("env content:%v\n", d[1])
}
}
exitSignal := make(chan os.Signal)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
<-exitSignal
}