forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
170 lines (147 loc) · 3.45 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/joho/godotenv"
)
var cmdEnv = &Command{
Run: runEnv,
Usage: "env",
NeedsApp: true,
Category: "config",
NumArgs: 0,
Short: "list env vars",
Long: `Show all env vars.`,
}
func runEnv(cmd *Command, args []string) {
cmd.AssertNumArgsCorrect(args)
config, err := client.ConfigVarInfo(mustApp())
must(err)
var configKeys []string
for k := range config {
configKeys = append(configKeys, k)
}
sort.Strings(configKeys)
for _, k := range configKeys {
fmt.Printf("%s=%s\n", k, config[k])
}
}
var cmdGet = &Command{
Run: runGet,
Usage: "get <name>",
NeedsApp: true,
Category: "config",
NumArgs: 1,
Short: "get env var" + extra,
Long: `
Get the value of an env var.
Example:
$ emp get BUILDPACK_URL
http://github.com/kr/heroku-buildpack-inline.git
`,
}
func runGet(cmd *Command, args []string) {
cmd.AssertNumArgsCorrect(args)
config, err := client.ConfigVarInfo(mustApp())
must(err)
value, found := config[args[0]]
if !found {
printFatal("No such key as '%s'", args[0])
}
fmt.Println(value)
}
var cmdSet = &Command{
Run: maybeMessage(runSet),
Usage: "set <name>=<value>...",
NeedsApp: true,
OptionalMessage: true,
Category: "config",
Short: "set env var",
Long: `
Set the value of an env var.
Example:
$ emp set BUILDPACK_URL=http://github.com/kr/heroku-buildpack-inline.git
Set env vars and restarted myapp.
`,
}
func runSet(cmd *Command, args []string) {
appname := mustApp()
message := getMessage()
if len(args) == 0 {
cmd.PrintUsage()
os.Exit(2)
}
config := make(map[string]*string)
for _, arg := range args {
i := strings.Index(arg, "=")
if i < 0 {
printFatal("bad format: %#q. See 'emp help set'", arg)
}
val := arg[i+1:]
config[arg[:i]] = &val
}
_, err := client.ConfigVarUpdate(appname, config, message)
must(err)
log.Printf("Set env vars and restarted " + appname + ".")
}
var cmdUnset = &Command{
Run: maybeMessage(runUnset),
Usage: "unset <name>...",
NeedsApp: true,
OptionalMessage: true,
Category: "config",
Short: "unset env var",
Long: `
Unset an env var.
Example:
$ emp unset BUILDPACK_URL
Unset env vars and restarted myapp.
`,
}
func runUnset(cmd *Command, args []string) {
appname := mustApp()
message := getMessage()
if len(args) == 0 {
cmd.PrintUsage()
os.Exit(2)
}
config := make(map[string]*string)
for _, key := range args {
config[key] = nil
}
_, err := client.ConfigVarUpdate(appname, config, message)
must(err)
log.Printf("Unset env vars and restarted %s.", appname)
}
var cmdEnvLoad = &Command{
Run: maybeMessage(runEnvLoad),
Usage: "env-load <file>",
NeedsApp: true,
OptionalMessage: true,
Category: "config",
NumArgs: 1,
Short: "load env file",
Long: `
Loads environment variables from a file.
Example:
$ emp env-load app.env
Set env vars from app.env and restarted myapp.
`,
}
func runEnvLoad(cmd *Command, args []string) {
appname := mustApp()
message := getMessage()
cmd.AssertNumArgsCorrect(args)
parsedVars, err := godotenv.Read(args[0])
must(err)
config := make(map[string]*string)
for key, value := range parsedVars {
config[key] = &value
}
_, err = client.ConfigVarUpdate(appname, config, message)
must(err)
log.Printf("Updated env vars from %s and restarted %s.", args[0], appname)
}