Skip to content

Commit 093a2b5

Browse files
authored
Add more info to version switch (#5)
1 parent fbabd2a commit 093a2b5

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

cmd/root.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
7+
"text/template"
68

79
"github.com/docker/cli/cli/command"
810
"github.com/spf13/cobra"
@@ -47,12 +49,26 @@ func cmd(_ command.Cli) *cobra.Command {
4749
Args: validateInputArgs,
4850
SilenceUsage: true,
4951
SilenceErrors: true,
52+
Version: version.FromBuild().Version,
5053
RunE: run,
5154
ValidArgsFunction: dockerImageValidArgsFunction,
5255
}
5356

54-
// setting the version template to just print out the string since we already have a templatized version string
55-
c.SetVersionTemplate(fmt.Sprintf("%s {{.Version}}\n", internal.ApplicationName))
57+
c.SetVersionTemplate(tprintf(`Application: {{ .Name }} ({{ .Version.Version }})
58+
Provider: {{ .SyftName }} ({{ .SyftVersion }})
59+
BuildDate: {{ .BuildDate }}
60+
GitCommit: {{ .GitCommit }}
61+
GitDescription: {{ .GitDescription }}
62+
Platform: {{ .Platform }}
63+
`, struct {
64+
Name string
65+
SyftName string
66+
version.Version
67+
}{
68+
Name: internal.BinaryName,
69+
SyftName: internal.SyftName,
70+
Version: version.FromBuild(),
71+
}))
5672

5773
setPackageFlags(c.Flags())
5874

@@ -63,6 +79,15 @@ func cmd(_ command.Cli) *cobra.Command {
6379
return c
6480
}
6581

82+
func tprintf(tmpl string, data interface{}) string {
83+
t := template.Must(template.New("").Parse(tmpl))
84+
buf := &bytes.Buffer{}
85+
if err := t.Execute(buf, data); err != nil {
86+
return ""
87+
}
88+
return buf.String()
89+
}
90+
6691
func setPackageFlags(flags *pflag.FlagSet) {
6792
// Universal options ///////////////////////////////////////////////////////
6893

@@ -132,6 +157,7 @@ func bindConfigOptions(flags *pflag.FlagSet) error {
132157

133158
return nil
134159
}
160+
135161
func validateInputArgs(cmd *cobra.Command, args []string) error {
136162
if len(args) == 0 {
137163
// in the case that no arguments are given we want to show the help text and return with a non-0 return code.

test/cli/sbom_cmd_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ func TestSBOMCmdFlags(t *testing.T) {
2828
assertFailingReturnCode,
2929
},
3030
},
31+
{
32+
name: "use-version-option",
33+
args: []string{"sbom", "--version"},
34+
assertions: []traitAssertion{
35+
assertInOutput("Application:"),
36+
assertInOutput("docker-sbom ("),
37+
assertInOutput("Provider:"),
38+
assertInOutput("GitDescription:"),
39+
assertInOutput("syft (v0.41.1)"),
40+
assertNotInOutput("not provided"),
41+
assertSuccessfulReturnCode,
42+
},
43+
},
3144
{
3245
name: "json-output-flag",
3346
args: []string{"sbom", "-o", "json", coverageImage},

0 commit comments

Comments
 (0)