From 899d0dc08f08c8d97a37aa6d0a0e8765c12a71ed Mon Sep 17 00:00:00 2001 From: ehlxr Date: Wed, 2 Oct 2019 17:15:22 +0800 Subject: [PATCH] release v1.0.1 --- Makefile | 74 ++++++++++----------- cmd/root.go | 11 ++-- cmd/version.go | 55 ++++++++++++++++ metadata/metadata.go | 9 ++- server/server.go | 149 +++++++++++-------------------------------- vesion | 1 + 6 files changed, 146 insertions(+), 153 deletions(-) create mode 100644 cmd/version.go create mode 100644 vesion diff --git a/Makefile b/Makefile index eb0bada..d944a1b 100644 --- a/Makefile +++ b/Makefile @@ -1,46 +1,48 @@ -.PHONY: default init build install release dep get_deps 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 goenv | 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}')/metadata +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" -default: - @echo "hc info: please choose a target for 'make'" - @echo "available target: build install release clean build_amd64 build_386 upx" +.PHONY : build release clean install upx build: - @ go build -ldflags "-s -w" -o dist/hc_$(strip $(GOOS))_$(strip $(if \ - $(findstring windows,$(GOOS)),\ - $(strip $(GOARCH)).exe,\ - $(strip $(GOARCH))\ - )) - -install: - go install -ldflags "-s -w" - -release: build_amd64 build_386 upx +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}}" clean: - go clean -i - rm -rf dist/hc* hc* hex-convert + rm -rf $(DIST_DIR)* -build_amd64: - @ $(foreach OS,\ - $(OSS),\ - $(shell CGO_ENABLED=0 GOOS=$(OS) GOARCH=amd64 go build -ldflags "-s -w" -o dist/hc_$(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/hc_$(OS)_386$(if $(findstring windows,$(OS)),.exe))) - @ echo done +install: + go install -ldflags $(LD_FLAGS) # 压缩。需要安装 https://github.com/upx/upx upx: - upx $(if $(PKG),$(PKG),dist/hc*) \ 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 b1fe9b3..369dd70 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,7 +24,6 @@ import ( "fmt" "os" - "github.com/ehlxr/hex-convert/metadata" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -33,10 +32,10 @@ import ( var cfgFile string var rootCmd = &cobra.Command{ - Use: "hc", - Short: "进制转换", - Long: `支持 76 位以内的任意进制相互转换`, - Version: metadata.VERSION, + Use: "hc", + Short: "进制转换", + Long: `支持 76 位以内的任意进制相互转换`, + // Version: metadata.VERSION, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -51,7 +50,7 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - rootCmd.Flags().BoolP("version", "v", false, "show version of the hc.") + // rootCmd.Flags().BoolP("version", "v", false, "show version of the hc.") rootCmd.PersistentFlags().IntP("scale", "s", 0, "要转换的进制") rootCmd.PersistentFlags().StringP("data", "d", "", "要转换数值") } diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..81067f6 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,55 @@ +/* +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/ehlxr/hex-convert/metadata" + "github.com/spf13/cobra" +) + +var versionTpl = `%s + +Name: hc +Version: %s +BuildTime: %s +GitCommit: %s +GoVersion: %s +` + +// 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, metadata.Version, metadata.BuildTime, metadata.GitCommit, metadata.GoVersion) + }, +} + +func init() { + rootCmd.AddCommand(versionCmd) +} diff --git a/metadata/metadata.go b/metadata/metadata.go index d7a387c..50c5b31 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -20,6 +20,13 @@ package metadata -const VERSION = "v0.0.1" +var ( + Version string + BuildTime string + GitCommit string + GoVersion string + + bannerBase64 = "DQogICBfXyAgICAgX18gICAgIF9fICAgICBfX19fX18gIA0KICAvXCBcICAgL1wgXCAgXyBcIFwgICAvXF9fICBfXCANCiBfXF9cIFwgIFwgXCBcLyAiLlwgXCAgXC9fL1wgXC8gDQovXF9fX19fXCAgXCBcX18vIi5+XF9cICAgIFwgXF9cIA0KXC9fX19fXy8gICBcL18vICAgXC9fLyAgICAgXC9fLyANCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgDQo=" +) var TEN_TO_ANY map[int]string = map[int]string{0: "0", 1: "1", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "a", 11: "b", 12: "c", 13: "d", 14: "e", 15: "f", 16: "g", 17: "h", 18: "i", 19: "j", 20: "k", 21: "l", 22: "m", 23: "n", 24: "o", 25: "p", 26: "q", 27: "r", 28: "s", 29: "t", 30: "u", 31: "v", 32: "w", 33: "x", 34: "y", 35: "z", 36: ":", 37: ";", 38: "<", 39: "=", 40: ">", 41: "?", 42: "@", 43: "[", 44: "]", 45: "^", 46: "_", 47: "{", 48: "|", 49: "}", 50: "A", 51: "B", 52: "C", 53: "D", 54: "E", 55: "F", 56: "G", 57: "H", 58: "I", 59: "J", 60: "K", 61: "L", 62: "M", 63: "N", 64: "O", 65: "P", 66: "Q", 67: "R", 68: "S", 69: "T", 70: "U", 71: "V", 72: "W", 73: "X", 74: "Y", 75: "Z"} diff --git a/server/server.go b/server/server.go index 18a6237..2ec45e2 100644 --- a/server/server.go +++ b/server/server.go @@ -1,119 +1,48 @@ -// Copyright © 2018 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. +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/ -package server +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" -import ( - "fmt" - "io/ioutil" - "log" - "net/http" - "os" - "strconv" - "strings" +.PHONY : build release clean install upx - "github.com/ehlxr/hex-convert/gen" +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}}" - "github.com/ehlxr/hex-convert/converter" -) +clean: + rm -rf $(DIST_DIR)* -func decimal(w http.ResponseWriter, req *http.Request) { - scale, _ := strconv.Atoi(req.FormValue("scale")) - data := req.FormValue("data") +install: + go install -ldflags $(LD_FLAGS) - result, err := converter.ToDecimal(scale, data) - if err != nil { - fmt.Println(err) - os.Exit(1) - } +# 压缩。需要安装 https://github.com/upx/upx +upx: + upx $(DIST_DIR)** - log.Println(result) - _, _ = fmt.Fprintf(w, "首页
%s", strconv.Itoa(result)) -} +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) -func binary(w http.ResponseWriter, req *http.Request) { - scale, _ := strconv.Atoi(req.FormValue("scale")) - data := req.FormValue("data") +# this tells 'make' to export all variables to child processes by default. +.EXPORT_ALL_VARIABLES: - result, err := converter.ToBinary(scale, data) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - log.Println(result) - _, _ = fmt.Fprintf(w, "首页
%s", result) -} - -func submit(w http.ResponseWriter, req *http.Request) { - scale, _ := strconv.Atoi(req.FormValue("scale")) - data := req.FormValue("data") - - var bd, od, dd, hd string - if scale != 2 && scale != 8 && scale != 10 && scale != 16 { - bd, od, dd, hd = "", "", "", "" - } else { - bd, _ = converter.ToBinary(scale, data) - d, err := converter.ToDecimal(scale, data) - if err == nil { - dd = strconv.Itoa(d) - } - - od, _ = converter.ToOctal(scale, data) - hd, _ = converter.ToHex(scale, data) - } - - log.Printf("%s %s %s %s", bd, od, dd, hd) - _, _ = fmt.Fprintf(w, "首页
二进制:%s 八进制:%s 十进制:%s 十六进制:%s", bd, od, dd, hd) -} - -func index(w http.ResponseWriter, r *http.Request) { - f, err := gen.Assets.Open("/index.tpl") - if err != nil { - log.Fatalln(err) - } - defer f.Close() - - fd, err := ioutil.ReadAll(f) - if err != nil { - panic(err) - } - - _, _ = fmt.Fprintf(w, string(fd)) -} - -func Start(host string, port int) error { - http.HandleFunc("/", index) - http.HandleFunc("/d", decimal) - http.HandleFunc("/b", binary) - http.HandleFunc("/s", submit) - - addr := fmt.Sprintf("%s:%d", host, port) - if strings.Contains(addr, "0.0.0.0") { - addr = strings.Replace(addr, "0.0.0.0", "", 1) - host = strings.Replace(host, "0.0.0.0", "127.0.0.1", 1) - } - fmt.Printf("server start on: %s\n", fmt.Sprintf("http://%s:%d", host, port)) - - if err := http.ListenAndServe(addr, nil); err != nil { - return fmt.Errorf("ListenAndServe: : %v", err) - } - return nil -} +GO111MODULE = on +GOPROXY = https://goproxy.cn,direct +GOSUMDB = sum.golang.google.cn diff --git a/vesion b/vesion new file mode 100644 index 0000000..6a2b0ac --- /dev/null +++ b/vesion @@ -0,0 +1 @@ +v1.0.1 \ No newline at end of file