mirror of
https://github.com/Luzifer/vault2env.git
synced 2024-11-09 08:40:06 +00:00
Add transform feature to rename keys from Vault
This commit is contained in:
parent
be03ca6e8e
commit
c6eee2ac53
1 changed files with 20 additions and 6 deletions
22
main.go
22
main.go
|
@ -29,6 +29,7 @@ var (
|
|||
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"`
|
||||
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)
|
||||
}
|
||||
|
||||
if cfg.Export {
|
||||
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 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)")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue