Skip to content

Commit

Permalink
chore: improve CLI error messages (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Nov 14, 2022
1 parent 4dfd5a4 commit 423647a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions cmd/relationtuple/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package relationtuple

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -75,18 +76,20 @@ func readTuplesFromArg(cmd *cobra.Command, arg string) ([]*ketoapi.RelationTuple
return nil, cmdx.FailSilently(cmd)
}

decoder := json.NewDecoder(bytes.NewReader(fc))
decoder.DisallowUnknownFields()
// it is ok to not validate beforehand because json.Unmarshal will report errors
if fc[0] == '[' {
var ts []*ketoapi.RelationTuple
if err := json.Unmarshal(fc, &ts); err != nil {
if err := decoder.Decode(&ts); err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not decode: %s\n", err)
return nil, cmdx.FailSilently(cmd)
}
return ts, nil
}

var r ketoapi.RelationTuple
if err := json.Unmarshal(fc, &r); err != nil {
if err := decoder.Decode(&r); err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not decode: %s\n", err)
return nil, cmdx.FailSilently(cmd)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/relationtuple/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewParseCmd() *cobra.Command {
Short: "Parse human readable relation tuples",
Long: "Parse human readable relation tuples as used in the documentation.\n" +
"Supports various output formats. Especially useful for piping into other commands by using `--format json`.\n" +
"Ignores comments (starting with `//`) and blank lines.",
"Ignores comments (lines starting with `//`) and blank lines.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var rts []*ketoapi.RelationTuple
Expand Down

0 comments on commit 423647a

Please sign in to comment.