Skip to content

Commit f66ae09

Browse files
authored
Merge pull request #9 from coolljt0725/version
Add --version to cmd to show version information
2 parents 721ba58 + 3630b6f commit f66ae09

File tree

4 files changed

+67
-17
lines changed

4 files changed

+67
-17
lines changed

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
GO15VENDOREXPERIMENT=1
22
export GO15VENDOREXPERIMENT
33

4+
COMMIT=$(shell git rev-parse HEAD 2> /dev/null || true)
5+
46
EPOCH_TEST_COMMIT ?= v0.2.0
57

68
default: help
@@ -19,9 +21,9 @@ check-license:
1921
@./.tool/check-license
2022

2123
tools:
22-
go build ./cmd/oci-create-runtime-bundle
23-
go build ./cmd/oci-unpack
24-
go build ./cmd/oci-image-validate
24+
go build -ldflags "-X main.gitCommit=${COMMIT}" ./cmd/oci-create-runtime-bundle
25+
go build -ldflags "-X main.gitCommit=${COMMIT}" ./cmd/oci-unpack
26+
go build -ldflags "-X main.gitCommit=${COMMIT}" ./cmd/oci-image-validate
2527

2628
lint:
2729
@echo "checking lint"

cmd/oci-create-runtime-bundle/main.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,28 @@ import (
2020
"os"
2121
"strings"
2222

23+
specs "github.com/opencontainers/image-spec/specs-go"
2324
"github.com/opencontainers/image-tools/image"
2425
"github.com/spf13/cobra"
2526
)
2627

28+
// gitCommit will be the hash that the binary was built from
29+
// and will be populated by the Makefile
30+
var gitCommit = ""
31+
2732
// supported bundle types
2833
var bundleTypes = []string{
2934
image.TypeImageLayout,
3035
image.TypeImage,
3136
}
3237

3338
type bundleCmd struct {
34-
stdout *log.Logger
35-
stderr *log.Logger
36-
typ string // the type to bundle, can be empty string
37-
ref string
38-
root string
39+
stdout *log.Logger
40+
stderr *log.Logger
41+
typ string // the type to bundle, can be empty string
42+
ref string
43+
root string
44+
version bool
3945
}
4046

4147
func main() {
@@ -81,10 +87,20 @@ func newBundleCmd(stdout, stderr *log.Logger) *cobra.Command {
8187
It is strongly recommended to keep the default value.`,
8288
)
8389

90+
cmd.Flags().BoolVar(
91+
&v.version, "version", false,
92+
`Print version information and exit`,
93+
)
8494
return cmd
8595
}
8696

8797
func (v *bundleCmd) Run(cmd *cobra.Command, args []string) {
98+
if v.version {
99+
v.stdout.Printf("commit: %s", gitCommit)
100+
v.stdout.Printf("spec: %s", specs.Version)
101+
os.Exit(0)
102+
103+
}
88104
if len(args) != 2 {
89105
v.stderr.Print("both src and dest must be provided")
90106
if err := cmd.Usage(); err != nil {

cmd/oci-image-validate/main.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ import (
2121
"strings"
2222

2323
"github.com/opencontainers/image-spec/schema"
24+
specs "github.com/opencontainers/image-spec/specs-go"
2425
"github.com/opencontainers/image-tools/image"
2526
"github.com/pkg/errors"
2627
"github.com/spf13/cobra"
2728
)
2829

30+
// gitCommit will be the hash that the binary was built from
31+
// and will be populated by the Makefile
32+
var gitCommit = ""
33+
2934
// supported validation types
3035
var validateTypes = []string{
3136
image.TypeImageLayout,
@@ -36,10 +41,11 @@ var validateTypes = []string{
3641
}
3742

3843
type validateCmd struct {
39-
stdout *log.Logger
40-
stderr *log.Logger
41-
typ string // the type to validate, can be empty string
42-
refs []string
44+
stdout *log.Logger
45+
stderr *log.Logger
46+
typ string // the type to validate, can be empty string
47+
refs []string
48+
version bool
4349
}
4450

4551
func main() {
@@ -78,10 +84,21 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command {
7884
`A set of refs pointing to the manifests to be validated. Each reference must be present in the "refs" subdirectory of the image. Only applicable if type is image or imageLayout.`,
7985
)
8086

87+
cmd.Flags().BoolVar(
88+
&v.version, "version", false,
89+
`Print version information and exit`,
90+
)
91+
8192
return cmd
8293
}
8394

8495
func (v *validateCmd) Run(cmd *cobra.Command, args []string) {
96+
if v.version {
97+
v.stdout.Printf("commit: %s", gitCommit)
98+
v.stdout.Printf("spec: %s", specs.Version)
99+
os.Exit(0)
100+
}
101+
85102
if len(args) < 1 {
86103
v.stderr.Printf("no files specified")
87104
if err := cmd.Usage(); err != nil {

cmd/oci-unpack/main.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,27 @@ import (
2020
"os"
2121
"strings"
2222

23+
specs "github.com/opencontainers/image-spec/specs-go"
2324
"github.com/opencontainers/image-tools/image"
2425
"github.com/spf13/cobra"
2526
)
2627

28+
// gitCommit will be the hash that the binary was built from
29+
// and will be populated by the Makefile
30+
var gitCommit = ""
31+
2732
// supported unpack types
2833
var unpackTypes = []string{
2934
image.TypeImageLayout,
3035
image.TypeImage,
3136
}
3237

3338
type unpackCmd struct {
34-
stdout *log.Logger
35-
stderr *log.Logger
36-
typ string // the type to unpack, can be empty string
37-
ref string
39+
stdout *log.Logger
40+
stderr *log.Logger
41+
typ string // the type to unpack, can be empty string
42+
ref string
43+
version bool
3844
}
3945

4046
func main() {
@@ -73,11 +79,20 @@ func newUnpackCmd(stdout, stderr *log.Logger) *cobra.Command {
7379
&v.ref, "ref", "v1.0",
7480
`The ref pointing to the manifest to be unpacked. This must be present in the "refs" subdirectory of the image.`,
7581
)
76-
82+
cmd.Flags().BoolVar(
83+
&v.version, "version", false,
84+
`Print version information and exit`,
85+
)
7786
return cmd
7887
}
7988

8089
func (v *unpackCmd) Run(cmd *cobra.Command, args []string) {
90+
if v.version {
91+
v.stdout.Printf("commit: %s", gitCommit)
92+
v.stdout.Printf("spec: %s", specs.Version)
93+
os.Exit(0)
94+
}
95+
8196
if len(args) != 2 {
8297
v.stderr.Print("both src and dest must be provided")
8398
if err := cmd.Usage(); err != nil {

0 commit comments

Comments
 (0)