From 6ab5957956e7250d18dcb10c637942d7788081b6 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Tue, 31 May 2022 18:09:18 +0700 Subject: [PATCH] feat: add command version --- README.md | 2 +- go.mod | 1 + go.sum | 2 ++ internal/cli/action.go | 17 ++++++++++++++++ internal/cli/action_generate.go | 2 +- internal/cli/app.go | 35 ++++++++++++++++++++------------- 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5fc06b6..45411aa 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Tool to generate `CHANGELOG.md`, `CHANGELOG.rst` from [Conventional Commits](htt - [x] Interactive mode - [ ] Unit test - [ ] Auto generate commit after gen CHANGELOG -- [ ] Add version command +- [x] Add version command ## Install diff --git a/go.mod b/go.mod index 3f825d4..6e10813 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/go-git/go-git/v5 v5.4.2 github.com/haunt98/clock-go v0.3.0 + github.com/make-go-great/buildinfo-go v0.0.1 github.com/make-go-great/color-go v0.3.0 github.com/make-go-great/ioe-go v0.4.0 github.com/make-go-great/markdown-go v0.5.0 diff --git a/go.sum b/go.sum index f72f0d9..9581015 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/make-go-great/buildinfo-go v0.0.1 h1:cMqVGVttHXC1hilwJxzZfyTF2LTvx5G6QAaFVG0KEHo= +github.com/make-go-great/buildinfo-go v0.0.1/go.mod h1:neEb4WWRyUmzQvlMqvrjiDexkS+QXZMq2B5yXLe4GS8= github.com/make-go-great/color-go v0.3.0 h1:ZVtVk/wVVsZZwSJG7PZpsY5zwIyGao5VoN48yKfSzYA= github.com/make-go-great/color-go v0.3.0/go.mod h1:G5G8IJ3PUo+vSQ+UvnfaswF8O/piVIhFJKv1mQjBGpI= github.com/make-go-great/ioe-go v0.4.0 h1:abRH8J01am7A9VsinEG0WnXDiT1CWk7Cd0auRvWuNyQ= diff --git a/internal/cli/action.go b/internal/cli/action.go index d2c6bdd..00cd4c0 100644 --- a/internal/cli/action.go +++ b/internal/cli/action.go @@ -1,8 +1,10 @@ package cli import ( + "fmt" "log" + "github.com/make-go-great/buildinfo-go" "github.com/urfave/cli/v2" ) @@ -33,6 +35,21 @@ type action struct { } } +func (a *action) RunVersion(c *cli.Context) error { + info, ok := buildinfo.Read() + if !ok { + return nil + } + + version := info.GitCommit + if info.GitTag != "" { + version = info.GitTag + } + + fmt.Println(version) + return nil +} + func (a *action) RunHelp(c *cli.Context) error { return cli.ShowAppHelp(c) } diff --git a/internal/cli/action_generate.go b/internal/cli/action_generate.go index 1d0e7eb..6d0eeb7 100644 --- a/internal/cli/action_generate.go +++ b/internal/cli/action_generate.go @@ -28,7 +28,7 @@ func (a *action) RunGenerate(c *cli.Context) error { return cli.ShowAppHelp(c) } } else { - fmt.Printf("Input version (%s):\n", usageVersion) + fmt.Printf("Input version (%s):\n", usageFlagVersion) a.flags.version = ioe.ReadInput() fmt.Printf("Input from (%s):\n", usageFrom) diff --git a/internal/cli/app.go b/internal/cli/app.go index 41f64fc..4d434ef 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -24,23 +24,25 @@ const ( flagInteractive = "interactive" commandGenerate = "generate" + commandVersion = "version" - usageGenerate = "generate changelog" - usageVerbose = "show what is going on" - usageVersion = "`VERSION` to generate, follow Semantic Versioning" - usageFrom = "from `COMMIT`, which is kinda new commit, default is latest commit" - usageTo = "to `COMMIT`, which is kinda old commit, default is oldest commit" - 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, default is true" + usageGenerate = "generate changelog" + usageCommandVersion = "version of changeloguru" + usageVerbose = "show what is going on" + usageFlagVersion = "`VERSION` to generate, follow Semantic Versioning" + usageFrom = "from `COMMIT`, which is kinda new commit, default is latest commit" + usageTo = "to `COMMIT`, which is kinda old commit, default is oldest commit" + 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, default is true" ) var ( - aliasGenerate = []string{"gen"} + aliasGenerate = []string{"g, gen"} aliasVerbose = []string{"v"} aliasInteractive = []string{"i"} ) @@ -68,7 +70,7 @@ func NewApp() *App { }, &cli.StringFlag{ Name: flagVersion, - Usage: usageVersion, + Usage: usageFlagVersion, }, &cli.StringFlag{ Name: flagFrom, @@ -115,6 +117,11 @@ func NewApp() *App { }, Action: a.RunGenerate, }, + { + Name: commandVersion, + Usage: usageCommandVersion, + Action: a.RunVersion, + }, }, Action: a.RunHelp, }