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
/
drains.go
214 lines (181 loc) · 4.48 KB
/
drains.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
package main
import (
"fmt"
"log"
"os"
"text/tabwriter"
"github.com/heroku/hk/Godeps/_workspace/src/github.com/bgentry/heroku-go"
)
var cmdDrains = &Command{
Run: runDrains,
Usage: "drains",
NeedsApp: true,
Category: "app",
Short: "list log drains" + extra,
Long: `
Lists log drains on an app. Shows the drain's ID, as well as its
Add-on name (if it's from an Add-on) or its URL.
Example:
$ hk drains
6af8b744-c513-4217-9f7c-1234567890ab logging-addon:jumbo
7f89b6bb-08af-4343-b0b4-d0415dd81712 syslog://my.log.host
23fcdb8a-3095-46f5-abc2-c5f293c54cf1 syslog://my.other.log.host
`,
}
func runDrains(cmd *Command, args []string) {
if len(args) != 0 {
cmd.PrintUsage()
os.Exit(2)
}
appname := mustApp()
// fetch app's addons concurrently in case we need to resolve addon names
addonsch := make(chan []heroku.Addon, 1)
errch := make(chan error, 1)
go func(appname string) {
if addons, err := client.AddonList(appname, nil); err != nil {
errch <- err
} else {
addonsch <- addons
}
}(appname)
drains, err := client.LogDrainList(appname, nil)
must(err)
hasAddonDrains := false
merged := make([]*mergedLogDrain, len(drains))
for i := range drains {
if !hasAddonDrains && drains[i].Addon != nil {
hasAddonDrains = true
}
merged[i] = &mergedLogDrain{drain: drains[i], hasAddon: drains[i].Addon != nil}
}
if hasAddonDrains {
// resolve addon names, use those instead of URLs
select {
case _ = <-errch:
// couldn't resolve addons, just move on
case addons := <-addonsch:
mergeDrainAddonInfo(merged, addons)
}
}
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
for _, m := range merged {
listRec(w, m.drain.Id, m.addonNameOrURL())
}
}
type mergedLogDrain struct {
drain heroku.LogDrain
hasAddon bool
addon *heroku.Addon
}
func (m *mergedLogDrain) addonNameOrURL() string {
switch {
case m.hasAddon && m.addon != nil:
return m.addon.Plan.Name
case m.hasAddon:
return "unknown"
default:
return m.drain.URL
}
}
// merge addon info into log drains
func mergeDrainAddonInfo(merged []*mergedLogDrain, addons []heroku.Addon) {
for i := range merged {
if merged[i].hasAddon {
for j := range addons {
if merged[i].drain.Addon.Id == addons[j].Id {
merged[i].addon = &addons[j]
break
}
}
}
}
}
var cmdDrainInfo = &Command{
Run: runDrainInfo,
Usage: "drain-info <id or url>",
NeedsApp: true,
Category: "app",
Short: "show info for a log drain" + extra,
Long: `
Shows detailed info for a log drain.
Example:
$ hk drain-info syslog://my.other.log.host
Id: 7f89b6bb-08af-4343-b0b4-d0415dd81712
Token: d.a9dc787f-e0a8-43f3-a2c8-1fbf937fd47c
Addon: none
URL: syslog://my.log.host
$ hk drain-info 23fcdb8a-3095-46f5-abc2-c5f293c54cf1
...
`,
}
func runDrainInfo(cmd *Command, args []string) {
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
appname := mustApp()
drainIdOrURL := args[0]
drain, err := client.LogDrainInfo(appname, drainIdOrURL)
must(err)
addonName := "none"
if drain.Addon != nil {
addon, err := client.AddonInfo(appname, drain.Addon.Id)
if err != nil {
addonName = "unknown"
} else {
addonName = addon.Name
}
}
fmt.Printf("Id: %s\n", drain.Id)
fmt.Printf("Token: %s\n", drain.Token)
fmt.Printf("Addon: %s\n", addonName)
fmt.Printf("URL: %s\n", drain.URL)
}
var cmdDrainAdd = &Command{
Run: runDrainAdd,
Usage: "drain-add <url>",
NeedsApp: true,
Category: "app",
Short: "add a log drain" + extra,
Long: `
Adds a log drain to an app.
Example:
$ hk drain-add syslog://my.log.host
Added log drain to myapp.
`,
}
func runDrainAdd(cmd *Command, args []string) {
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
url := args[0]
_, err := client.LogDrainCreate(mustApp(), url)
must(err)
log.Printf("Added log drain to %s.", mustApp())
}
var cmdDrainRemove = &Command{
Run: runDrainRemove,
Usage: "drain-remove <id or url>",
NeedsApp: true,
Category: "app",
Short: "remove a log drain" + extra,
Long: `
Removes a log drain from an app.
Example:
$ hk drain-remove 7f89b6bb-08af-4343-b0b4-d0415dd81712
Removed log drain from myapp.
$ hk drain-remove syslog://my.log.host
Removed log drain from myapp.
`,
}
func runDrainRemove(cmd *Command, args []string) {
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
drainId := args[0]
must(client.LogDrainDelete(mustApp(), drainId))
log.Printf("Removed log drain from %s.", mustApp())
}