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: ...@@ -50,7 +50,7 @@ jobs:
with: with:
args: ./... args: ./...
- name: Ensure no files were modified as a result of the build - 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: e2e:
# The type of runner that the job will run on # The type of runner that the job will run on
runs-on: ubuntu-latest runs-on: ubuntu-latest
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"log" "log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
type cobraCmd func(cmd *cobra.Command, args []string) type cobraCmd func(cmd *cobra.Command, args []string)
...@@ -20,14 +21,24 @@ func WrapCmd(f formatCmd) cobraCmd { ...@@ -20,14 +21,24 @@ func WrapCmd(f formatCmd) cobraCmd {
} }
// TODO: add flags to control output formatting (JSON, plaintext, etc.) // TODO: add flags to control output formatting (JSON, plaintext, etc.)
if s, ok := obj.(fmt.Stringer); ok { format := viper.GetString("format")
fmt.Print(s.String()) switch format {
} else { case "default":
b, err := json.Marshal(obj) if s, ok := obj.(fmt.Stringer); ok {
if err != nil { fmt.Print(s.String())
log.Fatal(err) } 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() { ...@@ -55,6 +55,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.rekor.yaml)") 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(&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 // these are bound here and not in PreRun so that all child commands can use them
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil { if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
...@@ -130,3 +131,28 @@ func (u *urlFlag) Set(s string) error { ...@@ -130,3 +131,28 @@ func (u *urlFlag) Set(s string) error {
func (u *urlFlag) Type() string { func (u *urlFlag) Type() string {
return "url" 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