diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 48e6695..d58abe2 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -162,6 +162,8 @@ jobs: fetch-depth: 0 - name: Compile binary uses: thatisuday/go-cross-build@v1 + env: + INPUT_LDFLAGS: "-s -w -X main.PKG_VERSION=${{ needs.prepare.outputs.RELEASE_VERSION }}" with: platforms: linux/amd64,darwin/amd64,windows/amd64,linux/arm64,darwin/amd64 name: semver-gen diff --git a/Makefile b/Makefile index 3836a48..4ccf2ff 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,9 @@ +LOCAL_VERSION?=$(shell semver-gen generate -l -c config-release.yaml | sed -e 's|SEMVER ||g') + all: build build: - @go build -o semver-gen *.go + go build -o semver-gen -ldflags="-s -w -X main.PKG_VERSION=${LOCAL_VERSION}" *.go run: build @./semver-gen diff --git a/cmd/main.go b/cmd/main.go index 01cacef..e9214b1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -32,8 +32,9 @@ import ( ) var ( - err error - repo *Setup + err error + repo *Setup + PKG_VERSION string ) type Wording struct { @@ -177,6 +178,10 @@ func (s *Setup) getSemver() string { } func main() { + if varShowVersion { + fmt.Println("semver-gen", PKG_VERSION, "\tMore information: https://github.com/lukaszraczylo/semver-generator") + return + } if repo.Generate { err := repo.ReadConfig(repo.LocalConfigFile) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 9d1394b..63ecbae 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,7 +30,9 @@ var rootCmd = &cobra.Command{ Effortless semantic version generator with git commit keywords matching, allowing you to focus on the development. Visit https://github.com/lukaszraczylo/semver-generator for more information, documentation and examples.`, - Run: func(cmd *cobra.Command, args []string) {}, + Run: func(cmd *cobra.Command, args []string) { + main() + }, } func Execute() { @@ -55,6 +57,7 @@ func (r *Setup) setupCobra() { var ( varRepoName, varLocalCfg string varUseLocal bool + varShowVersion bool ) func init() { @@ -64,5 +67,6 @@ func init() { rootCmd.PersistentFlags().StringVarP(&varRepoName, "repository", "r", "https://github.com/lukaszraczylo/simple-gql-client", "Remote repository URL.") rootCmd.PersistentFlags().StringVarP(&varLocalCfg, "config", "c", "config.yaml", "Path to config file") rootCmd.PersistentFlags().BoolVarP(&varUseLocal, "local", "l", false, "Use local repository") + rootCmd.PersistentFlags().BoolVarP(&varShowVersion, "version", "v", false, "Display version") } } diff --git a/main.go b/main.go index dc04915..d3c7fbe 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,11 @@ package main import "github.com/lukaszraczylo/semver-generator/cmd" +var ( + PKG_VERSION string +) + func main() { + cmd.PKG_VERSION = PKG_VERSION cmd.Execute() }