Skip to content

Commit a9d04e3

Browse files
authored
feat: Add the --format option to the finch version command (runfinch#926)
Currently, the `finch version` command does not support the `--format` option. However, a feature request has been submitted in the following issue to add the `--format` option to the `finch version` command. - runfinch#199 Therefore, this fix introduces the `--format` option to the `finch version` command. Issue #, if available: runfinch#199 *Description of changes:* Details are described in this commit message. *Testing done:* Yes - [x] I've reviewed the guidance in CONTRIBUTING.md #### License Acceptance By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. Signed-off-by: Hayato Kiwata <[email protected]>
1 parent 17bbcb1 commit a9d04e3

File tree

6 files changed

+413
-79
lines changed

6 files changed

+413
-79
lines changed

Diff for: cmd/finch/version.go

+107-27
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,48 @@
44
package main
55

66
import (
7+
"bytes"
78
"encoding/json"
89
"errors"
910
"fmt"
1011
"io"
12+
"text/template"
1113

1214
"github.com/spf13/cobra"
1315

1416
"github.com/runfinch/finch/pkg/command"
1517
"github.com/runfinch/finch/pkg/flog"
1618
"github.com/runfinch/finch/pkg/lima"
19+
"github.com/runfinch/finch/pkg/templates"
1720
"github.com/runfinch/finch/pkg/version"
1821
)
1922

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+
2049
func newVersionCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logger, stdOut io.Writer) *cobra.Command {
2150
versionCommand := &cobra.Command{
2251
Use: "version",
@@ -25,6 +54,8 @@ func newVersionCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logger
2554
RunE: newVersionAction(limaCmdCreator, logger, stdOut).runAdapter,
2655
}
2756

57+
versionCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'")
58+
2859
return versionCommand
2960
}
3061

@@ -40,6 +71,19 @@ type NerdctlVersionOutput struct {
4071
Server NerdctlServerOutput `json:"Server"`
4172
}
4273

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+
4387
// NerdctlClientOutput captures the nerdctl Client output.
4488
type NerdctlClientOutput struct {
4589
Version string `json:"Version"`
@@ -50,11 +94,6 @@ type NerdctlClientOutput struct {
5094
Components []NerdctlComponentsOutput `json:"Components"`
5195
}
5296

53-
// NerdctlServerOutput captures the nerdctl Server output.
54-
type NerdctlServerOutput struct {
55-
Components []NerdctlComponentsOutput `json:"Components"`
56-
}
57-
5897
// NerdctlComponentsOutput captures the nerdctl components output.
5998
type NerdctlComponentsOutput struct {
6099
Name string `json:"Name"`
@@ -64,23 +103,75 @@ type NerdctlComponentsOutput struct {
64103
}
65104
}
66105

106+
// NerdctlServerOutput captures the nerdctl Server output.
107+
type NerdctlServerOutput struct {
108+
Components []NerdctlComponentsOutput `json:"Components"`
109+
}
110+
67111
func newVersionAction(creator command.LimaCmdCreator, logger flog.Logger, stdOut io.Writer) *versionAction {
68112
return &versionAction{creator: creator, logger: logger, stdOut: stdOut}
69113
}
70114

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)
73122
}
74123

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 {
77126
fmt.Fprintf(va.stdOut, "Finch version:\t%s\n", version.Version)
78127
return err
79128
}
80129
return nil
81130
}
82131

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 {
84175
status, err := lima.GetVMStatus(va.creator, va.logger, limaInstanceName)
85176
if err != nil {
86177
return fmt.Errorf("failed to get VM status: %w", err)
@@ -102,24 +193,13 @@ func (va *versionAction) printVersion() error {
102193
return fmt.Errorf("failed to JSON-unmarshal the nerdctl version output: %w", err)
103194
}
104195

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
116199
}
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
123203
}
124204

125205
return nil

0 commit comments

Comments
 (0)