Skip to content

Commit cd9fcff

Browse files
committed
添加版本打印
1 parent b173dcd commit cd9fcff

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
version := $(shell git describe --tags --always)
2+
build_time := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
3+
14
build:
25
CGO_ENABLED=0 go build -ldflags="-s -w"
36

47
build-use-env:
5-
CGO_ENABLED=0 go build -ldflags="-s -w" -o build/schema-diff-${GOOS}-${GOARCH}
8+
CGO_ENABLED=0 go build -ldflags="-s -w -X 'main.Version=$(version)' -X 'main.BuildTime=$(build_time)'" -o build/schema-diff-${GOOS}-${GOARCH}
69

710
all:
811
make GOARCH=amd64 GOOS=linux build-use-env

main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"debug/buildinfo"
45
"flag"
56
"fmt"
67
"log"
@@ -23,6 +24,9 @@ var (
2324
skipTables string
2425
IgnoreCharset bool
2526
config *conf.Conf
27+
PrintVersion bool
28+
Version string
29+
BuildTime string
2630
)
2731

2832
func init() {
@@ -36,9 +40,15 @@ func init() {
3640
fs.StringVar(&MysqlVersion, "mysql-version", "", "mysql版本,仅使用文件比对时需要指定,默认值为:"+vtconfig.DefaultMySQLVersion)
3741
fs.StringVar(&skipTables, "skip-tables", "", "跳过特定表比对,多个表使用逗号分隔")
3842
fs.BoolVar(&IgnoreCharset, "ignore-charset", false, "忽略字符集比对")
43+
fs.BoolVar(&PrintVersion, "version", false, "打印版本信息")
3944

4045
fs.Parse(os.Args[1:])
4146

47+
if PrintVersion {
48+
printVersion()
49+
os.Exit(0)
50+
}
51+
4252
var err error
4353

4454
config, err = conf.NewConfFromFile(confFileName)
@@ -130,3 +140,34 @@ func main() {
130140
log.Printf("diff结果SQL文件生成成功[%s]", config.SaveSqlPath)
131141
}
132142
}
143+
144+
func printVersion() {
145+
if Version == "" {
146+
Version = "dev"
147+
}
148+
fmt.Printf("version: \t%s\n", Version)
149+
if BuildTime == "" {
150+
BuildTime = "unknown"
151+
}
152+
fmt.Printf("构建时间: \t%s\n", BuildTime)
153+
154+
selfFile := os.Args[0]
155+
res, err := buildinfo.ReadFile(selfFile)
156+
if err != nil {
157+
os.Exit(0)
158+
}
159+
160+
fmt.Printf("Go版本: \t%s\n", res.GoVersion)
161+
162+
for _, item := range res.Settings {
163+
if item.Key == "vcs.revision" {
164+
fmt.Printf("commit id: \t%s\n", item.Value)
165+
}
166+
if item.Key == "vcs.time" {
167+
fmt.Printf("commit time: \t%s\n", item.Value)
168+
}
169+
if item.Key == "vcs.modified" {
170+
fmt.Printf("dirty: \t\t%s\n", item.Value)
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)