forked from git-chglog/git-chglog
/
main.go
268 lines (218 loc) · 6.12 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
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"github.com/tsuyoshiwada/go-gitcmd"
"github.com/urfave/cli/v2"
)
// version is passed in via LDFLAGS main.version
var version string
// CreateApp creates and initializes CLI application
// with description, flags, version, etc.
func CreateApp(actionFunc cli.ActionFunc) *cli.App {
ttl := color.New(color.FgYellow).SprintFunc()
cli.AppHelpTemplate = fmt.Sprintf(`
%s
{{.Name}} [options] <tag query>
There are the following specification methods for <tag query>.
1. <old>..<new> - Commit contained in <old> tags from <new>.
2. <name>.. - Commit from the <name> to the latest tag.
3. ..<name> - Commit from the oldest tag to <name>.
4. <name> - Commit contained in <name>.
%s
{{range .Flags}}{{.}}
{{end}}
%s
$ {{.Name}}
If <tag query> is not specified, it corresponds to all tags.
This is the simplest example.
$ {{.Name}} 1.0.0..2.0.0
The above is a command to generate CHANGELOG including commit of 1.0.0 to 2.0.0.
$ {{.Name}} 1.0.0
The above is a command to generate CHANGELOG including commit of only 1.0.0.
$ {{.Name}} $(git describe --tags $(git rev-list --tags --max-count=1))
The above is a command to generate CHANGELOG with the commit included in the latest tag.
$ {{.Name}} --output CHANGELOG.md
The above is a command to output to CHANGELOG.md instead of standard output.
$ {{.Name}} --config custom/dir/config.yml
The above is a command that uses a configuration file placed other than ".chglog/config.yml".
$ {{.Name}} --path path/to/my/component --output CHANGELOG.component.md
Filter commits by specific paths or files in git and output to a component specific changelog.
`,
ttl("USAGE:"),
ttl("OPTIONS:"),
ttl("EXAMPLE:"),
)
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
cli.HelpPrinterCustom(colorable.NewColorableStdout(), templ, data, nil)
}
app := cli.NewApp()
app.Name = "git-chglog"
app.Usage = "todo usage for git-chglog"
app.Version = version
app.Flags = []cli.Flag{
// init
&cli.BoolFlag{
Name: "init",
Usage: "generate the git-chglog configuration file in interactive",
},
// path
&cli.StringSliceFlag{
Name: "path",
Usage: "Filter commits by path(s). Can use multiple times.",
},
// config
&cli.StringFlag{
Name: "config, c",
Aliases: []string{"c"},
Usage: "specifies a different configuration file to pick up",
Value: ".chglog/config.yml",
},
// template
&cli.StringFlag{
Name: "template",
Aliases: []string{"t"},
Usage: "specifies a template file to pick up. If not specified, use the one in config",
},
// repository url
&cli.StringFlag{
Name: "repository-url",
Usage: "specifies git repo URL. If not specified, use 'repository_url' in config",
},
// output
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "output path and filename for the changelogs. If not specified, output to stdout",
},
&cli.StringFlag{
Name: "next-tag",
Usage: "treat unreleased commits as specified tags (EXPERIMENTAL)",
},
// silent
&cli.BoolFlag{
Name: "silent",
Usage: "disable stdout output",
},
// no-color
&cli.BoolFlag{
Name: "no-color",
Usage: "disable color output",
EnvVars: []string{"NO_COLOR"},
},
// no-emoji
&cli.BoolFlag{
Name: "no-emoji",
Usage: "disable emoji output",
EnvVars: []string{"NO_EMOJI"},
},
// no-case
&cli.BoolFlag{
Name: "no-case",
Usage: "disable case sensitive filters",
},
// tag-filter-pattern
&cli.StringFlag{
Name: "tag-filter-pattern",
Usage: "Regular expression of tag filter. Is specified, only matched tags will be picked",
},
// jira-url
&cli.StringFlag{
Name: "jira-url",
Usage: "Jira URL",
EnvVars: []string{"JIRA_URL"},
},
// jira-username
&cli.StringFlag{
Name: "jira-username",
Usage: "Jira username",
EnvVars: []string{"JIRA_USERNAME"},
},
// jira-token
&cli.StringFlag{
Name: "jira-token",
Usage: "Jira token",
EnvVars: []string{"JIRA_TOKEN"},
},
// sort
&cli.StringFlag{
Name: "sort",
Usage: "Specify how to sort tags; currently supports \"date\" or by \"semver\"",
DefaultText: "date",
},
// help & version
cli.HelpFlag,
cli.VersionFlag,
}
app.Action = actionFunc
return app
}
// AppAction is a callback function to create initializer
// and CLIContext and ultimately run the application.
func AppAction(c *cli.Context) error {
wd, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to get working directory", err)
os.Exit(ExitCodeError)
}
// initializer
if c.Bool("init") {
initializer := NewInitializer(
&InitContext{
WorkingDir: wd,
Stdout: colorable.NewColorableStdout(),
Stderr: colorable.NewColorableStderr(),
},
fs,
NewQuestioner(
gitcmd.New(&gitcmd.Config{
Bin: "git",
}),
fs,
),
NewConfigBuilder(),
templateBuilderFactory,
)
os.Exit(initializer.Run())
}
// chglog
chglogCLI := NewCLI(
&CLIContext{
WorkingDir: wd,
Stdout: colorable.NewColorableStdout(),
Stderr: colorable.NewColorableStderr(),
ConfigPath: c.String("config"),
Template: c.String("template"),
RepositoryURL: c.String("repository-url"),
OutputPath: c.String("output"),
Silent: c.Bool("silent"),
NoColor: c.Bool("no-color"),
NoEmoji: c.Bool("no-emoji"),
NoCaseSensitive: c.Bool("no-case"),
Query: c.Args().First(),
NextTag: c.String("next-tag"),
TagFilterPattern: c.String("tag-filter-pattern"),
JiraUsername: c.String("jira-username"),
JiraToken: c.String("jira-token"),
JiraURL: c.String("jira-url"),
Paths: c.StringSlice("path"),
Sort: c.String("sort"),
},
fs,
NewConfigLoader(),
NewGenerator(),
)
os.Exit(chglogCLI.Run())
return nil
}
func main() {
app := CreateApp(AppAction)
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}