diff --git a/Makefile b/Makefile index 3e4030f..2ec45e2 100644 --- a/Makefile +++ b/Makefile @@ -1,60 +1,48 @@ -.PHONY: default init common-build install release dep get_deps common-clean build_amd64 build_386 upx +BUILD_VERSION := $(shell cat version) +BUILD_TIME := $(shell date "+%F %T") +COMMIT_SHA1 := $(shell git rev-parse HEAD) +ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/ +DIST_DIR := $(ROOT_DIR)/dist/ -# https://golang.org/doc/install/source#environment -GOOS := $(shell go env | awk -F= '$$1=="GOOS" {print $$2}' | awk -F '"' '{print $$2}') # 此处 awk 需使用两个 $ -GOARCH := $(shell go env | awk -F= '$$1=="GOARCH" {print $$2}' | awk -F '"' '{print $$2}') -OSS = darwin dragonfly freebsd linux netbsd openbsd plan9 solaris windows -PKG = -# ifeq ($(strip $(GOOS)), windows) -# GOARCH := $(strip $(GOARCH)).exe -# endif +VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/cmd +LD_GIT_COMMIT := -X '$(VERSION_PATH).GitCommit=$(COMMIT_SHA1)' +LD_BUILD_TIME := -X '$(VERSION_PATH).BuildTime=$(BUILD_TIME)' +LD_GO_VERSION := -X '$(VERSION_PATH).GoVersion=`go version`' +LD_GATEWAY_VERSION := -X '$(VERSION_PATH).Version=$(BUILD_VERSION)' +LD_FLAGS := "$(LD_GIT_COMMIT) $(LD_BUILD_TIME) $(LD_GO_VERSION) $(LD_GATEWAY_VERSION) -w -s" -# This rule is used to forward a target like "build" to "common-build". This -# allows a new "build" target to be defined in a Makefile which includes this -# one and override "common-build" without override warnings. -%: common-% ; +.PHONY : build release clean install upx -default: - @echo "JWT info: please choose a target for 'make'" - @echo "available target: init build install release dep get_deps clean build_amd64 build_386 upx" +build: +ifneq ($(shell type gox >/dev/null 2>&1;echo $$?), 0) + @echo "Can't find gox command, will start installation..." + GO111MODULE=off go get -v -u github.com/mitchellh/gox +endif + # @$(if $(findstring 0,$(shell type gox >/dev/null 2>&1;echo $$?)),,echo "Can't find gox command, will start installation...";GO111MODULE=off go get -v -u github.com/mitchellh/gox) + gox -ldflags $(LD_FLAGS) -osarch="darwin/amd64 linux/386 linux/amd64 windows/amd64" \ + -output="$(DIST_DIR){{.Dir}}_{{.OS}}_{{.Arch}}" -init: get-deps dep clean - go install -ldflags "-s -w" - -common-build: - @ go build -ldflags "-s -w" -o dist/jwt_$(strip $(GOOS))_$(strip $(if \ - $(findstring windows,$(GOOS)),\ - $(strip $(GOARCH)).exe,\ - $(strip $(GOARCH))\ - )) +clean: + rm -rf $(DIST_DIR)* install: - go install -ldflags "-s -w" - -release: build_amd64 build_386 upx - -dep: - dep ensure - -get_deps: - go get -u github.com/golang/dep/cmd/dep - -common-clean: - go clean -i - rm -rf dist/jwt* jwt* - -build_amd64: - @ $(foreach OS,\ - $(OSS),\ - $(shell CGO_ENABLED=0 GOOS=$(OS) GOARCH=amd64 go build -ldflags "-s -w" -o dist/jwt_$(OS)_amd64$(if $(findstring windows,$(OS)),.exe))) - @ echo done - -build_386: - @ $(foreach OS,\ - $(OSS),\ - $(shell CGO_ENABLED=0 GOOS=$(OS) GOARCH=386 go build -ldflags "-s -w" -o dist/jwt_$(OS)_386$(if $(findstring windows,$(OS)),.exe))) - @ echo done + go install -ldflags $(LD_FLAGS) # 压缩。需要安装 https://github.com/upx/upx upx: - upx $(if $(PKG),$(PKG),dist/jwt*) \ No newline at end of file + upx $(DIST_DIR)** + +release: build upx +ifneq ($(shell type ghr >/dev/null 2>&1;echo $$?), 0) + @echo "Can't find ghr command, will start installation..." + GO111MODULE=off go get -v -u github.com/tcnksm/ghr +endif + # @$(if $(findstring 0,$(shell type ghr >/dev/null 2>&1;echo $$?)),,echo "Can't find ghr command, will start installation...";GO111MODULE=off go get -v -u github.com/tcnksm/ghr) + ghr -u ehlxr -t $(GITHUB_RELEASE_TOKEN) -replace -delete --debug ${BUILD_VERSION} $(DIST_DIR) + +# this tells 'make' to export all variables to child processes by default. +.EXPORT_ALL_VARIABLES: + +GO111MODULE = on +GOPROXY = https://goproxy.cn,direct +GOSUMDB = sum.golang.google.cn diff --git a/cmd/root.go b/cmd/root.go index 3c25140..28c57e4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -37,7 +37,6 @@ var rootCmd = &cobra.Command{ Short: "JWT(Json Web Token) 工具", Long: `JWT(Json Web Token) 工具 用于生成、验证、查看 JWT`, - Version: "v0.0.2", } // Execute adds all child commands to the root command and sets flags appropriately. @@ -54,7 +53,7 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.jwt.yaml)") // rootCmd.Flags().BoolP("help", "h", false, "path to JWT token to verify or '-' to read from stdin") - rootCmd.Flags().BoolP("version", "v", false, "show version of the jwt.") + // rootCmd.Flags().BoolP("version", "v", false, "show version of the jwt.") } // initConfig reads in config file and ENV variables if set. diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..c510ce6 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,62 @@ +/* +Copyright © 2019 ehlxr + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "encoding/base64" + "fmt" + + "github.com/spf13/cobra" +) + +var bannerBase64 = "DQogICBfXyAgICAgX18gICAgIF9fICAgICBfX19fX18gIA0KICAvXCBcICAgL1wgXCAgXyBcIFwgICAvXF9fICBfXCANCiBfXF9cIFwgIFwgXCBcLyAiLlwgXCAgXC9fL1wgXC8gDQovXF9fX19fXCAgXCBcX18vIi5+XF9cICAgIFwgXF9cIA0KXC9fX19fXy8gICBcL18vICAgXC9fLyAgICAgXC9fLyANCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgDQo=" +var versionTpl = `%s + +Name: jwt +Version: %s +BuildTime: %s +GitCommit: %s +GoVersion: %s +` + +var ( + Version string + BuildTime string + GitCommit string + GoVersion string +) + +// versionCmd represents the version command +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print version", + Long: ` +Print version of jwt`, + Run: func(cmd *cobra.Command, args []string) { + banner, _ := base64.StdEncoding.DecodeString(bannerBase64) + fmt.Printf(versionTpl, banner, Version, BuildTime, GitCommit, GoVersion) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/version b/version new file mode 100644 index 0000000..6a2b0ac --- /dev/null +++ b/version @@ -0,0 +1 @@ +v1.0.1 \ No newline at end of file