|
16 | 16 | package app |
17 | 17 |
|
18 | 18 | import ( |
19 | | - "encoding/json" |
20 | | - "fmt" |
21 | | - "runtime" |
22 | | - "strings" |
23 | | - "text/tabwriter" |
24 | | - |
25 | | - "github.com/pkg/errors" |
26 | | - "github.com/sigstore/rekor/pkg/api" |
27 | | - "github.com/spf13/cobra" |
| 19 | + "sigs.k8s.io/release-utils/version" |
28 | 20 | ) |
29 | 21 |
|
30 | | -type versionOptions struct { |
31 | | - json bool |
32 | | -} |
33 | | - |
34 | | -var versionOpts = &versionOptions{} |
35 | | - |
36 | | -// verifyCmd represents the verify command |
37 | | -var versionCmd = &cobra.Command{ |
38 | | - Use: "version", |
39 | | - Short: "rekor-server version", |
40 | | - RunE: func(cmd *cobra.Command, args []string) error { |
41 | | - return runVersion(versionOpts) |
42 | | - }, |
43 | | -} |
44 | | - |
45 | 22 | func init() { |
46 | | - versionCmd.PersistentFlags().BoolVarP(&versionOpts.json, "json", "j", false, |
47 | | - "print JSON instead of text") |
48 | | - rootCmd.AddCommand(versionCmd) |
49 | | -} |
50 | | - |
51 | | -func runVersion(opts *versionOptions) error { |
52 | | - v := VersionInfo() |
53 | | - res := v.String() |
54 | | - |
55 | | - if opts.json { |
56 | | - j, err := v.JSONString() |
57 | | - if err != nil { |
58 | | - return errors.Wrap(err, "unable to generate JSON from version info") |
59 | | - } |
60 | | - res = j |
61 | | - } |
62 | | - |
63 | | - fmt.Println(res) |
64 | | - return nil |
65 | | -} |
66 | | - |
67 | | -type Info struct { |
68 | | - GitVersion string |
69 | | - GitCommit string |
70 | | - GitTreeState string |
71 | | - BuildDate string |
72 | | - GoVersion string |
73 | | - Compiler string |
74 | | - Platform string |
75 | | -} |
76 | | - |
77 | | -func VersionInfo() Info { |
78 | | - // These variables typically come from -ldflags settings and in |
79 | | - // their absence fallback to the global defaults set above. |
80 | | - return Info{ |
81 | | - GitVersion: api.GitVersion, |
82 | | - GitCommit: api.GitCommit, |
83 | | - GitTreeState: api.GitTreeState, |
84 | | - BuildDate: api.BuildDate, |
85 | | - GoVersion: runtime.Version(), |
86 | | - Compiler: runtime.Compiler, |
87 | | - Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), |
88 | | - } |
89 | | -} |
90 | | - |
91 | | -// String returns the string representation of the version info |
92 | | -func (i *Info) String() string { |
93 | | - b := strings.Builder{} |
94 | | - w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0) |
95 | | - |
96 | | - fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion) |
97 | | - fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit) |
98 | | - fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState) |
99 | | - fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate) |
100 | | - fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion) |
101 | | - fmt.Fprintf(w, "Compiler:\t%s\n", i.Compiler) |
102 | | - fmt.Fprintf(w, "Platform:\t%s\n", i.Platform) |
103 | | - |
104 | | - w.Flush() // #nosec |
105 | | - return b.String() |
106 | | -} |
107 | | - |
108 | | -// JSONString returns the JSON representation of the version info |
109 | | -func (i *Info) JSONString() (string, error) { |
110 | | - b, err := json.MarshalIndent(i, "", " ") |
111 | | - if err != nil { |
112 | | - return "", err |
113 | | - } |
114 | | - |
115 | | - return string(b), nil |
| 23 | + rootCmd.AddCommand(version.Version()) |
116 | 24 | } |
0 commit comments