-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
300 lines (260 loc) · 6.84 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"gopkg.in/yaml.v2"
)
type Section struct {
GaiaREST []string
Gaiacli []string
Gaia []string
SDK []string
Tendermint []string
}
func NewSection() *Section {
return &Section{GaiaREST: []string{}, Gaiacli: []string{}, Gaia: []string{}, SDK: []string{}}
}
func (se *Section) Empty() bool {
return se == nil || (len(se.GaiaREST) == 0 &&
len(se.Gaiacli) == 0 && len(se.Gaia) == 0 &&
len(se.SDK) == 0)
}
func (se Section) GetStanza(name string) ([]string, error) {
switch name {
case "gaiarest":
return se.GaiaREST, nil
case "gaiacli":
return se.Gaiacli, nil
case "gaia":
return se.Gaia, nil
case "sdk":
return se.SDK, nil
case "tendermint":
return se.Tendermint, nil
}
return nil, errors.New("unknown stanza")
}
func (se *Section) AppendItem(stanza, item string) error {
switch stanza {
case "gaiarest":
se.GaiaREST = append(se.GaiaREST, item)
case "gaiacli":
se.Gaiacli = append(se.Gaiacli, item)
case "gaia":
se.Gaia = append(se.Gaia, item)
case "sdk":
se.SDK = append(se.SDK, item)
case "tendermint":
se.Tendermint = append(se.Tendermint, item)
default:
return errors.New("unknown stanza")
}
return nil
}
type Release struct {
Breaking *Section
Features *Section
Improvements *Section
Bugfixes *Section
}
func NewRelease() *Release {
return &Release{Breaking: NewSection(), Features: NewSection(), Improvements: NewSection(), Bugfixes: NewSection()}
}
func (r Release) GetSection(name string) (*Section, error) {
switch name {
case "breaking":
return r.Breaking, nil
case "improvements":
return r.Improvements, nil
case "features":
return r.Features, nil
case "bugfixes":
return r.Bugfixes, nil
}
return nil, errors.New("unknown section")
}
var (
progName string
overwriteSourceFile bool
)
func init() {
progName = filepath.Base(os.Args[0])
flag.BoolVar(&overwriteSourceFile, "w", false, "write result to (source) file instead of stdout")
flag.Usage = printUsage
}
func errInsufficientArgs() {
log.Fatalf("insufficient arguments\nTry '%s -help' for more information.", progName)
}
func errTooManyArgs() {
log.Fatalf("too many arguments\nTry '%s -help' for more information.", progName)
}
func unknownCommand(cmd string) {
log.Fatalf("unknown command -- '%s'\nTry '%s -help' for more information.", cmd, progName)
}
func main() {
log.SetFlags(0)
log.SetPrefix(fmt.Sprintf("%s: ", filepath.Base(progName)))
flag.Parse()
if flag.NArg() < 1 {
errInsufficientArgs()
}
cmd := flag.Arg(0)
switch cmd {
case "new":
newFile()
return
case "add":
switch {
case flag.NArg() < 4:
errInsufficientArgs()
case flag.NArg() > 4:
errTooManyArgs()
}
editFile(flag.Arg(1), flag.Arg(2), flag.Arg(3))
return
case "convert":
switch {
case flag.NArg() < 3:
errInsufficientArgs()
case flag.NArg() > 3:
errTooManyArgs()
}
convert(flag.Arg(1), flag.Arg(2))
return
default:
unknownCommand(cmd)
}
}
func newFile() { fmt.Printf("%s", mustMarshal(NewRelease())) }
func editFile(clFile, section, stanza string) {
r := unmarshalChangelogFile(clFile)
releaseSection, err := r.GetSection(section)
if err != nil {
log.Fatalf("unknown section %q, possible values are %s", section,
[]string{"breaking", "features", "improvements", "bugfixes"})
}
if _, err := releaseSection.GetStanza(stanza); err != nil {
log.Fatalf("unknown stanza %q, possible values are %s", stanza,
[]string{"gaia", "gaiacli", "gaiarest", "sdk", "tendermint"})
}
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error: %v", err)
}
if err := releaseSection.AppendItem(stanza, strings.TrimSpace(string(bytes))); err != nil {
panic(err)
}
out, err := yaml.Marshal(r)
if err != nil {
log.Fatalf("error: %v", err)
}
outFile := os.Stdout
if overwriteSourceFile {
outFile, err = os.OpenFile(clFile, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
log.Fatal(err)
}
}
fmt.Fprintf(outFile, "%s", out)
}
func convert(clFile, version string) {
r := unmarshalChangelogFile(clFile)
md := fmt.Sprintf("## %s\n", version)
md += processSection(r.Breaking, "BREAKING CHANGES")
md += processSection(r.Features, "FEATURES")
md += processSection(r.Improvements, "IMPROVEMENTS")
md += processSection(r.Bugfixes, "BUGFIXES")
fmt.Println(md)
}
func processSection(section *Section, header string) string {
if section.Empty() {
return ""
}
s := fmt.Sprintf("### %s\n", header)
s += processStanza(section.GaiaREST, "Gaia REST API (`gaiacli rest-server`)")
s += processStanza(section.Gaiacli, "Gaia CLI (`gaiacli`)")
s += processStanza(section.Gaia, "Gaia")
s += processStanza(section.SDK, "SDK")
s += processStanza(section.Tendermint, "Tendermint")
return s
}
func processStanza(stanza []string, header string) string {
// regex to beautify github issues URLs
if len(stanza) == 0 {
return ""
}
s := fmt.Sprintf("* %s\n", header)
for _, item := range stanza {
s += processLine(item) + "\n"
}
return s
}
func processLine(s string) string {
linesSlice := strings.Split(s, "\n")
var out string
if len(linesSlice) == 1 {
return fmt.Sprintf(" * %s\n", expandGhURLs(linesSlice[0]))
}
for i, line := range linesSlice {
line = strings.Trim(line, "\n")
if i == 0 {
out = fmt.Sprintf(" * %s\n", expandGhURLs(line))
} else {
out += fmt.Sprintf(" %s\n", expandGhURLs(line))
}
}
return out
}
var reGhIssue = regexp.MustCompilePOSIX(`issue#([0-9]+)`)
var reGhPR = regexp.MustCompilePOSIX(`pr#([0-9]+)`)
func expandGhURLs(s string) string {
return reGhPR.ReplaceAllString(reGhIssue.ReplaceAllString(s,
"[\\#$1](https://github.com/cosmos/cosmos-sdk/issues/$1)"),
"[\\#$1](https://github.com/cosmos/cosmos-sdk/pull/$1)")
}
func unmarshalChangelogFile(clFile string) *Release {
contents, err := ioutil.ReadFile(clFile)
if err != nil {
log.Fatal(err)
}
r := NewRelease()
if err := yaml.Unmarshal(contents, r); err != nil {
log.Fatal(err)
}
return r
}
func mustMarshal(t interface{}) []byte {
out, err := yaml.Marshal(t)
if err != nil {
log.Fatal(err)
}
return out
}
func printUsage() {
usageText := fmt.Sprintf(`usage: %s [-w] [option]
Commands:
new Create new empty changelog.
add [-w] FILE SECTION STANZA Add entry to a changelog file.
Read from stdin until it
encounters EOF.
convert FILE VERSION Convert a changelog into
Markdown format and print it
to stdout.
Sections: Stanzas:
--- ---
breaking gaia
features gaiacli
improvements gaiarest
bugfixes sdk
tendermint
`, progName)
fmt.Fprintf(os.Stderr, "%s\nFlags:\n", usageText)
flag.PrintDefaults()
}