changeloguru/internal/cli/app.go

172 lines
4.1 KiB
Go
Raw Normal View History

package cli
import (
"os"
"github.com/urfave/cli/v2"
2022-11-27 18:28:59 +00:00
"github.com/make-go-great/color-go"
)
const (
name = "changeloguru"
usage = "generate changelog from conventional commits"
2022-08-15 17:47:25 +00:00
commandGenerateName = "generate"
commandGenerateUsage = "generate changelog"
flagVerboseName = "verbose"
flagVerboseUsage = "show what is going on"
flagVersionName = "version"
flagVersionUsage = "`VERSION` to generate, follow Semantic Versioning"
flagFromName = "from"
flagFromUsage = "`FROM COMMIT`, which is kinda new commit, default is latest commit"
2022-08-15 17:47:25 +00:00
flagToName = "to"
flagToUsage = "`TO COMMIT`, which is kinda old commit, default is oldest commit"
2022-08-15 17:47:25 +00:00
flagScopeName = "scope"
flagScopeUsage = "scope to generate"
flagRepositoryName = "repository"
flagRepositoryUsage = "`REPOSITORY` directory path"
flagOutputName = "output"
flagOutputUsage = "`OUTPUT` directory path, relative to `REPOSITORY` path"
flagFilenameName = "filename"
flagFilenameUsage = "output `FILENAME`"
flagFiletypeName = "filetype"
flagFiletypeUsage = "output `FILETYPE`"
flagDryRunName = "dry-run"
flagDryRunUsage = "demo run without actually changing anything"
flagInteractiveName = "interactive"
flagInteractiveUsage = "interactive mode"
flagInteractiveFromName = "interactive-from"
flagInteractiveFromUsage = "enable ask `FROM COMMIT` in interactive mode"
flagInteractiveToName = "interactive-to"
flagInteractiveToUsage = "enable ask `TO COMMIT` in interactive mode, otherwise use latest SemVer tag"
2022-09-04 15:44:57 +00:00
flagAutoGitCommitName = "auto-commit"
flagAutoGitCommitUsage = "enable auto git commit after generating changelog"
flagAutoGitTagName = "auto-tag"
flagAutoGitTagUsage = "enable auto git tag after generating changelog, only works if auto-commit is enabled"
flagAutoGitPushName = "auto-push"
flagAutoGitPushUsage = "enable auto git push after generating changelog, only works if auto-commit is enabled, if auto-tag is enabled will auto git push tag too"
)
var (
2022-08-15 17:47:25 +00:00
commandGenerateAliases = []string{"g", "gen"}
flagVerboseAliases = []string{"v"}
flagInteractiveAliases = []string{"i"}
)
type App struct {
cliApp *cli.App
}
func NewApp() *App {
a := &action{}
cliApp := &cli.App{
Name: name,
Usage: usage,
Commands: []*cli.Command{
{
2022-08-15 17:47:25 +00:00
Name: commandGenerateName,
Aliases: commandGenerateAliases,
Usage: commandGenerateUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
2022-08-15 17:47:25 +00:00
Name: flagVerboseName,
Aliases: flagVerboseAliases,
Usage: flagVerboseUsage,
},
2022-02-07 15:52:35 +00:00
&cli.StringFlag{
2022-08-15 17:47:25 +00:00
Name: flagVersionName,
Usage: flagVersionUsage,
2022-02-07 15:52:35 +00:00
},
&cli.StringFlag{
2022-08-15 17:47:25 +00:00
Name: flagFromName,
Usage: flagFromUsage,
},
&cli.StringFlag{
2022-08-15 17:47:25 +00:00
Name: flagToName,
Usage: flagToUsage,
},
&cli.StringSliceFlag{
2022-08-15 17:47:25 +00:00
Name: flagScopeName,
Usage: flagScopeUsage,
},
&cli.StringFlag{
2022-08-15 17:47:25 +00:00
Name: flagRepositoryName,
Usage: flagRepositoryUsage,
},
&cli.StringFlag{
2022-08-15 17:47:25 +00:00
Name: flagOutputName,
Usage: flagOutputUsage,
},
&cli.StringFlag{
2022-08-15 17:47:25 +00:00
Name: flagFilenameName,
Usage: flagFilenameUsage,
},
&cli.StringFlag{
2022-08-15 17:47:25 +00:00
Name: flagFiletypeName,
Usage: flagFiletypeUsage,
},
&cli.BoolFlag{
2022-08-15 17:47:25 +00:00
Name: flagDryRunName,
Usage: flagDryRunUsage,
},
2022-02-07 14:22:20 +00:00
&cli.BoolFlag{
2022-08-15 17:47:25 +00:00
Name: flagInteractiveName,
Usage: flagInteractiveUsage,
Aliases: flagInteractiveAliases,
2022-02-07 14:22:20 +00:00
},
&cli.BoolFlag{
Name: flagInteractiveFromName,
Usage: flagInteractiveFromUsage,
},
&cli.BoolFlag{
Name: flagInteractiveToName,
Usage: flagInteractiveToUsage,
},
&cli.BoolFlag{
2022-09-04 15:44:57 +00:00
Name: flagAutoGitCommitName,
Usage: flagAutoGitCommitUsage,
},
2022-09-04 16:30:16 +00:00
&cli.BoolFlag{
Name: flagAutoGitTagName,
Usage: flagAutoGitTagUsage,
},
&cli.BoolFlag{
Name: flagAutoGitPushName,
Usage: flagAutoGitPushUsage,
},
},
Action: a.RunGenerate,
},
},
Action: a.RunHelp,
}
return &App{
cliApp: cliApp,
}
}
func (a *App) Run() {
if err := a.cliApp.Run(os.Args); err != nil {
color.PrintAppError(name, err.Error())
}
}