Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't print help errors to stdout #440

Closed
xordspar0 opened this issue May 19, 2020 · 3 comments
Closed

Don't print help errors to stdout #440

xordspar0 opened this issue May 19, 2020 · 3 comments
Labels
Milestone

Comments

@xordspar0
Copy link

Describe the bug
When yq reports an error, it prints it (along with the usage docs) on standard out. That is unhelpful, especially if I'm piping stdout to another command.

Input Yaml
No yaml is needed to reproduce this.

Command

yq read nonexistent.yaml search_key | awk '{ print $3 }'

Actual behavior

11:51:07 main [ERRO] open nonexistent.yaml: no such file or directory
nonexistent.yaml:

[yaml_file]






things.yaml
-
things.yaml
things.yaml
things.yaml
-d1
things.yaml
--



collect
string
string
explode
help
print
string
yaml
scalar,


print
int
pretty
output
verbose

Expected behavior

Error: open nonexistent.yaml: no such file or directory
Usage:
  yq read [yaml_file] [path_expression] [flags]

Aliases:
  read, r

Examples:

yq read things.yaml 'a.b.c'
yq r - 'a.b.c' # reads from stdin
yq r things.yaml 'a.*.c'
yq r things.yaml 'a.**.c' # deep splat
yq r things.yaml 'a.(child.subchild==co*).c'
yq r -d1 things.yaml 'a.array[0].blah'
yq r things.yaml 'a.array[*].blah'
yq r -- things.yaml '--key-starting-with-dashes.blah'


Flags:
  -c, --collect               collect results into array
  -D, --defaultValue string   default value printed when there are no results
  -d, --doc string            process document index number (0 based, * for all documents) (default "0")
  -X, --explodeAnchors        explode anchors
  -h, --help                  help for read
  -l, --length                print length of results
  -p, --printMode string      print mode (v (values, default), p (paths), pv (path and value pairs) (default "v")
      --stripComments         print yaml without any comments
      --unwrapScalar          unwrap scalar, print the value with no quotes, colors or comments (default true)

Global Flags:
  -C, --colors        print with colors
  -I, --indent int    sets indent level for output (default 2)
  -P, --prettyPrint   pretty print
  -j, --tojson        output as json. By default it prints a json document in one line, use the prettyPrint flag to print a formatted doc.
  -v, --verbose       verbose mode

11:54:32 main [ERRO] open nonexistent.yaml: no such file or directory

Additional context
None

@xordspar0 xordspar0 added the bug label May 19, 2020
@xordspar0
Copy link
Author

I think this is the line that configures this:

yq/cmd/root.go

Line 58 in 6fc3566

rootCmd.SetOutput(os.Stdout)

It was introduced originally in this commit, but I don't understand the context: cf2f23d

@mikefarah
Copy link
Owner

Ah yep - I see usage of SetOutput is deprecated in the latest spf13/cobra and you can pipe StdOut and StdErr separately.

https://godoc.org/github.com/spf13/cobra#Command

Will fix.

@alexiswl
Copy link

Hello,

Looks like this might still be an issue

$ yq --version
yq (https://github.com/mikefarah/yq/) version 4.23.1
$ BUNDLE_CATEGORIES="${bundle_categories}" \
yq --unwrapScalar     '.bundles[] |
      select(
        contains(
          {
            "bundle_categories": [ env(BUNDLE_CATEGORIES) ]
          }
        )
      ) |
    {
      "bundle_id": .bundle_id,
      "bundle_name": .bundle_name
    }
' 2>/dev/null

Yields

Usage:
  yq eval [expression] [yaml_file1]... [flags]

Aliases:
  eval, e

Examples:

# Reads field under the given path for each file
yq e '.a.b' f1.yml f2.yml

# Prints out the file
yq e sample.yaml

# Pipe from STDIN
## use '-' as a filename to pipe from STDIN
cat file2.yml | yq e '.a.b' file1.yml - file3.yml

# Creates a new yaml document
## Note that editing an empty file does not work.
yq e -n '.a.b.c = "cat"'

# Update a file inplace
yq e '.a.b = "cool"' -i file.yaml


Flags:
  -h, --help   help for eval

Global Flags:
  -C, --colors                        force print with colors
  -e, --exit-status                   set exit status if there are no matches or null or false is returned
      --expression string             forcibly set the expression argument. Useful when yq argument detection thinks your expression is a file.
      --from-file string              Load expression from specified file.
  -f, --front-matter string           (extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact
      --header-preprocess             Slurp any header comments and separators before processing expression. (default true)
  -I, --indent int                    sets indent level for output (default 2)
  -i, --inplace                       update the file inplace of first file given.
  -p, --input-format string           [yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml. (default "yaml")
  -M, --no-colors                     force print with no colors
  -N, --no-doc                        Don't print document separators (---)
  -n, --null-input                    Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.
  -o, --output-format string          [yaml|y|json|j|props|p|xml|x] output format type. (default "yaml")
  -P, --prettyPrint                   pretty print, shorthand for '... style = ""'
  -s, --split-exp string              print each result (or doc) into a file named (exp). [exp] argument must return a string. You can use $index in the expression as the result counter.
      --unwrapScalar                  unwrap scalar, print the value with no quotes, colors or comments (default true)
  -v, --verbose                       verbose mode
      --xml-attribute-prefix string   prefix for xml attributes (default "+")
      --xml-content-name string       name for xml content (if no attribute name is present). (default "+content")

Error was that I forgot to specify the actual file to query on. But it was concerning that it had a zero exit code.

This tripped me up as I tried to assign the output to a variable and it worked without realising there was an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants