From 90b764bbe0fa4fdf2ace164ca10c2a899e1013a7 Mon Sep 17 00:00:00 2001 From: Maciej Szulik Date: Fri, 20 Dec 2024 17:37:47 +0100 Subject: [PATCH] refactor: Create structs for each tools command to better isolate commands - cont'd (#3357) Signed-off-by: Maciej Szulik --- src/cmd/root.go | 2 + src/cmd/version.go | 127 ++++++++++++++++++++++++--------------------- 2 files changed, 71 insertions(+), 58 deletions(-) diff --git a/src/cmd/root.go b/src/cmd/root.go index 153d31c1c9..03665fbd9e 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -145,6 +145,8 @@ func NewZarfCommand() *cobra.Command { rootCmd.AddCommand(NewInternalCommand(rootCmd)) rootCmd.AddCommand(NewPackageCommand()) + rootCmd.AddCommand(NewVersionCommand()) + return rootCmd } diff --git a/src/cmd/version.go b/src/cmd/version.go index a08af2aaab..c98d8a6552 100644 --- a/src/cmd/version.go +++ b/src/cmd/version.go @@ -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 }