Skip to content

Commit

Permalink
Merge pull request #9 from florianl/flo-json
Browse files Browse the repository at this point in the history
Add JSON output option for `version` and `list`
  • Loading branch information
clelange authored Jan 11, 2022
2 parents e4ccce4 + f7335a1 commit b7cf4c3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
13 changes: 13 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(listCmd)
listCmd.PersistentFlags().StringVarP(&protocol, "protocol", "p", "http", "Protocol to be used (http or root)")
listCmd.Flags().BoolVar(&jsonOut, "json", false, "JSON output.")

}

var (
Expand All @@ -28,6 +32,15 @@ var (
if err != nil {
er(err)
}
if jsonOut {
b, err := json.MarshalIndent(recordJSON, "", "\t")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to marshal json: %v", err)
return
}
fmt.Println(string(b))
return
}
filesList, err := getFilesList(recordJSON)
if err != nil {
er(err)
Expand Down
29 changes: 25 additions & 4 deletions cmd/version.go
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
},
}
)
2 changes: 1 addition & 1 deletion docs/cernopendata-client-go.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ files and complete records.
* [cernopendata-client-go download](cernopendata-client-go_download.md) - Download files belonging to a record
* [cernopendata-client-go list](cernopendata-client-go_list.md) - Get a list of data file locations of a record

###### Auto generated by spf13/cobra on 21-Oct-2021
###### Auto generated by spf13/cobra on 10-Jan-2022
2 changes: 1 addition & 1 deletion docs/cernopendata-client-go_download.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ cernopendata-client-go download [flags]

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

###### Auto generated by spf13/cobra on 21-Oct-2021
###### Auto generated by spf13/cobra on 10-Jan-2022
3 changes: 2 additions & 1 deletion docs/cernopendata-client-go_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cernopendata-client-go list [flags]

```
-h, --help help for list
--json JSON output.
-p, --protocol string Protocol to be used (http or root) (default "http")
```

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

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

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

0 comments on commit b7cf4c3

Please sign in to comment.