From 65aa4d797dd6c7aba890a97d433ae3651d3b61e1 Mon Sep 17 00:00:00 2001 From: "Hsing-Yu (David) Chen" Date: Wed, 11 Jan 2023 20:42:17 -0500 Subject: [PATCH] refactor(version): wrap duplicated code in a function (#147) ## Summary PR is a follow-up of https://github.com/runfinch/finch/pull/108#discussion_r1066534579. ## 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: Hsing-Yu (David) Chen --- cmd/finch/version.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/finch/version.go b/cmd/finch/version.go index 6c885c089..90f052ed0 100644 --- a/cmd/finch/version.go +++ b/cmd/finch/version.go @@ -73,13 +73,19 @@ func (va *versionAction) runAdapter(cmd *cobra.Command, args []string) error { } func (va *versionAction) run() error { + if err := va.printVersion(); err != nil { + fmt.Fprintf(va.stdOut, "Finch version:\t%s\n", version.Version) + return err + } + return nil +} + +func (va *versionAction) printVersion() error { status, err := lima.GetVMStatus(va.creator, va.logger, limaInstanceName) if err != nil { - va.printVersion() return fmt.Errorf("failed to get VM status: %w", err) } if status != lima.Running { - va.printVersion() return errors.New("detailed version info is unavailable because VM is not running") } @@ -91,9 +97,7 @@ func (va *versionAction) run() error { var nerdctlVersion NerdctlVersionOutput err = json.Unmarshal(out, &nerdctlVersion) - if err != nil { - va.printVersion() return fmt.Errorf("failed to JSON-unmarshal the nerdctl version output: %w", err) } @@ -119,7 +123,3 @@ func (va *versionAction) run() error { return nil } - -func (va *versionAction) printVersion() { - (fmt.Fprintf(va.stdOut, "Finch version:\t%s\n", version.Version)) -}