Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 065626d

Browse files
authored
Merge pull request #16 from eunomie/version
Version command
2 parents 1977311 + c3df0d4 commit 065626d

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

.goreleaser.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ builds:
2222
-s
2323
-extldflags '-static'
2424
-X github.com/docker/index-cli-plugin/internal.version={{.Version}}
25-
-X github.com/docker/index-cli-plugin/internal.commit={{.Commit}}
2625

2726
archives:
2827
- format: tar.gz

commands/cmd.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"os"
2525
"strings"
2626

27+
"github.com/docker/index-cli-plugin/internal"
28+
2729
"github.com/moby/term"
2830
"github.com/pkg/errors"
2931
"github.com/sirupsen/logrus"
@@ -243,7 +245,18 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
243245
},
244246
}
245247

246-
cmd.AddCommand(loginCommand, logoutCommand, sbomCommand, cveCommand, uploadCommand, diffCommand, watchCommand)
248+
versionCommand := &cobra.Command{
249+
Use: "version",
250+
Short: "Print version",
251+
Args: cobra.NoArgs,
252+
Run: func(_ *cobra.Command, _ []string) {
253+
v := internal.FromBuild()
254+
fmt.Printf("version: %s (%s - %s)\n", v.Version, v.GoVersion, v.Platform)
255+
fmt.Println("git commit:", v.Commit)
256+
},
257+
}
258+
259+
cmd.AddCommand(loginCommand, logoutCommand, sbomCommand, cveCommand, uploadCommand, diffCommand, watchCommand, versionCommand)
247260
return cmd
248261
}
249262

internal/version.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package internal
2222
import (
2323
"fmt"
2424
"runtime"
25+
"runtime/debug"
2526
)
2627

2728
// build-time arguments
@@ -30,6 +31,16 @@ var (
3031
commit = "n/a"
3132
)
3233

34+
func init() {
35+
if info, ok := debug.ReadBuildInfo(); ok {
36+
for _, setting := range info.Settings {
37+
if setting.Key == "vcs.revision" {
38+
commit = setting.Value
39+
}
40+
}
41+
}
42+
}
43+
3344
// Version information from build time args and environment
3445
type Version struct {
3546
Version string

0 commit comments

Comments
 (0)