4
4
package main
5
5
6
6
import (
7
+ "bytes"
7
8
"encoding/json"
8
9
"errors"
9
10
"fmt"
10
11
"io"
12
+ "text/template"
11
13
12
14
"github.com/spf13/cobra"
13
15
14
16
"github.com/runfinch/finch/pkg/command"
15
17
"github.com/runfinch/finch/pkg/flog"
16
18
"github.com/runfinch/finch/pkg/lima"
19
+ "github.com/runfinch/finch/pkg/templates"
17
20
"github.com/runfinch/finch/pkg/version"
18
21
)
19
22
23
+ const defaultVersionTemplate = `{{with .Client -}}
24
+ Client:
25
+ Version: {{.Version}}
26
+ GitCommit: {{.GitCommit}}
27
+ {{with .NerdctlClient -}}
28
+ OS/Arch: {{.Os}}/{{.Arch}}
29
+ nerdctl:
30
+ Version: {{.Version}}
31
+ GitCommit: {{.GitCommit}}
32
+ {{- range $component := .Components}}
33
+ {{$component.Name}}:
34
+ Version: {{.Version}}
35
+ GitCommit: {{.Details.GitCommit}}
36
+ {{- end}}
37
+ {{- end}}
38
+ {{- end}}
39
+
40
+ {{with .Server -}}
41
+ Server:
42
+ {{- range $component := .Components}}
43
+ {{$component.Name}}:
44
+ Version: {{.Version}}
45
+ GitCommit: {{.Details.GitCommit}}
46
+ {{- end}}
47
+ {{- end}}`
48
+
20
49
func newVersionCommand (limaCmdCreator command.LimaCmdCreator , logger flog.Logger , stdOut io.Writer ) * cobra.Command {
21
50
versionCommand := & cobra.Command {
22
51
Use : "version" ,
@@ -25,6 +54,8 @@ func newVersionCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logger
25
54
RunE : newVersionAction (limaCmdCreator , logger , stdOut ).runAdapter ,
26
55
}
27
56
57
+ versionCommand .Flags ().StringP ("format" , "f" , "" , "Format the output using the given Go template, e.g, '{{json .}}'" )
58
+
28
59
return versionCommand
29
60
}
30
61
@@ -40,6 +71,19 @@ type NerdctlVersionOutput struct {
40
71
Server NerdctlServerOutput `json:"Server"`
41
72
}
42
73
74
+ // FinchVersionOutput captures the finch version.
75
+ type FinchVersionOutput struct {
76
+ Client ClientVersionOutput `json:"Client"`
77
+ Server NerdctlServerOutput `json:"Server"`
78
+ }
79
+
80
+ // ClientVersionOutput captures the commit ID for finch and the nerdctl Client output.
81
+ type ClientVersionOutput struct {
82
+ Version string `json:"Version"`
83
+ GitCommit string `json:"GitCommit"`
84
+ NerdctlClient NerdctlClientOutput `json:"NerdctlClient"`
85
+ }
86
+
43
87
// NerdctlClientOutput captures the nerdctl Client output.
44
88
type NerdctlClientOutput struct {
45
89
Version string `json:"Version"`
@@ -50,11 +94,6 @@ type NerdctlClientOutput struct {
50
94
Components []NerdctlComponentsOutput `json:"Components"`
51
95
}
52
96
53
- // NerdctlServerOutput captures the nerdctl Server output.
54
- type NerdctlServerOutput struct {
55
- Components []NerdctlComponentsOutput `json:"Components"`
56
- }
57
-
58
97
// NerdctlComponentsOutput captures the nerdctl components output.
59
98
type NerdctlComponentsOutput struct {
60
99
Name string `json:"Name"`
@@ -64,23 +103,75 @@ type NerdctlComponentsOutput struct {
64
103
}
65
104
}
66
105
106
+ // NerdctlServerOutput captures the nerdctl Server output.
107
+ type NerdctlServerOutput struct {
108
+ Components []NerdctlComponentsOutput `json:"Components"`
109
+ }
110
+
67
111
func newVersionAction (creator command.LimaCmdCreator , logger flog.Logger , stdOut io.Writer ) * versionAction {
68
112
return & versionAction {creator : creator , logger : logger , stdOut : stdOut }
69
113
}
70
114
71
- func (va * versionAction ) runAdapter (_ * cobra.Command , _ []string ) error {
72
- return va .run ()
115
+ func (va * versionAction ) runAdapter (cmd * cobra.Command , _ []string ) error {
116
+ format , err := cmd .Flags ().GetString ("format" )
117
+ if err != nil {
118
+ return err
119
+ }
120
+
121
+ return va .run (format )
73
122
}
74
123
75
- func (va * versionAction ) run () error {
76
- if err := va .printVersion (); err != nil {
124
+ func (va * versionAction ) run (format string ) error {
125
+ if err := va .printVersion (format ); err != nil {
77
126
fmt .Fprintf (va .stdOut , "Finch version:\t %s\n " , version .Version )
78
127
return err
79
128
}
80
129
return nil
81
130
}
82
131
83
- func (va * versionAction ) printVersion () error {
132
+ func newVersionTemplate (templateFormat string ) (* template.Template , error ) {
133
+ switch templateFormat {
134
+ case "" :
135
+ templateFormat = defaultVersionTemplate
136
+ case templates .JSONFormatKey :
137
+ templateFormat = templates .JSONFormat
138
+ }
139
+
140
+ tmpl := templates .New ("version" )
141
+ tmpl , err := tmpl .Parse (templateFormat )
142
+ if err != nil {
143
+ return nil , err
144
+ }
145
+
146
+ return tmpl , nil
147
+ }
148
+
149
+ func createFinchVersionOutput (nerdctlVersion NerdctlVersionOutput ) FinchVersionOutput {
150
+ var finchVersionOutput FinchVersionOutput
151
+
152
+ finchVersionOutput .Client .Version = version .Version
153
+ finchVersionOutput .Client .GitCommit = version .GitCommit
154
+ finchVersionOutput .Client .NerdctlClient = nerdctlVersion .Client
155
+ finchVersionOutput .Server = nerdctlVersion .Server
156
+
157
+ return finchVersionOutput
158
+ }
159
+
160
+ func (va * versionAction ) showVersionMessage (tmpl * template.Template , nerdctlVersion NerdctlVersionOutput ) error {
161
+ var b bytes.Buffer
162
+
163
+ finchVersionOutput := createFinchVersionOutput (nerdctlVersion )
164
+ if err := tmpl .Execute (& b , finchVersionOutput ); err != nil {
165
+ return err
166
+ }
167
+ if _ , err := fmt .Fprintln (va .stdOut , b .String ()); err != nil {
168
+ return err
169
+ }
170
+
171
+ return nil
172
+ }
173
+
174
+ func (va * versionAction ) printVersion (format string ) error {
84
175
status , err := lima .GetVMStatus (va .creator , va .logger , limaInstanceName )
85
176
if err != nil {
86
177
return fmt .Errorf ("failed to get VM status: %w" , err )
@@ -102,24 +193,13 @@ func (va *versionAction) printVersion() error {
102
193
return fmt .Errorf ("failed to JSON-unmarshal the nerdctl version output: %w" , err )
103
194
}
104
195
105
- fmt .Fprintf (va .stdOut , "Client:\n " )
106
- fmt .Fprintf (va .stdOut , " Version:\t %s\n " , version .Version )
107
- fmt .Fprintf (va .stdOut , " OS/Arch:\t %s/%s\n " , nerdctlVersion .Client .Os , nerdctlVersion .Client .Arch )
108
- fmt .Fprintf (va .stdOut , " GitCommit:\t %s\n " , version .GitCommit )
109
- fmt .Fprintf (va .stdOut , " nerdctl:\n " )
110
- fmt .Fprintf (va .stdOut , " Version:\t %s\n " , nerdctlVersion .Client .Version )
111
- fmt .Fprintf (va .stdOut , " GitCommit:\t %s\n " , nerdctlVersion .Client .GitCommit )
112
- for _ , compo := range nerdctlVersion .Client .Components {
113
- fmt .Fprintf (va .stdOut , " %s:\n " , compo .Name )
114
- fmt .Fprintf (va .stdOut , " Version:\t %s\n " , compo .Version )
115
- fmt .Fprintf (va .stdOut , " GitCommit:\t %s\n " , compo .Details .GitCommit )
196
+ tmpl , err := newVersionTemplate (format )
197
+ if err != nil {
198
+ return err
116
199
}
117
- fmt .Fprintf (va .stdOut , "\n " )
118
- fmt .Fprintf (va .stdOut , "Server:\n " )
119
- for _ , compo := range nerdctlVersion .Server .Components {
120
- fmt .Fprintf (va .stdOut , " %s:\n " , compo .Name )
121
- fmt .Fprintf (va .stdOut , " Version:\t %s\n " , compo .Version )
122
- fmt .Fprintf (va .stdOut , " GitCommit:\t %s\n " , compo .Details .GitCommit )
200
+ err = va .showVersionMessage (tmpl , nerdctlVersion )
201
+ if err != nil {
202
+ return err
123
203
}
124
204
125
205
return nil
0 commit comments