diff --git a/.golangci.yaml b/.golangci.yaml index 840ceeaf5..009160954 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -54,6 +54,8 @@ linters-settings: - "fmt.Print" - "fmt.Printf" - "fmt.Println" + - "strings.Builder.WriteString" + - "strings.Builder.WriteByte" issues: diff --git a/Makefile b/Makefile index 07f1ba835..706af90e4 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,8 @@ SHELL := /bin/bash GO := go GCFLAGS := -gcflags=all='-N -l' +LDFLAGS := -X 'github.com/kakao/varlog/internal/buildinfo.version=$(shell git describe --abbrev=0 --tags)' +LDFLAGS := -ldflags "$(LDFLAGS)" GOPATH := $(shell $(GO) env GOPATH) PKGS := $(shell $(GO) list ./... | \ egrep -v "github.com/kakao/varlog/vendor" | \ @@ -39,19 +41,19 @@ BENCHMARK := $(BIN_DIR)/benchmark .PHONY: build vmr varlogadm varlogsn varlogctl varlogcli mrtool benchmark build: vmr varlogadm varlogsn varlogctl varlogcli mrtool benchmark vmr: - $(GO) build $(GCFLAGS) -o $(VMR) $(CURDIR)/cmd/varlogmr + $(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VMR) $(CURDIR)/cmd/varlogmr varlogadm: - $(GO) build $(GCFLAGS) -o $(VARLOGADM) $(CURDIR)/cmd/varlogadm + $(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VARLOGADM) $(CURDIR)/cmd/varlogadm varlogsn: - $(GO) build $(GCFLAGS) -o $(VARLOGSN) $(CURDIR)/cmd/varlogsn + $(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VARLOGSN) $(CURDIR)/cmd/varlogsn varlogctl: - $(GO) build $(GCFLAGS) -o $(VARLOGCTL) $(CURDIR)/cmd/varlogctl + $(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VARLOGCTL) $(CURDIR)/cmd/varlogctl varlogcli: - $(GO) build $(GCFLAGS) -o $(VARLOGCLI) $(CURDIR)/cmd/varlogcli + $(GO) build $(GCFLAGS) $(LDFLAGS) -o $(VARLOGCLI) $(CURDIR)/cmd/varlogcli mrtool: - $(GO) build $(GCFLAGS) -o $(MRTOOL) $(CURDIR)/cmd/mrtool + $(GO) build $(GCFLAGS) $(LDFLAGS) -o $(MRTOOL) $(CURDIR)/cmd/mrtool benchmark: - $(GO) build $(GCFLAGS) -o $(BENCHMARK) $(CURDIR)/cmd/benchmark + $(GO) build $(GCFLAGS) $(LDFLAGS) -o $(BENCHMARK) $(CURDIR)/cmd/benchmark # testing diff --git a/cmd/varlogadm/cli.go b/cmd/varlogadm/cli.go index f9aa9a403..c127fb1df 100644 --- a/cmd/varlogadm/cli.go +++ b/cmd/varlogadm/cli.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "github.com/urfave/cli/v2" "go.uber.org/zap" @@ -10,6 +11,7 @@ import ( "github.com/kakao/varlog/internal/admin/mrmanager" "github.com/kakao/varlog/internal/admin/snmanager" "github.com/kakao/varlog/internal/admin/snwatcher" + "github.com/kakao/varlog/internal/buildinfo" "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/pkg/types" "github.com/kakao/varlog/pkg/util/log" @@ -17,10 +19,14 @@ import ( ) func newAdminApp() *cli.App { + buildInfo := buildinfo.ReadVersionInfo() + cli.VersionPrinter = func(*cli.Context) { + fmt.Println(buildInfo.String()) + } return &cli.App{ Name: "varlogadm", Usage: "run varlog admin server", - Version: "0.0.1", + Version: buildInfo.Version, Commands: []*cli.Command{ newStartCommand(), }, diff --git a/cmd/varlogcli/varlogcli.go b/cmd/varlogcli/varlogcli.go index 4ae72de8f..be37519ad 100644 --- a/cmd/varlogcli/varlogcli.go +++ b/cmd/varlogcli/varlogcli.go @@ -1,12 +1,14 @@ package main import ( + "fmt" "log" "os" "github.com/pkg/errors" "github.com/urfave/cli/v2" + "github.com/kakao/varlog/internal/buildinfo" "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/internal/varlogcli" "github.com/kakao/varlog/pkg/types" @@ -26,9 +28,14 @@ func run() int { } func newApp() *cli.App { + buildInfo := buildinfo.ReadVersionInfo() + cli.VersionPrinter = func(*cli.Context) { + fmt.Println(buildInfo.String()) + } app := &cli.App{ - Name: "varlogcli", - Usage: "Varlog client", + Name: "varlogcli", + Usage: "Varlog client", + Version: buildInfo.Version, Commands: []*cli.Command{ newAppend(), newSubscribe(), diff --git a/cmd/varlogctl/cli.go b/cmd/varlogctl/cli.go index 05ad3e659..bb4b3d597 100644 --- a/cmd/varlogctl/cli.go +++ b/cmd/varlogctl/cli.go @@ -1,14 +1,16 @@ package main import ( + "fmt" "time" "github.com/urfave/cli/v2" + + "github.com/kakao/varlog/internal/buildinfo" ) const ( appName = "varlogctl" - version = "0.0.1" defaultTimeout = time.Second * 5 ) @@ -23,10 +25,14 @@ func commonFlags(flags ...cli.Flag) []cli.Flag { } func newVarlogControllerApp() *cli.App { + buildInfo := buildinfo.ReadVersionInfo() + cli.VersionPrinter = func(*cli.Context) { + fmt.Println(buildInfo.String()) + } app := &cli.App{ Name: appName, Usage: "controller application for varlog", - Version: version, + Version: buildInfo.Version, Commands: []*cli.Command{ newStorageNodeCommand(), newTopicCommand(), diff --git a/cmd/varlogmr/metadata_repository.go b/cmd/varlogmr/metadata_repository.go index 10a062391..918525925 100644 --- a/cmd/varlogmr/metadata_repository.go +++ b/cmd/varlogmr/metadata_repository.go @@ -11,6 +11,7 @@ import ( _ "go.uber.org/automaxprocs" "go.uber.org/zap" + "github.com/kakao/varlog/internal/buildinfo" "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/internal/metarepos" "github.com/kakao/varlog/pkg/types" @@ -111,10 +112,14 @@ func start(c *cli.Context) error { } func initCLI() *cli.App { + buildInfo := buildinfo.ReadVersionInfo() + cli.VersionPrinter = func(*cli.Context) { + fmt.Println(buildInfo.String()) + } return &cli.App{ Name: "metadata_repository", Usage: "run metadata repository", - Version: "v0.0.1", + Version: buildInfo.Version, Commands: []*cli.Command{{ Name: "start", Aliases: []string{"s"}, diff --git a/cmd/varlogsn/cli.go b/cmd/varlogsn/cli.go index 9ce579441..92d4cf35d 100644 --- a/cmd/varlogsn/cli.go +++ b/cmd/varlogsn/cli.go @@ -1,8 +1,11 @@ package main import ( + "fmt" + "github.com/urfave/cli/v2" + "github.com/kakao/varlog/internal/buildinfo" "github.com/kakao/varlog/internal/flags" "github.com/kakao/varlog/internal/storagenode" "github.com/kakao/varlog/internal/storagenode/logstream" @@ -12,14 +15,17 @@ import ( const ( appName = "varlogsn" - version = "0.0.1" ) func newStorageNodeApp() *cli.App { + buildInfo := buildinfo.ReadVersionInfo() + cli.VersionPrinter = func(*cli.Context) { + fmt.Println(buildInfo.String()) + } return &cli.App{ Name: appName, Usage: "storage node", - Version: version, + Version: buildInfo.Version, Commands: []*cli.Command{ newStartCommand(), }, diff --git a/internal/buildinfo/buildinfo.go b/internal/buildinfo/buildinfo.go new file mode 100644 index 000000000..9653944d0 --- /dev/null +++ b/internal/buildinfo/buildinfo.go @@ -0,0 +1,46 @@ +package buildinfo + +import ( + "runtime/debug" + "strings" +) + +const ( + goOS = "GOOS" + goArch = "GOARCH" + vcsRevision = "vcs.revision" + vcsTime = "vcs.time" +) + +var version = "devel" + +type Info struct { + *debug.BuildInfo + settingsMap map[string]string + + Version string +} + +func ReadVersionInfo() Info { + info := Info{ + Version: version, + } + if buildInfo, ok := debug.ReadBuildInfo(); ok { + info.BuildInfo = buildInfo + info.settingsMap = make(map[string]string, len(buildInfo.Settings)) + for _, kv := range buildInfo.Settings { + info.settingsMap[kv.Key] = kv.Value + } + } + return info +} + +func (info Info) String() string { + var sb strings.Builder + sb.WriteString("Version: " + info.Version + "\n") + sb.WriteString("Go Version: " + info.GoVersion + "\n") + sb.WriteString("Git Commit: " + info.settingsMap[vcsRevision] + "\n") + sb.WriteString("Built: " + info.settingsMap[vcsTime] + "\n") + sb.WriteString("OS/Arch: " + info.settingsMap[goOS] + "/" + info.settingsMap[goArch]) + return sb.String() +}