From cad619e1054e54477930b12a3ee1fa38822ae7b1 Mon Sep 17 00:00:00 2001 From: Maina Wycliffe Date: Sun, 12 Apr 2020 23:44:04 +0300 Subject: [PATCH] Use Global Output Flag Instead of Local One Before I had a local flag for version but this now being replaced by a single global flag for outputs as it will be required in multiple commands. --- cmd/root.go | 2 +- cmd/version.go | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 5b0fd97..807facb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -48,7 +48,7 @@ func init() { // this can be used to pass project alias to sub commands, incase having // multiple projects rootCmd.PersistentFlags().StringP("project", "P", "default", "The firebase project to use") - findUserCmd.PersistentFlags().StringP("output", "o", "", "A file where data from firebase will be saved.") + rootCmd.PersistentFlags().StringP("output", "o", "", "A file where data from firebase will be saved.") rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/version.go b/cmd/version.go index f968dd3..fca4dc1 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -17,7 +17,6 @@ var ( commit = "none" date = "unknown" builtBy = "goreleaser" - output = "text" ) // versionCmd represents the version command @@ -26,6 +25,11 @@ var versionCmd = &cobra.Command{ Aliases: []string{"v"}, Short: "Version will output the current build information", Run: func(cmd *cobra.Command, args []string) { + output, err := cmd.Flags().GetString("output") + if err != nil { + utils.StdOutError(os.Stderr, "Error reading output %s", err.Error()) + os.Exit(1) + } kamandaVersion := map[string]string{ "version": version, "commitHash": commit, @@ -33,16 +37,6 @@ var versionCmd = &cobra.Command{ "built": date, "OS/Arch": fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), } - if output == "text" { - t := tabby.New() - t.AddLine("Version:", version) - t.AddLine("Release Date:", date) - t.AddLine("Commit Hash:", commit) - t.AddLine("Built by:", builtBy) - t.AddLine("OS/Arch:", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)) - t.Print() - os.Exit(0) - } if output == "json" { output, err := json.Marshal(kamandaVersion) if err != nil { @@ -61,10 +55,18 @@ var versionCmd = &cobra.Command{ fmt.Printf("%s\n", output) os.Exit(0) } + // default to text output + t := tabby.New() + t.AddLine("Version:", version) + t.AddLine("Release Date:", date) + t.AddLine("Commit Hash:", commit) + t.AddLine("Built by:", builtBy) + t.AddLine("OS/Arch:", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)) + t.Print() + os.Exit(0) }, } func init() { rootCmd.AddCommand(versionCmd) - versionCmd.Flags().StringVarP(&output, "output", "o", "text", "Output format. One of 'Text', 'yaml' or 'json'.") }