forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapps.go
246 lines (224 loc) · 6.08 KB
/
apps.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
package main
import (
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/remind101/empire/pkg/heroku"
)
var cmdApps = &Command{
Run: runApps,
Usage: "apps [<name>...]",
Category: "app",
Short: "list apps",
Long: `
Lists apps. Shows the app name, owner, and last release time (or
time the app was created, if it's never been released).
Examples:
$ emp apps
myapp [email protected] us Jan 2 12:34
myapp-eu user@longdomainname… eu Jan 2 12:35
$ emp apps myapp
myapp [email protected] us Jan 2 12:34
`,
}
func init() {
cmdApps.Flag.StringVarP(&flagOrgName, "org", "o", "", "organization name")
}
func runApps(cmd *Command, names []string) {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
var apps []hkapp
if len(names) == 0 {
var err error
apps, err = getAppList(flagOrgName)
must(err)
} else {
appch := make(chan *heroku.App, len(names))
errch := make(chan error, len(names))
for _, name := range names {
if name == "" {
appch <- nil
} else {
go func(appname string) {
if app, err := client.AppInfo(appname); err != nil {
errch <- err
} else {
appch <- app
}
}(name)
}
}
for _ = range names {
select {
case err := <-errch:
printFatal(err.Error())
case app := <-appch:
if app != nil {
apps = append(apps, fromApp(*app))
}
}
}
}
printAppList(w, apps)
}
func getAppList(orgName string) ([]hkapp, error) {
if orgName != "" {
apps, err := client.OrganizationAppListForOrganization(orgName, &heroku.ListRange{Field: "name", Max: 1000})
if err != nil {
return nil, err
}
return fromOrgApps(apps), nil
}
apps, err := client.AppList(&heroku.ListRange{Field: "name", Max: 1000})
if err != nil {
return nil, err
}
return fromApps(apps), nil
}
func printAppList(w io.Writer, apps []hkapp) {
sort.Sort(appsByName(apps))
abbrevEmailApps(apps)
for _, a := range apps {
if a.Name != "" {
listApp(w, a)
}
}
}
func abbrevEmailApps(apps []hkapp) {
domains := make(map[string]int)
for _, a := range apps {
if a.Organization != "" {
parts := strings.SplitN(a.OwnerEmail, "@", 2)
if len(parts) == 2 {
domains["@"+parts[1]]++
}
}
}
smax, nmax := "", 0
for s, n := range domains {
if n > nmax {
smax = s
nmax = n
}
}
for i := range apps {
if apps[i].Organization != "" {
// reference the app directly in the slice so we're not modifying a copy
if strings.HasSuffix(apps[i].OwnerEmail, smax) {
apps[i].OwnerEmail = apps[i].OwnerEmail[:len(apps[i].OwnerEmail)-len(smax)]
}
}
}
}
func listApp(w io.Writer, a hkapp) {
t := a.CreatedAt
if a.ReleasedAt != nil {
t = *a.ReleasedAt
}
orgOrEmail := a.Organization
if orgOrEmail == "" {
orgOrEmail = a.OwnerEmail
}
listRec(w,
a.Name,
fmtCerts(a.Certs),
prettyTime{t},
)
}
type fmtCerts map[string]string
func (certs fmtCerts) String() string {
var p []string
for process, cert := range certs {
p = append(p, fmt.Sprintf("%s=%s", process, cert))
}
return strings.Join(p, ",")
}
type appsByName []hkapp
func (a appsByName) Len() int { return len(a) }
func (a appsByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a appsByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
type hkapp struct {
ArchivedAt *time.Time
BuildpackProvidedDescription *string
CreatedAt time.Time
GitURL string
Id string
Maintenance bool
Name string
Organization string
OwnerEmail string
Region string
ReleasedAt *time.Time
RepoSize *int
SlugSize *int
Stack string
UpdatedAt time.Time
WebURL string
Certs map[string]string
}
func fromApp(app heroku.App) (happ hkapp) {
orgName := ""
if strings.HasSuffix(app.Owner.Email, "@herokumanager.com") {
orgName = strings.TrimSuffix(app.Owner.Email, "@herokumanager.com")
}
return hkapp{
ArchivedAt: app.ArchivedAt,
BuildpackProvidedDescription: app.BuildpackProvidedDescription,
CreatedAt: app.CreatedAt,
GitURL: app.GitURL,
Id: app.Id,
Maintenance: app.Maintenance,
Name: app.Name,
Organization: orgName,
OwnerEmail: app.Owner.Email,
Region: app.Region.Name,
ReleasedAt: app.ReleasedAt,
RepoSize: app.RepoSize,
SlugSize: app.SlugSize,
Stack: app.Stack.Name,
UpdatedAt: app.UpdatedAt,
WebURL: app.WebURL,
Certs: app.Certs,
}
}
func fromApps(apps []heroku.App) (happs []hkapp) {
happs = make([]hkapp, len(apps))
for i := range apps {
happs[i] = fromApp(apps[i])
}
return
}
func fromOrgApp(oapp heroku.OrganizationApp) (happ hkapp) {
orgName := ""
if oapp.Organization != nil {
orgName = oapp.Organization.Name
}
return hkapp{
ArchivedAt: oapp.ArchivedAt,
BuildpackProvidedDescription: oapp.BuildpackProvidedDescription,
CreatedAt: oapp.CreatedAt,
GitURL: oapp.GitURL,
Id: oapp.Id,
Maintenance: oapp.Maintenance,
Name: oapp.Name,
Organization: orgName,
Region: oapp.Region.Name,
ReleasedAt: oapp.ReleasedAt,
RepoSize: oapp.RepoSize,
SlugSize: oapp.SlugSize,
Stack: oapp.Stack.Name,
UpdatedAt: oapp.UpdatedAt,
WebURL: oapp.WebURL,
}
}
func fromOrgApps(oapps []heroku.OrganizationApp) (happs []hkapp) {
happs = make([]hkapp, len(oapps))
for i := range oapps {
happs[i] = fromOrgApp(oapps[i])
}
return
}