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

refactor: Create structs for each tools command to better isolate commands - cont'd #3357

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ func NewZarfCommand() *cobra.Command {
rootCmd.AddCommand(NewInternalCommand(rootCmd))
rootCmd.AddCommand(NewPackageCommand())

rootCmd.AddCommand(NewVersionCommand())

return rootCmd
}

Expand Down
127 changes: 69 additions & 58 deletions src/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,70 +19,81 @@ import (
"github.com/zarf-dev/zarf/src/config/lang"
)

var outputFormat string
// VersionOptions holds the command-line options for 'version' sub-command.
type VersionOptions struct {
outputFormat string
}

var versionCmd = &cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: lang.CmdVersionShort,
Long: lang.CmdVersionLong,
RunE: func(_ *cobra.Command, _ []string) error {
if outputFormat == "" {
fmt.Println(config.CLIVersion)
return nil
}
// NewVersionCommand creates the `version` sub-command.
func NewVersionCommand() *cobra.Command {
o := VersionOptions{}

output := make(map[string]interface{})
output["version"] = config.CLIVersion
cmd := &cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: lang.CmdVersionShort,
Long: lang.CmdVersionLong,
RunE: o.Run,
}

buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return errors.New("failed to get build info")
}
depMap := map[string]string{}
for _, dep := range buildInfo.Deps {
if dep.Replace != nil {
depMap[dep.Path] = fmt.Sprintf("%s -> %s %s", dep.Version, dep.Replace.Path, dep.Replace.Version)
} else {
depMap[dep.Path] = dep.Version
}
}
output["dependencies"] = depMap
cmd.Flags().StringVarP(&o.outputFormat, "output", "o", "", "Output format (yaml|json)")

buildMap := make(map[string]interface{})
buildMap["platform"] = runtime.GOOS + "/" + runtime.GOARCH
buildMap["goVersion"] = runtime.Version()
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()
return cmd
}

// Run performs the execution of 'version' sub-command.
func (o *VersionOptions) Run(_ *cobra.Command, _ []string) error {
if o.outputFormat == "" {
fmt.Println(config.CLIVersion)
return nil
}

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

buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return errors.New("failed to get build info")
}
depMap := map[string]string{}
for _, dep := range buildInfo.Deps {
if dep.Replace != nil {
depMap[dep.Path] = fmt.Sprintf("%s -> %s %s", dep.Version, dep.Replace.Path, dep.Replace.Version)
} else {
depMap[dep.Path] = dep.Version
}
output["build"] = buildMap
}
output["dependencies"] = depMap

buildMap := make(map[string]interface{})
buildMap["platform"] = runtime.GOOS + "/" + runtime.GOARCH
buildMap["goVersion"] = runtime.Version()
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["build"] = buildMap

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

func init() {
versionCmd.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format (yaml|json)")
rootCmd.AddCommand(versionCmd)
return nil
}
Loading