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

feat: implement version command #419

Merged
merged 2 commits into from
Oct 31, 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
2 changes: 1 addition & 1 deletion .dev.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ builds:
- goos: windows
goarch: arm64
ldflags:
- -s -w -X {{.ModulePath}}/internal/version.Version={{.Version}} -X {{.ModulePath}}/internal/version.BuildMetadata=
- -s -w -X {{.ModulePath}}/internal/version.Version={{.Version}} -X {{.ModulePath}}/internal/version.GitCommit={{.FullCommit}} -X {{.ModulePath}}/internal/version.BuildMetadata=
archives:
- format: tar.gz
format_overrides:
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ builds:
- goos: windows
goarch: arm64
ldflags:
- -s -w -X {{.ModulePath}}/internal/version.Version={{.Version}} -X {{.ModulePath}}/internal/version.BuildMetadata=
- -s -w -X {{.ModulePath}}/internal/version.Version={{.Version}} -X {{.ModulePath}}/internal/version.GitCommit={{.FullCommit}} -X {{.ModulePath}}/internal/version.BuildMetadata=
archives:
- format: tar.gz
format_overrides:
Expand Down
21 changes: 15 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
MODULE = github.com/notaryproject/notation
COMMANDS = notation
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
BUILD_METADATA =
ifeq ($(GIT_TAG),) # unreleased build
GIT_COMMIT = $(shell git rev-parse HEAD)
GIT_STATUS = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "unreleased")
BUILD_METADATA = $(GIT_COMMIT).$(GIT_STATUS)
GIT_COMMIT = $(shell git rev-parse HEAD)

# if the commit was tagged, BuildMetadata is empty.
ifndef BUILD_METADATA
BUILD_METADATA := unreleased
endif

ifneq ($(GIT_TAG),)
BUILD_METADATA :=
endif
LDFLAGS = -X $(MODULE)/internal/version.BuildMetadata=$(BUILD_METADATA)

# set flags
LDFLAGS := -w \
-X $(MODULE)/internal/version.GitCommit=$(GIT_COMMIT) \
-X $(MODULE)/internal/version.BuildMetadata=$(BUILD_METADATA)

GO_BUILD_FLAGS = --ldflags="$(LDFLAGS)"

.PHONY: help
Expand Down
6 changes: 3 additions & 3 deletions cmd/notation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package main
import (
"log"

"github.com/notaryproject/notation/internal/version"
"github.com/spf13/cobra"
)

func main() {
cmd := &cobra.Command{
Use: "notation",
Short: "Notation - Notary V2",
Version: version.GetVersion(),
SilenceUsage: true,
}
cmd.AddCommand(
Expand All @@ -22,7 +20,9 @@ func main() {
keyCommand(),
pluginCommand(),
loginCommand(nil),
logoutCommand(nil))
logoutCommand(nil),
versionCommand(),
)
cmd.PersistentFlags().Bool(flagPlainHTTP.Name, false, flagPlainHTTP.Usage)
if err := cmd.Execute(); err != nil {
log.Fatal(err)
Expand Down
32 changes: 32 additions & 0 deletions cmd/notation/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"runtime"

"github.com/notaryproject/notation/internal/version"
"github.com/spf13/cobra"
)

func versionCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Show the notation version information",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runVersion()
},
}
return cmd
}

func runVersion() {
fmt.Printf("Notation: Notary v2, A tool to sign, store, and verify artifacts.\n\n")

fmt.Printf("Version: %s\n", version.GetVersion())
fmt.Printf("Go version: %s\n", runtime.Version())

if version.GitCommit != "" {
fmt.Printf("Git commit: %s\n", version.GitCommit)
}
}
7 changes: 7 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ var (
Version = "v0.11.0-alpha.4"

// BuildMetadata stores the build metadata.
//
// When execute `make build` command, it will be overridden by
// environment variable `BUILD_METADATA`. If commit tag was set,
// BuildMetadata will be empty.
BuildMetadata = "unreleased"

// GitCommit stores the git HEAD commit id
GitCommit = ""
)

// GetVersion returns the version string in SemVer 2.
Expand Down