Skip to content
Snippets Groups Projects
Commit 4b6effe0 authored by Dan Lorenc's avatar Dan Lorenc
Browse files

Add a json output type to the CLI.

parent 78d257d3
No related branches found
No related tags found
No related merge requests found
......@@ -50,7 +50,7 @@ jobs:
with:
args: ./...
- name: Ensure no files were modified as a result of the build
run: git update-index --refresh && git diff-index --quiet HEAD --
run: git update-index --refresh && git diff-index --quiet HEAD -- || git diff
e2e:
# The type of runner that the job will run on
runs-on: ubuntu-latest
......
......@@ -6,6 +6,7 @@ import (
"log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type cobraCmd func(cmd *cobra.Command, args []string)
......@@ -20,14 +21,24 @@ func WrapCmd(f formatCmd) cobraCmd {
}
// TODO: add flags to control output formatting (JSON, plaintext, etc.)
if s, ok := obj.(fmt.Stringer); ok {
fmt.Print(s.String())
} else {
b, err := json.Marshal(obj)
if err != nil {
log.Fatal(err)
format := viper.GetString("format")
switch format {
case "default":
if s, ok := obj.(fmt.Stringer); ok {
fmt.Print(s.String())
} else {
fmt.Println(toJson(s))
}
fmt.Println(string(b))
case "json":
fmt.Println(toJson(obj))
}
}
}
func toJson(i interface{}) string {
b, err := json.Marshal(i)
if err != nil {
log.Fatal(err)
}
return string(b)
}
......@@ -55,6 +55,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.rekor.yaml)")
rootCmd.PersistentFlags().Var(&urlFlag{url: "http://localhost:3000"}, "rekor_server", "Server address:port")
rootCmd.PersistentFlags().Var(&formatFlag{format: "default"}, "format", "Command output format")
// these are bound here and not in PreRun so that all child commands can use them
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
......@@ -130,3 +131,28 @@ func (u *urlFlag) Set(s string) error {
func (u *urlFlag) Type() string {
return "url"
}
type formatFlag struct {
format string
}
func (f *formatFlag) String() string {
return f.format
}
func (f *formatFlag) Set(s string) error {
choices := map[string]struct{}{"default": {}, "json": {}}
if s == "" {
f.format = "default"
return nil
}
if _, ok := choices[s]; ok {
f.format = s
return nil
}
return fmt.Errorf("invalid flag value: %s, valid values are [default, json]", s)
}
func (u *formatFlag) Type() string {
return "format"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment