Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Added more information to the version command #688

Merged
merged 4 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ protoc:
.PHONY: build
build:
$(eval LATEST_VERSION = $(shell git describe --tags --abbrev=0))
$(eval COMMIT_HASH = $(shell git rev-parse --short HEAD))
go build -ldflags="-X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(LATEST_VERSION)+$(COMMIT_HASH)'" main.go
$(eval COMMIT_HASH = $(shell git rev-parse HEAD))
$(eval BRANCH = $(shell git rev-parse --abbrev-ref HEAD | tr -d '\040\011\012\015\n'))
$(eval TIME = $(shell date))
go build -o polygon-edge -ldflags="\
-X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(LATEST_VERSION)' \
-X 'github.com/0xPolygon/polygon-edge/versioning.Commit=$(COMMIT_HASH)'\
-X 'github.com/0xPolygon/polygon-edge/versioning.Branch=$(BRANCH)'\
-X 'github.com/0xPolygon/polygon-edge/versioning.BuildTime=$(TIME)'" \
main.go

.PHONY: lint
lint:
Expand Down
23 changes: 21 additions & 2 deletions command/version/result.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package version

import (
"bytes"
"fmt"
"github.com/0xPolygon/polygon-edge/command/helper"
)

type VersionResult struct {
Version string `json:"version"`
Version string `json:"version"`
Commit string `json:"commit"`
Branch string `json:"branch"`
BuildTime string `json:"buildTime"`
}

func (r *VersionResult) GetOutput() string {
return r.Version
var buffer bytes.Buffer

buffer.WriteString("\n[VERSION INFO]\n")
buffer.WriteString(helper.FormatKV([]string{
fmt.Sprintf("Release version|%s", r.Version),
fmt.Sprintf("Git branch|%s", r.Branch),
fmt.Sprintf("Commit hash|%s", r.Commit),
fmt.Sprintf("Build time|%s", r.BuildTime),
}))

return buffer.String()
}
5 changes: 4 additions & 1 deletion command/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func runCommand(cmd *cobra.Command, _ []string) {

outputter.SetCommandResult(
&VersionResult{
Version: versioning.Version,
Version: versioning.Version,
Commit: versioning.Commit,
Branch: versioning.Branch,
BuildTime: versioning.BuildTime,
},
)
}
7 changes: 6 additions & 1 deletion versioning/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package versioning

var (
// Version is the main version at the moment.
// Commit is the git commit that the binary was built on
// BuildTime is the timestamp of the build
// Embedded by --ldflags on build time
// Versioning should follow the SemVer guidelines
// https://semver.org/
Version = "v0.1.0"
Version string
Branch string
Commit string
BuildTime string
)