feat(cli): use --debug instead of --verbose

main
hau 2021-01-05 14:02:49 +07:00
parent e921459daf
commit ed8e4704e3
1 changed files with 19 additions and 18 deletions

33
main.go
View File

@ -39,7 +39,7 @@ const (
outputDirFlag = "output-dir" outputDirFlag = "output-dir"
filenameFlag = "filename" filenameFlag = "filename"
filetypeFlag = "filetype" filetypeFlag = "filetype"
verboseFlag = "verbose" debugFlag = "debug"
) )
func main() { func main() {
@ -90,8 +90,9 @@ func main() {
DefaultText: defaultFiletype, DefaultText: defaultFiletype,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: verboseFlag, Name: debugFlag,
Usage: "show what is going on", Aliases: []string{"-d"},
Usage: "Show debugging info",
}, },
}, },
Action: a.Run, Action: a.Run,
@ -106,7 +107,7 @@ func main() {
} }
type action struct { type action struct {
verbose bool debug bool
flags map[string]string flags map[string]string
args map[string]string args map[string]string
} }
@ -124,10 +125,10 @@ func (a *action) Run(c *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
a.log("commits %+v", commits) a.logDebug("commits %+v", commits)
conventionalCommits := a.getConventionalCommits(commits) conventionalCommits := a.getConventionalCommits(commits)
a.log("conventional commits %+v", conventionalCommits) a.logDebug("conventional commits %+v", conventionalCommits)
if err := a.generateChangelog(conventionalCommits); err != nil { if err := a.generateChangelog(conventionalCommits); err != nil {
return err return err
@ -137,7 +138,7 @@ func (a *action) Run(c *cli.Context) error {
} }
func (a *action) getFlags(c *cli.Context) { func (a *action) getFlags(c *cli.Context) {
a.verbose = c.Bool(verboseFlag) a.debug = c.Bool(debugFlag)
a.flags[fromFlag] = c.String(fromFlag) a.flags[fromFlag] = c.String(fromFlag)
a.flags[excludeToFlag] = c.String(excludeToFlag) a.flags[excludeToFlag] = c.String(excludeToFlag)
a.flags[includeToFlag] = c.String(includeToFlag) a.flags[includeToFlag] = c.String(includeToFlag)
@ -159,7 +160,7 @@ func (a *action) getFlagValue(c *cli.Context, flag, fallback string) string {
func (a *action) getCommits() ([]git.Commit, error) { func (a *action) getCommits() ([]git.Commit, error) {
repository := a.flags[repositoryFlag] repository := a.flags[repositoryFlag]
a.log("repository %s", repository) a.logDebug("repository %s", repository)
r, err := git.NewRepository(repository) r, err := git.NewRepository(repository)
if err != nil { if err != nil {
@ -167,13 +168,13 @@ func (a *action) getCommits() ([]git.Commit, error) {
} }
fromRev := a.flags[fromFlag] fromRev := a.flags[fromFlag]
a.log("from revision %s", fromRev) a.logDebug("from revision %s", fromRev)
excludeToRev := a.flags[excludeToFlag] excludeToRev := a.flags[excludeToFlag]
a.log("exclude to revision %s", excludeToRev) a.logDebug("exclude to revision %s", excludeToRev)
includeToRev := a.flags[includeToFlag] includeToRev := a.flags[includeToFlag]
a.log("include to revision %s", includeToRev) a.logDebug("include to revision %s", includeToRev)
if excludeToRev != "" && includeToRev != "" { if excludeToRev != "" && includeToRev != "" {
return nil, errors.New("excludeToFlag and includeToFlag can not appear same time") return nil, errors.New("excludeToFlag and includeToFlag can not appear same time")
@ -195,7 +196,7 @@ func (a *action) getConventionalCommits(commits []git.Commit) []convention.Commi
for _, commit := range commits { for _, commit := range commits {
conventionalCommit, err := convention.NewCommit(commit) conventionalCommit, err := convention.NewCommit(commit)
if err != nil { if err != nil {
a.log("failed to new conventional commits %+v: %s", commit, err) a.logDebug("failed to new conventional commits %+v: %s", commit, err)
continue continue
} }
@ -228,7 +229,7 @@ func (a *action) getOutputPath() (string, string, string) {
nameWithExt := filename + "." + filetype nameWithExt := filename + "." + filetype
path := filepath.Join(outputDir, nameWithExt) path := filepath.Join(outputDir, nameWithExt)
a.log("output path %s", path) a.logDebug("output path %s", path)
return path, filename, filetype return path, filename, filetype
} }
@ -243,7 +244,7 @@ func (a *action) getVersion() (string, error) {
return "", fmt.Errorf("invalid semver %s", version) return "", fmt.Errorf("invalid semver %s", version)
} }
a.log("version %s", version) a.logDebug("version %s", version)
return version, nil return version, nil
} }
@ -266,8 +267,8 @@ func (a *action) generateMarkdownChangelog(outputPath, version string, commits [
return nil return nil
} }
func (a *action) log(format string, v ...interface{}) { func (a *action) logDebug(format string, v ...interface{}) {
if a.verbose { if a.debug {
log.Printf(format, v...) log.Printf(format, v...)
} }
} }