This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
addons.go
381 lines (328 loc) · 8.43 KB
/
addons.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"fmt"
"io"
"log"
"math/big"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/heroku/hk/Godeps/_workspace/src/github.com/bgentry/heroku-go"
)
var cmdAddons = &Command{
Run: runAddons,
Usage: "addons [<service>:<plan>...]",
NeedsApp: true,
Category: "add-on",
Short: "list addons",
Long: `
Lists addons.
Examples:
$ hk addons
heroku-postgresql-blue heroku-postgresql:crane Nov 19 12:40
pgbackups pgbackups:plus Sep 30 15:43
$ hk addons pgbackups
pgbackups pgbackups:plus Sep 30 15:43
`,
}
func runAddons(cmd *Command, names []string) {
appname := mustApp()
addons, err := client.AddonList(appname, nil)
must(err)
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
for i, s := range names {
names[i] = strings.ToLower(s)
}
for _, a := range addons {
if len(names) == 0 || addonMatch(a, names) {
listAddon(w, a)
}
}
}
func addonMatch(a heroku.Addon, names []string) bool {
for _, name := range names {
if name == strings.ToLower(a.Name) {
return true
}
if name == strings.ToLower(a.Plan.Name) {
return true
}
if name == strings.ToLower(a.Id) {
return true
}
}
return false
}
func listAddon(w io.Writer, a heroku.Addon) {
name := a.Name
if name == "" {
name = "[unnamed]"
}
listRec(w,
name,
a.Plan.Name,
prettyTime{a.CreatedAt},
)
}
var cmdAddonAdd = &Command{
Run: runAddonAdd,
Usage: "addon-add <service>[:<plan>] [<config>=<value>...]",
NeedsApp: true,
Category: "add-on",
Short: "add an addon",
Long: `
Adds an addon to an app.
Examples:
$ hk addon-add heroku-postgresql
Added heroku-postgresql:hobby-dev to myapp as heroku-postgresql-yellow.
$ hk addon-add heroku-postgresql:standard-tengu
Added heroku-postgresql:standard-tengu to myapp as heroku-postgresql-orange.
`,
}
func runAddonAdd(cmd *Command, args []string) {
appname := mustApp()
if len(args) == 0 {
cmd.PrintUsage()
os.Exit(2)
}
plan := args[0]
var opts heroku.AddonCreateOpts
if len(args) > 1 {
config, err := parseAddonAddConfig(args[1:])
if err != nil {
log.Println(err)
os.Exit(2)
}
// if this is a postgres addon, resolve fork/follow/rollback args
provider, _ := splitProviderAndPlan(plan)
if provider == hpgAddonName() && config != nil {
for k := range *config {
if i := stringsIndex(hpgOptNames, k); i != -1 {
// contains an hpgOptNames key, we need to resolve these against envs
appEnv, err := client.ConfigVarInfo(appname)
must(err)
must(hpgAddonOptResolve(config, appEnv))
break
}
}
}
opts = heroku.AddonCreateOpts{Config: config}
}
addon, err := client.AddonCreate(appname, plan, &opts)
must(err)
log.Printf("Added %s to %s as %s.", addon.Plan.Name, appname, addon.Name)
}
func splitProviderAndPlan(providerAndPlan string) (provider string, plan string) {
parts := strings.Split(providerAndPlan, ":")
if len(parts) > 0 {
provider = parts[0]
}
if len(parts) > 1 {
plan = parts[1]
}
return
}
func parseAddonAddConfig(config []string) (*map[string]string, error) {
conf := make(map[string]string, len(config))
for _, kv := range config {
iEq := strings.IndexRune(kv, '=')
if iEq < 1 || len(kv) < iEq+2 {
return nil, fmt.Errorf("Invalid option '%s', must be of form 'key=value'", kv)
}
val := kv[iEq+1:]
if val[0] == '\'' {
val = strings.Trim(val, "'")
} else if val[0] == '"' {
val = strings.Trim(val, "\"")
}
conf[kv[:iEq]] = val
}
return &conf, nil
}
var cmdAddonDestroy = &Command{
Run: runAddonDestroy,
Usage: "addon-destroy <name>",
NeedsApp: true,
Category: "add-on",
Short: "destroy an addon",
Long: `
Removes an addon from an app, permanently destroying any data
stored by that addon. The command will prompt for confirmation,
or accept confirmation via stdin.
Examples:
$ hk addon-destroy heroku-postgresql-blue
warning: This will destroy heroku-postgresql-blue on myapp. Please type "myapp" to continue:
> myapp
Destroyed heroku-postgresql-blue on myapp.
$ echo myapp | hk addon-destroy redistogo
Destroyed redistogo on myapp.
`,
}
func runAddonDestroy(cmd *Command, args []string) {
appname := mustApp()
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
name := args[0]
if strings.IndexRune(name, ':') != -1 {
// specified an addon with plan name, unsupported in v3
log.Println("Please specify an addon name, not a plan name.")
cmd.PrintUsage()
os.Exit(2)
}
warning := "This will destroy %s on %s. Please type %q to continue:"
mustConfirm(fmt.Sprintf(warning, name, appname, appname), appname)
checkAddonError(client.AddonDelete(appname, name))
log.Printf("Destroyed %s on %s.", name, appname)
}
var cmdAddonOpen = &Command{
Run: runAddonOpen,
Usage: "addon-open <name>",
NeedsApp: true,
Category: "add-on",
Short: "open an addon" + extra,
Long: `
Open the addon's management page in your default web browser.
Examples:
$ hk addon-open heroku-postgresql-blue
$ hk addon-open redistogo
`,
}
func runAddonOpen(cmd *Command, args []string) {
appname := mustApp()
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
name := args[0]
// look up addon to make sure it exists and to get plan name
a, err := client.AddonInfo(appname, name)
checkAddonError(err)
must(openURL("https://addons-sso.heroku.com/apps/" + appname + "/addons/" + a.Plan.Name))
}
var cmdAddonPlan = &Command{
Run: runAddonPlan,
Usage: "addon-plan <name> <plan>",
NeedsApp: true,
Category: "add-on",
Short: "change an addon's plan" + extra,
Long: `
Change an addon's plan. Not all add-on providers support this
Examples:
$ hk addon-plan redistogo small
Changed redistogo plan to small on myapp.
`,
}
func runAddonPlan(cmd *Command, args []string) {
appname := mustApp()
if len(args) != 2 {
cmd.PrintUsage()
os.Exit(2)
}
name := args[0]
plan := args[1]
addon, err := client.AddonInfo(appname, name)
checkAddonError(err)
// assemble service:plan string
serviceAndPlan := strings.Split(addon.Plan.Name, ":")[0] + ":" + plan
a, err := client.AddonUpdate(appname, name, serviceAndPlan)
checkAddonError(err)
log.Printf("Changed %s plan to %s on %s.", a.Name, plan, appname)
}
func checkAddonError(err error) {
if err != nil {
if hkerr, ok := err.(heroku.Error); ok && hkerr.Id == "not_found" {
printFatal(err.Error() + " Choose an addon name from `hk addons`.")
} else {
printFatal(err.Error())
}
os.Exit(2)
}
}
var cmdAddonServices = &Command{
Run: runAddonServices,
Usage: "addon-services",
Category: "add-on",
Short: "list addon services" + extra,
Long: `
Lists available addon services.
Examples:
$ hk addon-services
heroku-postgresql
newrelic
redisgreen
...
`,
}
func runAddonServices(cmd *Command, args []string) {
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
services, err := client.AddonServiceList(nil)
must(err)
for _, s := range services {
fmt.Println(s.Name)
}
}
var cmdAddonPlans = &Command{
Run: runAddonPlans,
Usage: "addon-plans <service>",
Category: "add-on",
Short: "list addon plans" + extra,
Long: `
Lists available addon plans for an addon provider.
Examples:
$ hk addon-plans heroku-postgresql
hobby-dev $0/mo
hobby-basic $9/mo
standard-yanari $50/mo
standard-tengu $200/mo
premium-yanari $200/mo
premium-tengu $350/mo
standard-ika $750/mo
premium-ika $1200/mo
...
`,
}
func runAddonPlans(cmd *Command, args []string) {
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
service := args[0]
plans, err := client.PlanList(service, nil)
must(err)
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
sort.Sort(addonPlansByPrice(plans))
for _, p := range plans {
listRec(w,
strings.TrimPrefix(p.Name, service+":"),
addonPlanPriceString(p),
)
}
}
type addonPlansByPrice []heroku.Plan
func (a addonPlansByPrice) Len() int { return len(a) }
func (a addonPlansByPrice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a addonPlansByPrice) Less(i, j int) bool { return a[i].Price.Cents < a[j].Price.Cents }
func addonPlanPriceString(p heroku.Plan) string {
r := big.NewRat(int64(p.Price.Cents), 100)
decimals := 2
if p.Price.Cents%100 == 0 {
decimals = 0
}
return "$" + r.FloatString(decimals) + "/" + shortenPriceUnit(p.Price.Unit)
}
func shortenPriceUnit(unit string) string {
switch unit {
case "month":
return "mo"
default:
return unit
}
}