-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from florianl/flo-json
Add JSON output option for `version` and `list`
- Loading branch information
Showing
5 changed files
with
42 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
versionCmd.Flags().BoolVar(&shortened, "short", false, "Print just the version number.") | ||
versionCmd.Flags().BoolVar(&jsonOut, "json", false, "JSON output.") | ||
rootCmd.AddCommand(versionCmd) | ||
} | ||
|
||
var ( | ||
// Versioning | ||
shortened = false | ||
jsonOut = false | ||
version = "dev" | ||
commit = "none" | ||
date = "unknown" | ||
output = "json" | ||
|
||
versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the current version of cernopendata-client-go", | ||
Run: func(_ *cobra.Command, _ []string) { | ||
if shortened { | ||
switch { | ||
case jsonOut: | ||
type versioning struct { | ||
Name string | ||
Version string | ||
Commit string | ||
Date string | ||
} | ||
b, err := json.MarshalIndent(versioning{ | ||
Name: "cernopendata-client-go", | ||
Version: version, | ||
Commit: commit, | ||
Date: date, | ||
}, "", "\t") | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Failed to marshal json: %v", err) | ||
return | ||
} | ||
fmt.Println(string(b)) | ||
case shortened: | ||
fmt.Println(version) | ||
} else { | ||
default: | ||
fmt.Println("cernopendata-client-go", version, "commit", commit, "built at", date) | ||
} | ||
return | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters