Skip to content

Commit

Permalink
JSON output in version command
Browse files Browse the repository at this point in the history
  • Loading branch information
alex27riva committed Oct 17, 2024
1 parent a9e8287 commit 936cab4
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,43 @@ See the LICENSE file for details.
package cmd

import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"log"
)

var Version = "dev"

type verOutput struct {
Version string `json:"version"`
}

func displayVersion(asJSON bool) {
if asJSON {
jsonData, err := json.MarshalIndent(verOutput{Version: Version}, "", " ")
if err != nil {
log.Fatalf("Error marshalling JSON: %v", err)
}
fmt.Println(string(jsonData))

} else {
fmt.Printf("soc-cli version: %s\n", Version)
}

}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Show the version of the program",
Long: `Display the current version of this CLI tool.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("soc-cli version: %s\n", Version)
asJSON, _ := cmd.Flags().GetBool("json")
displayVersion(asJSON)
},
}

func init() {
versionCmd.Flags().Bool("json", false, "Output version in JSON format")
rootCmd.AddCommand(versionCmd)
}

0 comments on commit 936cab4

Please sign in to comment.