release v1.0.1
This commit is contained in:
parent
863442bbce
commit
899d0dc08f
74
Makefile
74
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
|
VERSION_PATH := $(shell cat `go env GOMOD` | awk '/^module/{print $$2}')/metadata
|
||||||
GOOS := $(shell goenv | awk -F= '$$1=="GOOS" {print $$2}' | awk -F '"' '{print $$2}') # 此处 awk 需使用两个 $
|
LD_GIT_COMMIT := -X '$(VERSION_PATH).GitCommit=$(COMMIT_SHA1)'
|
||||||
GOARCH := $(shell go env | awk -F= '$$1=="GOARCH" {print $$2}' | awk -F '"' '{print $$2}')
|
LD_BUILD_TIME := -X '$(VERSION_PATH).BuildTime=$(BUILD_TIME)'
|
||||||
OSS = darwin dragonfly freebsd linux netbsd openbsd plan9 solaris windows
|
LD_GO_VERSION := -X '$(VERSION_PATH).GoVersion=`go version`'
|
||||||
PKG =
|
LD_GATEWAY_VERSION := -X '$(VERSION_PATH).Version=$(BUILD_VERSION)'
|
||||||
# ifeq ($(strip $(GOOS)), windows)
|
LD_FLAGS := "$(LD_GIT_COMMIT) $(LD_BUILD_TIME) $(LD_GO_VERSION) $(LD_GATEWAY_VERSION) -w -s"
|
||||||
# GOARCH := $(strip $(GOARCH)).exe
|
|
||||||
# endif
|
|
||||||
|
|
||||||
default:
|
.PHONY : build release clean install upx
|
||||||
@echo "hc info: please choose a target for 'make'"
|
|
||||||
@echo "available target: build install release clean build_amd64 build_386 upx"
|
|
||||||
|
|
||||||
build:
|
build:
|
||||||
@ go build -ldflags "-s -w" -o dist/hc_$(strip $(GOOS))_$(strip $(if \
|
ifneq ($(shell type gox >/dev/null 2>&1;echo $$?), 0)
|
||||||
$(findstring windows,$(GOOS)),\
|
@echo "Can't find gox command, will start installation..."
|
||||||
$(strip $(GOARCH)).exe,\
|
GO111MODULE=off go get -v -u github.com/mitchellh/gox
|
||||||
$(strip $(GOARCH))\
|
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" \
|
||||||
install:
|
-output="$(DIST_DIR){{.Dir}}_{{.OS}}_{{.Arch}}"
|
||||||
go install -ldflags "-s -w"
|
|
||||||
|
|
||||||
release: build_amd64 build_386 upx
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
go clean -i
|
rm -rf $(DIST_DIR)*
|
||||||
rm -rf dist/hc* hc* hex-convert
|
|
||||||
|
|
||||||
build_amd64:
|
install:
|
||||||
@ $(foreach OS,\
|
go install -ldflags $(LD_FLAGS)
|
||||||
$(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
|
|
||||||
|
|
||||||
# 压缩。需要安装 https://github.com/upx/upx
|
# 压缩。需要安装 https://github.com/upx/upx
|
||||||
upx:
|
upx:
|
||||||
upx $(if $(PKG),$(PKG),dist/hc*)
|
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
|
||||||
|
11
cmd/root.go
11
cmd/root.go
@ -24,7 +24,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/ehlxr/hex-convert/metadata"
|
|
||||||
homedir "github.com/mitchellh/go-homedir"
|
homedir "github.com/mitchellh/go-homedir"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@ -33,10 +32,10 @@ import (
|
|||||||
var cfgFile string
|
var cfgFile string
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "hc",
|
Use: "hc",
|
||||||
Short: "进制转换",
|
Short: "进制转换",
|
||||||
Long: `支持 76 位以内的任意进制相互转换`,
|
Long: `支持 76 位以内的任意进制相互转换`,
|
||||||
Version: metadata.VERSION,
|
// Version: metadata.VERSION,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
@ -51,7 +50,7 @@ func Execute() {
|
|||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
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().IntP("scale", "s", 0, "要转换的进制")
|
||||||
rootCmd.PersistentFlags().StringP("data", "d", "", "要转换数值")
|
rootCmd.PersistentFlags().StringP("data", "d", "", "要转换数值")
|
||||||
}
|
}
|
||||||
|
55
cmd/version.go
Normal file
55
cmd/version.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2019 ehlxr <ehlxr.me@gmail.com>
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
@ -20,6 +20,13 @@
|
|||||||
|
|
||||||
package metadata
|
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"}
|
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"}
|
||||||
|
149
server/server.go
149
server/server.go
@ -1,119 +1,48 @@
|
|||||||
// Copyright © 2018 ehlxr <ehlxr.me@gmail.com>
|
BUILD_VERSION := $(shell cat version)
|
||||||
//
|
BUILD_TIME := $(shell date "+%F %T")
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
COMMIT_SHA1 := $(shell git rev-parse HEAD)
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))/
|
||||||
// in the Software without restriction, including without limitation the rights
|
DIST_DIR := $(ROOT_DIR)/dist/
|
||||||
// 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 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 (
|
.PHONY : build release clean install upx
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"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) {
|
install:
|
||||||
scale, _ := strconv.Atoi(req.FormValue("scale"))
|
go install -ldflags $(LD_FLAGS)
|
||||||
data := req.FormValue("data")
|
|
||||||
|
|
||||||
result, err := converter.ToDecimal(scale, data)
|
# 压缩。需要安装 https://github.com/upx/upx
|
||||||
if err != nil {
|
upx:
|
||||||
fmt.Println(err)
|
upx $(DIST_DIR)**
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println(result)
|
release: build upx
|
||||||
_, _ = fmt.Fprintf(w, "<a href='/'>首页</a><br> %s", strconv.Itoa(result))
|
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) {
|
# this tells 'make' to export all variables to child processes by default.
|
||||||
scale, _ := strconv.Atoi(req.FormValue("scale"))
|
.EXPORT_ALL_VARIABLES:
|
||||||
data := req.FormValue("data")
|
|
||||||
|
|
||||||
result, err := converter.ToBinary(scale, data)
|
GO111MODULE = on
|
||||||
if err != nil {
|
GOPROXY = https://goproxy.cn,direct
|
||||||
fmt.Println(err)
|
GOSUMDB = sum.golang.google.cn
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println(result)
|
|
||||||
_, _ = fmt.Fprintf(w, "<a href='/'>首页</a><br> %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, "<a href='/'>首页</a><br>二进制:%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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user