Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
RothAndrew authored Dec 20, 2024
2 parents 24406d1 + 90b764b commit 0ff45cb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 58 deletions.
2 changes: 2 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,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
}

0 comments on commit 0ff45cb

Please sign in to comment.