forked from miquella/vaulted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
53 lines (45 loc) · 1.02 KB
/
env.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
package main
import (
"fmt"
"os"
"sort"
"strings"
)
type Env struct {
VaultName string
Shell string
UsageHint bool
}
func (e *Env) Run(steward Steward) error {
_, env, err := steward.GetEnvironment(e.VaultName, nil)
if err != nil {
return err
}
usageHint := ""
setVar := ""
quoteReplacement := "\""
switch e.Shell {
case "fish":
usageHint = "# To load these variables into your shell, execute:\n# eval (%s)\n"
setVar = "set -x %s \"%s\";\n"
quoteReplacement = "\\\""
default:
usageHint = "# To load these variables into your shell, execute:\n# eval $(%s)\n"
setVar = "export %s=\"%s\"\n"
quoteReplacement = "\\\""
}
// sort the vars
var keys []string
for key := range env.Vars {
keys = append(keys, key)
}
sort.Strings(keys)
// display the vars using the format string for the shell
if e.UsageHint {
fmt.Printf(usageHint, strings.Join(os.Args, " "))
}
for _, key := range keys {
fmt.Printf(setVar, key, strings.Replace(env.Vars[key], "\"", quoteReplacement, -1))
}
return nil
}