/
main.go
209 lines (167 loc) · 4.59 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
package main
import (
"fmt"
"io"
"os"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
gitcmd "github.com/tsuyoshiwada/go-gitcmd"
"github.com/urfave/cli"
)
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 ".changelog/config.yml".
`,
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-changelog"
app.Usage = "todo usage for git-changelog"
app.Version = version(FnVersionOptions{HashAbbrevLength: 8})
app.Flags = []cli.Flag{
// init
cli.BoolFlag{
Name: "init",
Usage: "generate the git-changelog configuration file in interactive",
},
// config
cli.StringFlag{
Name: "config, c",
Usage: "specifies a different configuration file to pick up",
Value: ".changelog/config.yml",
},
// output
cli.StringFlag{
Name: "output, 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)",
},
cli.BoolFlag{
Name: "next-tag-now",
Usage: "assign date of 'next-tag' to current time (EXPERIMENTAL)",
},
cli.BoolFlag{
Name: "unreleased, u",
Usage: "include unreleased commits",
},
// silent
cli.BoolFlag{
Name: "silent",
Usage: "disable stdout output",
},
// no-color
cli.BoolFlag{
Name: "no-color",
Usage: "disable color output",
EnvVar: "NO_COLOR",
},
// no-emoji
cli.BoolFlag{
Name: "no-emoji",
Usage: "disable emoji output",
EnvVar: "NO_EMOJI",
},
// no-case
cli.BoolFlag{
Name: "no-case",
Usage: "disable case sensitive filters",
},
// tag-filter-pattern
cli.StringFlag{
Name: "tag-filter-pattern, p",
Usage: "Regular expression of tag filter. Is specified, only matched tags will be picked",
},
// help & version
cli.HelpFlag,
cli.VersionFlag,
}
app.Action = actionFunc
return app
}
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())
}
// changelog
changelogCLI := NewCLI(
&CLIContext{
WorkingDir: wd,
Stdout: colorable.NewColorableStdout(),
Stderr: colorable.NewColorableStderr(),
ConfigPath: c.String("config"),
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"),
NextTagNow: c.Bool("next-tag-now"),
TagFilterPattern: c.String("tag-filter-pattern"),
Unreleased: c.Bool("unreleased"),
},
fs,
NewConfigLoader(),
NewGenerator(),
)
os.Exit(changelogCLI.Run())
return nil
}
func main() {
app := createApp(appAction)
app.Run(os.Args)
}