From f5e3b38a6d0f45e96d03720eaabdf4b41df1e909 Mon Sep 17 00:00:00 2001 From: Zack Brady Date: Wed, 4 Dec 2024 14:29:51 -0500 Subject: [PATCH] updated version flag to internal/flags (#369) --- cmd/hauler/cli/version.go | 6 +++--- internal/flags/version.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 internal/flags/version.go diff --git a/cmd/hauler/cli/version.go b/cmd/hauler/cli/version.go index 24d194ba..b693aa13 100644 --- a/cmd/hauler/cli/version.go +++ b/cmd/hauler/cli/version.go @@ -10,7 +10,7 @@ import ( ) func addVersion(parent *cobra.Command, ro *flags.CliRootOpts) { - var json bool + o := &flags.VersionOpts{} cmd := &cobra.Command{ Use: "version", @@ -23,7 +23,7 @@ func addVersion(parent *cobra.Command, ro *flags.CliRootOpts) { v.FontName = "starwars" cmd.SetOut(cmd.OutOrStdout()) - if json { + if o.JSON { out, err := v.JSONString() if err != nil { return fmt.Errorf("unable to generate JSON from version info: %w", err) @@ -35,7 +35,7 @@ func addVersion(parent *cobra.Command, ro *flags.CliRootOpts) { return nil }, } - cmd.Flags().BoolVar(&json, "json", false, "toggle output in JSON") + o.AddFlags(cmd) parent.AddCommand(cmd) } diff --git a/internal/flags/version.go b/internal/flags/version.go new file mode 100644 index 00000000..be440462 --- /dev/null +++ b/internal/flags/version.go @@ -0,0 +1,12 @@ +package flags + +import "github.com/spf13/cobra" + +type VersionOpts struct { + JSON bool +} + +func (o *VersionOpts) AddFlags(cmd *cobra.Command) { + f := cmd.Flags() + f.BoolVar(&o.JSON, "json", false, "Set the output format to JSON") +}