Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement version command
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
JeyJeyGao committed Oct 26, 2022
1 parent f747031 commit 133861c
Showing 6 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .dev.goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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:
17 changes: 11 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
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.
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
6 changes: 3 additions & 3 deletions cmd/notation/main.go
Original file line number Diff line number Diff line change
@@ -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(
@@ -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)
33 changes: 33 additions & 0 deletions cmd/notation/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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,
RunE: func(cmd *cobra.Command, args []string) error {
runVersion()
return nil
},
}
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
@@ -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.

0 comments on commit 133861c

Please sign in to comment.