Skip to content

Commit b7cf4c3

Browse files
authored
Merge pull request #9 from florianl/flo-json
Add JSON output option for `version` and `list`
2 parents e4ccce4 + f7335a1 commit b7cf4c3

5 files changed

+42
-7
lines changed

cmd/list.go

+13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
57

68
"github.com/spf13/cobra"
79
)
810

911
func init() {
1012
rootCmd.AddCommand(listCmd)
1113
listCmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "http", "Protocol to be used (http or root)")
14+
listCmd.Flags().BoolVar(&jsonOut, "json", false, "JSON output.")
15+
1216
}
1317

1418
var (
@@ -28,6 +32,15 @@ var (
2832
if err != nil {
2933
er(err)
3034
}
35+
if jsonOut {
36+
b, err := json.MarshalIndent(recordJSON, "", "\t")
37+
if err != nil {
38+
fmt.Fprintf(os.Stderr, "Failed to marshal json: %v", err)
39+
return
40+
}
41+
fmt.Println(string(b))
42+
return
43+
}
3144
filesList, err := getFilesList(recordJSON)
3245
if err != nil {
3346
er(err)

cmd/version.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,55 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
57

68
"github.com/spf13/cobra"
79
)
810

911
func init() {
1012
versionCmd.Flags().BoolVar(&shortened, "short", false, "Print just the version number.")
13+
versionCmd.Flags().BoolVar(&jsonOut, "json", false, "JSON output.")
1114
rootCmd.AddCommand(versionCmd)
1215
}
1316

1417
var (
1518
// Versioning
1619
shortened = false
20+
jsonOut = false
1721
version = "dev"
1822
commit = "none"
1923
date = "unknown"
20-
output = "json"
2124

2225
versionCmd = &cobra.Command{
2326
Use: "version",
2427
Short: "Print the current version of cernopendata-client-go",
2528
Run: func(_ *cobra.Command, _ []string) {
26-
if shortened {
29+
switch {
30+
case jsonOut:
31+
type versioning struct {
32+
Name string
33+
Version string
34+
Commit string
35+
Date string
36+
}
37+
b, err := json.MarshalIndent(versioning{
38+
Name: "cernopendata-client-go",
39+
Version: version,
40+
Commit: commit,
41+
Date: date,
42+
}, "", "\t")
43+
if err != nil {
44+
fmt.Fprintf(os.Stderr, "Failed to marshal json: %v", err)
45+
return
46+
}
47+
fmt.Println(string(b))
48+
case shortened:
2749
fmt.Println(version)
28-
} else {
50+
default:
2951
fmt.Println("cernopendata-client-go", version, "commit", commit, "built at", date)
3052
}
31-
return
3253
},
3354
}
3455
)

docs/cernopendata-client-go.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ files and complete records.
2323
* [cernopendata-client-go download](cernopendata-client-go_download.md) - Download files belonging to a record
2424
* [cernopendata-client-go list](cernopendata-client-go_list.md) - Get a list of data file locations of a record
2525

26-
###### Auto generated by spf13/cobra on 21-Oct-2021
26+
###### Auto generated by spf13/cobra on 10-Jan-2022

docs/cernopendata-client-go_download.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ cernopendata-client-go download [flags]
3333

3434
* [cernopendata-client-go](cernopendata-client-go.md) - A commandline tool to interact with the CERN Open Data portal
3535

36-
###### Auto generated by spf13/cobra on 21-Oct-2021
36+
###### Auto generated by spf13/cobra on 10-Jan-2022

docs/cernopendata-client-go_list.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cernopendata-client-go list [flags]
1515

1616
```
1717
-h, --help help for list
18+
--json JSON output.
1819
-p, --protocol string Protocol to be used (http or root) (default "http")
1920
```
2021

@@ -31,4 +32,4 @@ cernopendata-client-go list [flags]
3132

3233
* [cernopendata-client-go](cernopendata-client-go.md) - A commandline tool to interact with the CERN Open Data portal
3334

34-
###### Auto generated by spf13/cobra on 21-Oct-2021
35+
###### Auto generated by spf13/cobra on 10-Jan-2022

0 commit comments

Comments
 (0)