11package cmd
22
33import (
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+
6691func setPackageFlags (flags * pflag.FlagSet ) {
6792 // Universal options ///////////////////////////////////////////////////////
6893
@@ -132,6 +157,7 @@ func bindConfigOptions(flags *pflag.FlagSet) error {
132157
133158 return nil
134159}
160+
135161func 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.
0 commit comments