Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle errors in version command #2589

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions src/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"runtime"
"runtime/debug"

"github.com/Masterminds/semver/v3"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
goyaml "github.com/goccy/go-yaml"
"github.com/spf13/cobra"

"runtime/debug"

goyaml "github.com/goccy/go-yaml"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
)

var outputFormat string
Expand All @@ -29,13 +29,18 @@ var versionCmd = &cobra.Command{
},
Short: lang.CmdVersionShort,
Long: lang.CmdVersionLong,
Run: func(_ *cobra.Command, _ []string) {
RunE: func(_ *cobra.Command, _ []string) error {
if outputFormat == "" {
fmt.Println(config.CLIVersion)
return nil
}

output := make(map[string]interface{})
output["version"] = config.CLIVersion

buildInfo, ok := debug.ReadBuildInfo()
if !ok && outputFormat != "" {
fmt.Println("Failed to get build info")
return
if !ok {
return errors.New("failed to get build info")
}
depMap := map[string]string{}
for _, dep := range buildInfo.Deps {
Expand All @@ -50,28 +55,33 @@ var versionCmd = &cobra.Command{
buildMap := make(map[string]interface{})
buildMap["platform"] = runtime.GOOS + "/" + runtime.GOARCH
buildMap["goVersion"] = runtime.Version()
ver, _ := semver.NewVersion(config.CLIVersion)
ver, err := semver.NewVersion(config.CLIVersion)
if err != nil && !errors.Is(err, semver.ErrInvalidSemVer) {
return fmt.Errorf("Could not parse CLI version %s: %w", config.CLIVersion, err)
}
if ver != nil {
buildMap["major"] = ver.Major()
buildMap["minor"] = ver.Minor()
buildMap["patch"] = ver.Patch()
buildMap["prerelease"] = ver.Prerelease()
}

output["version"] = config.CLIVersion

output["build"] = buildMap

switch outputFormat {
case "yaml":
text, _ := goyaml.Marshal(output)
fmt.Println(string(text))
b, err := goyaml.Marshal(output)
if err != nil {
return fmt.Errorf("could not marshal yaml output: %w", err)
}
fmt.Println(string(b))
case "json":
text, _ := json.Marshal(output)
fmt.Println(string(text))
default:
fmt.Println(config.CLIVersion)
b, err := json.Marshal(output)
if err != nil {
return fmt.Errorf("could not marshal json output: %w", err)
}
fmt.Println(string(b))
}
return nil
},
}

Expand Down
Loading