From f6240686ec797ece8627faaedbd7ab7f79625e71 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Mon, 7 Feb 2022 21:22:20 +0700 Subject: [PATCH] chore: use cli default --- internal/cli/action.go | 39 +++++++++------------ internal/cli/app.go | 78 +++++++++++++++++++++++------------------- 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/internal/cli/action.go b/internal/cli/action.go index 26dc79d..5cc1fb0 100644 --- a/internal/cli/action.go +++ b/internal/cli/action.go @@ -19,16 +19,17 @@ const ( type action struct { flags struct { - verbose bool - from string - to string - version string - scopes map[string]struct{} - repository string - output string - filename string - filetype string - dryRun bool + verbose bool + from string + to string + version string + scopes map[string]struct{} + repository string + output string + filename string + filetype string + dryRun bool + interactive bool } } @@ -47,24 +48,16 @@ func (a *action) getFlags(c *cli.Context) { a.flags.scopes[scope] = struct{}{} } - a.flags.repository = a.getFlagValue(c, flagRepository, defaultRepository) - a.flags.output = a.getFlagValue(c, flagOutput, defaultOutput) - a.flags.filename = a.getFlagValue(c, flagFilename, defaultFilename) - a.flags.filetype = a.getFlagValue(c, flagFiletype, defaultFiletype) + a.flags.repository = c.String(flagRepository) + a.flags.output = c.String(flagOutput) + a.flags.filename = c.String(flagFilename) + a.flags.filetype = c.String(flagFiletype) a.flags.dryRun = c.Bool(flagDryRun) + a.flags.interactive = c.Bool(flagInteractive) a.log("flags %+v", a.flags) } -func (a *action) getFlagValue(c *cli.Context, flag, fallback string) string { - value := c.String(flag) - if value == "" { - value = fallback - } - - return value -} - func (a *action) log(format string, v ...interface{}) { if a.flags.verbose { log.Printf(format, v...) diff --git a/internal/cli/app.go b/internal/cli/app.go index 83c13c1..cf914a1 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -11,35 +11,38 @@ const ( name = "changeloguru" usage = "generate changelog from conventional commits" - flagVerbose = "verbose" - flagFrom = "from" - flagTo = "to" - flagVersion = "version" - flagScope = "scope" - flagRepository = "repository" - flagOutput = "output" - flagFilename = "filename" - flagFiletype = "filetype" - flagDryRun = "dry-run" + flagVerbose = "verbose" + flagFrom = "from" + flagTo = "to" + flagVersion = "version" + flagScope = "scope" + flagRepository = "repository" + flagOutput = "output" + flagFilename = "filename" + flagFiletype = "filetype" + flagDryRun = "dry-run" + flagInteractive = "interactive" commandGenerate = "generate" - usageGenerate = "generate changelog" - usageVerbose = "show what is going on" - usageFrom = "from `COMMIT`, which is kinda new commit" - usageTo = "to `COMMIT`, which is kinda old commit" - usageVersion = "`VERSION` to generate, follow Semantic Versioning" - usageScope = "scope to generate" - usageRepository = "`REPOSITORY` directory path" - usageOutput = "`OUTPUT` directory path" - usageFilename = "output `FILENAME`" - usageFiletype = "output `FILETYPE`" - usageDryRun = "demo run without actually changing anything" + usageGenerate = "generate changelog" + usageVerbose = "show what is going on" + usageFrom = "from `COMMIT`, which is kinda new commit" + usageTo = "to `COMMIT`, which is kinda old commit" + usageVersion = "`VERSION` to generate, follow Semantic Versioning" + usageScope = "scope to generate" + usageRepository = "`REPOSITORY` directory path" + usageOutput = "`OUTPUT` directory path" + usageFilename = "output `FILENAME`" + usageFiletype = "output `FILETYPE`" + usageDryRun = "demo run without actually changing anything" + usageInteractive = "interactive mode" ) var ( - aliasGenerate = []string{"gen"} - aliasVerbose = []string{"v"} + aliasGenerate = []string{"gen"} + aliasVerbose = []string{"v"} + aliasInteractive = []string{"i"} ) type App struct { @@ -80,29 +83,34 @@ func NewApp() *App { Usage: usageScope, }, &cli.StringFlag{ - Name: flagRepository, - Usage: usageRepository, - DefaultText: defaultRepository, + Name: flagRepository, + Usage: usageRepository, + Value: defaultRepository, }, &cli.StringFlag{ - Name: flagOutput, - Usage: usageOutput, - DefaultText: defaultOutput, + Name: flagOutput, + Usage: usageOutput, + Value: defaultOutput, }, &cli.StringFlag{ - Name: flagFilename, - Usage: usageFilename, - DefaultText: defaultFilename, + Name: flagFilename, + Usage: usageFilename, + Value: defaultFilename, }, &cli.StringFlag{ - Name: flagFiletype, - Usage: usageFiletype, - DefaultText: defaultFiletype, + Name: flagFiletype, + Usage: usageFiletype, + Value: defaultFiletype, }, &cli.BoolFlag{ Name: flagDryRun, Usage: usageDryRun, }, + &cli.BoolFlag{ + Name: flagInteractive, + Usage: usageInteractive, + Aliases: aliasInteractive, + }, }, Action: a.RunGenerate, },