2021-04-11 09:11:41 +00:00
|
|
|
package cli
|
|
|
|
|
2021-04-23 03:51:27 +00:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2021-12-11 03:38:24 +00:00
|
|
|
"github.com/make-go-great/color-go"
|
2021-04-23 03:51:27 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
2021-04-11 09:11:41 +00:00
|
|
|
|
|
|
|
const (
|
2022-02-07 13:59:20 +00:00
|
|
|
name = "changeloguru"
|
|
|
|
usage = "generate changelog from conventional commits"
|
2021-04-11 09:11:41 +00:00
|
|
|
|
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"
|
|
|
|
|
|
|
|
flagToName = "to"
|
|
|
|
flagToUsage = "to `COMMIT`, which is kinda old commit, default is oldest commit"
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
flagAutoCommitName = "auto-commit"
|
|
|
|
flagAutoCommitUsage = "enable auto commit after generating changelog"
|
2021-04-11 09:11:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-08-15 17:47:25 +00:00
|
|
|
commandGenerateAliases = []string{"g", "gen"}
|
|
|
|
flagVerboseAliases = []string{"v"}
|
|
|
|
flagInteractiveAliases = []string{"i"}
|
2021-04-11 09:11:41 +00:00
|
|
|
)
|
|
|
|
|
2021-04-23 03:51:27 +00:00
|
|
|
type App struct {
|
|
|
|
cliApp *cli.App
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApp() *App {
|
2021-04-11 09:11:41 +00:00
|
|
|
a := &action{}
|
|
|
|
|
2021-04-23 03:51:27 +00:00
|
|
|
cliApp := &cli.App{
|
2022-02-07 13:59:20 +00:00
|
|
|
Name: name,
|
|
|
|
Usage: usage,
|
2021-04-11 09:11:41 +00:00
|
|
|
Commands: []*cli.Command{
|
|
|
|
{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: commandGenerateName,
|
|
|
|
Aliases: commandGenerateAliases,
|
|
|
|
Usage: commandGenerateUsage,
|
2021-04-11 09:11:41 +00:00
|
|
|
Flags: []cli.Flag{
|
2021-04-23 03:51:27 +00:00
|
|
|
&cli.BoolFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagVerboseName,
|
|
|
|
Aliases: flagVerboseAliases,
|
|
|
|
Usage: flagVerboseUsage,
|
2021-04-23 03:51:27 +00:00
|
|
|
},
|
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
|
|
|
},
|
2021-04-11 09:11:41 +00:00
|
|
|
&cli.StringFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagFromName,
|
|
|
|
Usage: flagFromUsage,
|
2021-04-11 09:11:41 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagToName,
|
|
|
|
Usage: flagToUsage,
|
2021-04-11 09:11:41 +00:00
|
|
|
},
|
|
|
|
&cli.StringSliceFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagScopeName,
|
|
|
|
Usage: flagScopeUsage,
|
2021-04-11 09:11:41 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagRepositoryName,
|
|
|
|
Usage: flagRepositoryUsage,
|
2021-04-11 09:11:41 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagOutputName,
|
|
|
|
Usage: flagOutputUsage,
|
2021-04-11 09:11:41 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagFilenameName,
|
|
|
|
Usage: flagFilenameUsage,
|
2021-04-11 09:11:41 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagFiletypeName,
|
|
|
|
Usage: flagFiletypeUsage,
|
2021-04-11 09:11:41 +00:00
|
|
|
},
|
2021-04-14 07:31:00 +00:00
|
|
|
&cli.BoolFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagDryRunName,
|
|
|
|
Usage: flagDryRunUsage,
|
2021-04-14 07:31:00 +00:00
|
|
|
},
|
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
|
|
|
},
|
2022-07-03 16:19:14 +00:00
|
|
|
&cli.BoolFlag{
|
2022-08-15 17:47:25 +00:00
|
|
|
Name: flagAutoCommitName,
|
|
|
|
Usage: flagAutoCommitUsage,
|
2022-07-03 16:19:14 +00:00
|
|
|
},
|
2021-04-11 09:11:41 +00:00
|
|
|
},
|
|
|
|
Action: a.RunGenerate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: a.RunHelp,
|
|
|
|
}
|
|
|
|
|
2021-04-23 03:51:27 +00:00
|
|
|
return &App{
|
|
|
|
cliApp: cliApp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) Run() {
|
|
|
|
if err := a.cliApp.Run(os.Args); err != nil {
|
2022-02-07 13:59:20 +00:00
|
|
|
color.PrintAppError(name, err.Error())
|
2021-04-23 03:51:27 +00:00
|
|
|
}
|
2021-04-11 09:11:41 +00:00
|
|
|
}
|