refactor(cli): move all cli related to cli pkg (#15)

* refactor: move all cli related to cli pkg

* refactor: move all usage to const

* fix: verbose flag should be inside generate command

Co-authored-by: Tran Hau <ngtranhau@gmail.com>
main
sudo pacman -Syu 2021-04-23 10:51:27 +07:00 committed by GitHub
parent 228ca6370e
commit 2a12f7649b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 35 deletions

11
main.go
View File

@ -1,15 +1,8 @@
package main package main
import ( import "github.com/haunt98/changeloguru/pkg/cli"
"os"
"github.com/haunt98/changeloguru/pkg/cli"
"github.com/haunt98/color"
)
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
if err := app.Run(os.Args); err != nil { app.Run()
color.PrintAppError(cli.AppName, err.Error())
}
} }

View File

@ -1,11 +1,18 @@
package cli package cli
import "github.com/urfave/cli/v2" import (
"os"
"github.com/haunt98/color"
"github.com/urfave/cli/v2"
)
const ( const (
AppName = "changeloguru" appName = "changeloguru"
appUsage = "generate changelog from conventional commits"
// flags // flags
verboseFlag = "verbose"
fromFlag = "from" fromFlag = "from"
toFlag = "to" toFlag = "to"
versionFlag = "version" versionFlag = "version"
@ -15,77 +22,94 @@ const (
filenameFlag = "filename" filenameFlag = "filename"
filetypeFlag = "filetype" filetypeFlag = "filetype"
dryRunFlag = "dry-run" dryRunFlag = "dry-run"
verboseFlag = "verbose"
// commands // commands
generateCommand = "generate" generateCommand = "generate"
// flag usage
verboseUsage = "show what is going on"
fromUsage = "generate from `COMMIT`"
toUsage = "generate to `COMMIT`"
versionUsage = "`VERSION` to generate, follow Semantic Versioning"
scopeUsage = "scope to generate"
repositoryUsage = "`REPOSITORY` directory path"
outputUsage = "`OUTPUT` directory path"
filenameUsage = "output `FILENAME`"
filetypeUsage = "output `FILETYPE`"
dryRunUsage = "demo run without actually changing anything"
// command usage
generateUsage = "generate changelog"
) )
var ( var (
// flags // flag aliases
verboseAliases = []string{"v"} verboseAliases = []string{"v"}
// commands // command aliases
generateAliases = []string{"gen"} generateAliases = []string{"gen"}
) )
func NewApp() *cli.App { type App struct {
cliApp *cli.App
}
func NewApp() *App {
a := &action{} a := &action{}
app := &cli.App{ cliApp := &cli.App{
Name: AppName, Name: appName,
Usage: "generate changelog from conventional commits", Usage: appUsage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Aliases: verboseAliases,
Usage: "show what is going on",
},
},
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: generateCommand, Name: generateCommand,
Aliases: generateAliases, Aliases: generateAliases,
Usage: generateUsage,
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{
Name: verboseFlag,
Aliases: verboseAliases,
Usage: verboseUsage,
},
&cli.StringFlag{ &cli.StringFlag{
Name: fromFlag, Name: fromFlag,
Usage: "generate from `COMMIT`", Usage: fromUsage,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: toFlag, Name: toFlag,
Usage: "generate to `COMMIT`", Usage: toUsage,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: versionFlag, Name: versionFlag,
Usage: "`VERSION` to generate, follow Semantic Versioning", Usage: versionUsage,
}, },
&cli.StringSliceFlag{ &cli.StringSliceFlag{
Name: scopeFlag, Name: scopeFlag,
Usage: "scope to generate", Usage: scopeUsage,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: repositoryFlag, Name: repositoryFlag,
Usage: "`REPOSITORY` directory path", Usage: repositoryUsage,
DefaultText: defaultRepository, DefaultText: defaultRepository,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: outputFlag, Name: outputFlag,
Usage: "`OUTPUT` directory path", Usage: outputUsage,
DefaultText: defaultOutput, DefaultText: defaultOutput,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: filenameFlag, Name: filenameFlag,
Usage: "output `FILENAME`", Usage: filenameUsage,
DefaultText: defaultFilename, DefaultText: defaultFilename,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: filetypeFlag, Name: filetypeFlag,
Usage: "output `FILETYPE`", Usage: filetypeUsage,
DefaultText: defaultFiletype, DefaultText: defaultFiletype,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: dryRunFlag, Name: dryRunFlag,
Usage: "demo run without actually changing anything", Usage: dryRunUsage,
}, },
}, },
Action: a.RunGenerate, Action: a.RunGenerate,
@ -94,5 +118,13 @@ func NewApp() *cli.App {
Action: a.RunHelp, Action: a.RunHelp,
} }
return app return &App{
cliApp: cliApp,
}
}
func (a *App) Run() {
if err := a.cliApp.Run(os.Args); err != nil {
color.PrintAppError(appName, err.Error())
}
} }