Skip to content

Commit

Permalink
refactor(version): wrap duplicated code in a function (#147)
Browse files Browse the repository at this point in the history
## Summary

PR is a follow-up of
#108 (comment).

## 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 <[email protected]>
  • Loading branch information
davidhsingyuchen authored Jan 12, 2023
1 parent 2484f6e commit 65aa4d7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions cmd/finch/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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)
}

Expand All @@ -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))
}

0 comments on commit 65aa4d7

Please sign in to comment.