|
4 | 4 | package main
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "encoding/json" |
| 8 | + "errors" |
7 | 9 | "fmt"
|
| 10 | + "io" |
8 | 11 |
|
9 | 12 | "github.com/spf13/cobra"
|
10 | 13 |
|
| 14 | + "github.com/runfinch/finch/pkg/command" |
| 15 | + "github.com/runfinch/finch/pkg/flog" |
| 16 | + "github.com/runfinch/finch/pkg/lima" |
11 | 17 | "github.com/runfinch/finch/pkg/version"
|
12 | 18 | )
|
13 | 19 |
|
14 |
| -var finchVersion = version.Version |
15 |
| - |
16 |
| -func newVersionCommand() *cobra.Command { |
| 20 | +func newVersionCommand(limaCmdCreator command.LimaCmdCreator, logger flog.Logger, stdOut io.Writer) *cobra.Command { |
17 | 21 | versionCommand := &cobra.Command{
|
18 | 22 | Use: "version",
|
19 | 23 | Args: cobra.NoArgs,
|
20 | 24 | Short: "Shows Finch version information",
|
21 |
| - RunE: versionAction, |
| 25 | + RunE: newVersionAction(limaCmdCreator, logger, stdOut).runAdapter, |
22 | 26 | }
|
23 | 27 |
|
24 | 28 | return versionCommand
|
25 | 29 | }
|
26 | 30 |
|
27 |
| -func versionAction(cmd *cobra.Command, args []string) error { |
28 |
| - fmt.Println("Finch version:", finchVersion) |
| 31 | +type versionAction struct { |
| 32 | + creator command.LimaCmdCreator |
| 33 | + logger flog.Logger |
| 34 | + stdOut io.Writer |
| 35 | +} |
| 36 | + |
| 37 | +// NerdctlVersionOutput captures the nerdctl version. |
| 38 | +type NerdctlVersionOutput struct { |
| 39 | + Client NerdctlClientOuput `json:"Client"` |
| 40 | + Server NerdctlServerOutput `json:"Server"` |
| 41 | +} |
| 42 | + |
| 43 | +// NerdctlClientOuput captures the nerdctl Client output. |
| 44 | +type NerdctlClientOuput struct { |
| 45 | + Version string `json:"Version"` |
| 46 | + GitCommit string `json:"GitCommit"` |
| 47 | + GoVersion string `json:"GoVersion"` |
| 48 | + Os string `json:"Os"` |
| 49 | + Arch string `json:"Arch"` |
| 50 | + Components []NerdctlComponentsOutput `json:"Components"` |
| 51 | +} |
| 52 | + |
| 53 | +// NerdctlServerOutput captures the nerdctl Server output. |
| 54 | +type NerdctlServerOutput struct { |
| 55 | + Components []NerdctlComponentsOutput `json:"Components"` |
| 56 | +} |
| 57 | + |
| 58 | +// NerdctlComponentsOutput captures the nerdctl components output. |
| 59 | +type NerdctlComponentsOutput struct { |
| 60 | + Name string `json:"Name"` |
| 61 | + Version string `json:"Version"` |
| 62 | + Details struct { |
| 63 | + GitCommit string `json:"GitCommit"` |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func newVersionAction(creator command.LimaCmdCreator, logger flog.Logger, stdOut io.Writer) *versionAction { |
| 68 | + return &versionAction{creator: creator, logger: logger, stdOut: stdOut} |
| 69 | +} |
| 70 | + |
| 71 | +func (va *versionAction) runAdapter(cmd *cobra.Command, args []string) error { |
| 72 | + return va.run() |
| 73 | +} |
| 74 | + |
| 75 | +func (va *versionAction) run() error { |
| 76 | + status, err := lima.GetVMStatus(va.creator, va.logger, limaInstanceName) |
| 77 | + if err != nil { |
| 78 | + va.printVersion() |
| 79 | + return fmt.Errorf("failed to get VM status: %w", err) |
| 80 | + } |
| 81 | + if status != lima.Running { |
| 82 | + va.printVersion() |
| 83 | + return errors.New("detailed version info is unavailable because VM is not running") |
| 84 | + } |
| 85 | + |
| 86 | + limaArgs := []string{"shell", limaInstanceName, "nerdctl", "version", "--format", "json"} |
| 87 | + out, err := va.creator.CreateWithoutStdio(limaArgs...).Output() |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("failed to create the nerdctl version command: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + var nerdctlVersion NerdctlVersionOutput |
| 93 | + err = json.Unmarshal(out, &nerdctlVersion) |
| 94 | + |
| 95 | + if err != nil { |
| 96 | + va.printVersion() |
| 97 | + return fmt.Errorf("failed to JSON-unmarshal the nerdctl version output: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + fmt.Fprintf(va.stdOut, "Client:\n") |
| 101 | + fmt.Fprintf(va.stdOut, " Version:\t%s\n", version.Version) |
| 102 | + fmt.Fprintf(va.stdOut, " OS/Arch:\t%s/%s\n", nerdctlVersion.Client.Os, nerdctlVersion.Client.Arch) |
| 103 | + fmt.Fprintf(va.stdOut, " GitCommit:\t%s\n", version.GitCommit) |
| 104 | + fmt.Fprintf(va.stdOut, " nerdctl:\n") |
| 105 | + fmt.Fprintf(va.stdOut, " Version:\t%s\n", nerdctlVersion.Client.Version) |
| 106 | + fmt.Fprintf(va.stdOut, " GitCommit:\t%s\n", nerdctlVersion.Client.GitCommit) |
| 107 | + for _, compo := range nerdctlVersion.Client.Components { |
| 108 | + fmt.Fprintf(va.stdOut, " %s:\n", compo.Name) |
| 109 | + fmt.Fprintf(va.stdOut, " Version:\t%s\n", compo.Version) |
| 110 | + fmt.Fprintf(va.stdOut, " GitCommit:\t%s\n", compo.Details.GitCommit) |
| 111 | + } |
| 112 | + fmt.Fprintf(va.stdOut, "\n") |
| 113 | + fmt.Fprintf(va.stdOut, "Server:\n") |
| 114 | + for _, compo := range nerdctlVersion.Server.Components { |
| 115 | + fmt.Fprintf(va.stdOut, " %s:\n", compo.Name) |
| 116 | + fmt.Fprintf(va.stdOut, " Version:\t%s\n", compo.Version) |
| 117 | + fmt.Fprintf(va.stdOut, " GitCommit:\t%s\n", compo.Details.GitCommit) |
| 118 | + } |
| 119 | + |
29 | 120 | return nil
|
30 | 121 | }
|
| 122 | + |
| 123 | +func (va *versionAction) printVersion() { |
| 124 | + (fmt.Fprintf(va.stdOut, "Finch version:\t%s\n", version.Version)) |
| 125 | +} |
0 commit comments