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
/
releases.go
230 lines (202 loc) · 5.25 KB
/
releases.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
package main
import (
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/heroku/hk/Godeps/_workspace/src/github.com/bgentry/heroku-go"
)
var releaseCount int
var cmdReleases = &Command{
Run: runReleases,
Usage: "releases [-n <limit>] [<version>...]",
NeedsApp: true,
Category: "release",
Short: "list releases",
Long: `
Lists releases. Shows the version of the release (e.g. v1), who
made the release, time of the release, and description.
Options:
-n <limit> maximum number of recent releases to display
Examples:
$ hk releases
v1 bob@test.com Jun 12 18:28 Deploy 3ae20c2
v2 john@me.com Jun 13 18:14 Deploy 0fda0ae
v3 john@me.com Jun 13 18:31 Rollback to v2
$ hk releases -n 2
v2 john Jun 13 18:14 Deploy 0fda0ae
v3 john Jun 13 18:31 Rollback to v2
$ hk releases 1 3
v1 bob@test.com Jun 12 18:28 Deploy 3ae20c2
v3 john@me.com Jun 13 18:31 Rollback to v2
`,
}
func init() {
cmdReleases.Flag.IntVarP(&releaseCount, "number", "n", 20, "max number of recent releases to display")
}
func runReleases(cmd *Command, versions []string) {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
listReleases(w, versions)
}
func listReleases(w io.Writer, versions []string) {
appname := mustApp()
if len(versions) == 0 {
hrels, err := client.ReleaseList(appname, &heroku.ListRange{
Field: "version",
Max: releaseCount,
Descending: true,
})
must(err)
rels := make([]*Release, len(hrels))
for i := range hrels {
rels[i] = newRelease(&hrels[i])
}
sort.Sort(releasesByVersion(rels))
gitDescribe(rels)
abbrevEmailReleases(rels)
for _, r := range rels {
listRelease(w, r)
}
return
}
var rels []*Release
relch := make(chan *heroku.Release, len(versions))
errch := make(chan error, len(versions))
for _, name := range versions {
if name == "" {
relch <- nil
} else {
go func(relname string) {
if rel, err := client.ReleaseInfo(appname, relname); err != nil {
errch <- err
} else {
relch <- rel
}
}(strings.TrimPrefix(name, "v"))
}
}
for _ = range versions {
select {
case err := <-errch:
printFatal(err.Error())
case rel := <-relch:
if rel != nil {
rels = append(rels, newRelease(rel))
}
}
}
sort.Sort(releasesByVersion(rels))
gitDescribe(rels)
abbrevEmailReleases(rels)
for _, r := range rels {
listRelease(w, r)
}
}
func abbrevEmailReleases(rels []*Release) {
domains := make(map[string]int)
for _, r := range rels {
r.Who = r.User.Email
if a := strings.SplitN(r.Who, "@", 2); len(a) == 2 {
domains["@"+a[1]]++
}
}
smax, nmax := "", 0
for s, n := range domains {
if n > nmax {
smax = s
}
}
for _, r := range rels {
if strings.HasSuffix(r.Who, smax) {
r.Who = r.Who[:len(r.Who)-len(smax)]
}
}
}
func listRelease(w io.Writer, r *Release) {
desc := r.Description
// add the git tag to the description if it's not a hash (and thus isn't
// included already)
if r.Commit != "" && !strings.Contains(r.Description, r.Commit) {
desc += " (" + abbrev(r.Commit, 12) + ")"
}
listRec(w,
fmt.Sprintf("v%d", r.Version),
abbrev(r.Who, 10),
prettyTime{r.CreatedAt},
desc,
)
}
type releasesByVersion []*Release
func (a releasesByVersion) Len() int { return len(a) }
func (a releasesByVersion) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a releasesByVersion) Less(i, j int) bool { return a[i].Version < a[j].Version }
func newRelease(rel *heroku.Release) *Release {
return &Release{*rel, "", ""}
}
var cmdReleaseInfo = &Command{
Run: runReleaseInfo,
Usage: "release-info <version>",
NeedsApp: true,
Category: "release",
Short: "show release info",
Long: `
release-info shows detailed information about a release.
Examples:
$ hk release-info v116
Version: v116
By: user@test.com
Change: Deploy 62b3059
When: 2014-01-13T21:20:57Z
Id: abcd1234-5678-def0-8190-12347060474d
Slug: 98765432-82ba-10ba-fedc-8d206789d062
`,
}
func runReleaseInfo(cmd *Command, args []string) {
appname := mustApp()
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
ver := strings.TrimPrefix(args[0], "v")
rel, err := client.ReleaseInfo(appname, ver)
must(err)
fmt.Printf("Version: v%d\n", rel.Version)
fmt.Printf("By: %s\n", rel.User.Email)
fmt.Printf("Change: %s\n", rel.Description)
fmt.Printf("When: %s\n", rel.CreatedAt.UTC().Format(time.RFC3339))
fmt.Printf("Id: %s\n", rel.Id)
if rel.Slug != nil {
fmt.Printf("Slug: %s\n", rel.Slug.Id)
}
}
var cmdRollback = &Command{
Run: runRollback,
Usage: "rollback <version>",
NeedsApp: true,
Category: "release",
Short: "roll back to a previous release",
Long: `
Rollback re-releases an app at an older version. This action
creates a new release based on the older release, then restarts
the app's dynos on the new release.
Examples:
$ hk rollback v4
Rolled back myapp to v4 as v7.
`,
}
func runRollback(cmd *Command, args []string) {
appname := mustApp()
if len(args) != 1 {
cmd.PrintUsage()
os.Exit(2)
}
ver := strings.TrimPrefix(args[0], "v")
rel, err := client.ReleaseRollback(appname, ver)
must(err)
log.Printf("Rolled back %s to v%s as v%d.\n", appname, ver, rel.Version)
}