1
0
Fork 0
mirror of https://github.com/Luzifer/vault2env.git synced 2024-12-20 20:21:20 +00:00

Add transform feature to rename keys from Vault

This commit is contained in:
Knut Ahlers 2016-10-04 13:12:36 +02:00
parent be03ca6e8e
commit c6eee2ac53
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

26
main.go
View file

@ -28,8 +28,9 @@ var (
TokenAuth struct {
Token string `flag:"vault-token" env:"VAULT_TOKEN" vardefault:"vault-token" description:"Specify a token to use instead of app-id auth"`
}
Export bool `flag:"export,e" default:"false" description:"Show export statements instead of running the command specified"`
VersionAndExit bool `flag:"version" default:"false" description:"Print program version and exit"`
Export bool `flag:"export,e" default:"false" description:"Show export statements instead of running the command specified"`
Transform []string `flag:"transform,t" default:"" description:"Translates keys to different names (oldkey=newkey)"`
VersionAndExit bool `flag:"version" default:"false" description:"Print program version and exit"`
}{}
version = "dev"
)
@ -122,23 +123,36 @@ func main() {
log.Fatalf("Unable to fetch data: %s", err)
}
transformMap := env.ListToMap(cfg.Transform)
envData := map[string]string{}
for k, v := range data.Data {
key := k
if newKey, ok := transformMap[key]; ok {
key = newKey
}
envData[key] = v.(string)
}
if cfg.Export {
for k, v := range data.Data {
for k, v := range envData {
fmt.Printf("export %s=\"%s\"\n", k, v)
}
return
}
emap := env.ListToMap(os.Environ())
for k, v := range data.Data {
emap[k] = v.(string)
for k, v := range emap {
if _, ok := envData[k]; !ok {
envData[k] = v
}
}
cmd := exec.Command(rconfig.Args()[2], rconfig.Args()[3:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = env.MapToList(emap)
cmd.Env = env.MapToList(envData)
if err := cmd.Run(); err != nil {
log.Fatal("Command exitted unclean (code != 0)")
}