Skip to content

Commit

Permalink
implement hyphen for stdin (ref #90)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jul 14, 2021
1 parent 80e93a1 commit ec1d097
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
6 changes: 4 additions & 2 deletions cli/cli.go
Expand Up @@ -260,7 +260,9 @@ Synopsis:
}

func slurpFile(name string) (interface{}, error) {
iter := newSlurpInputIter(newFilesInputIter(newJSONInputIter, []string{name}))
iter := newSlurpInputIter(
newFilesInputIter(newJSONInputIter, []string{name}, nil),
)
defer iter.Close()
val, _ := iter.Next()
if err, ok := val.(error); ok {
Expand Down Expand Up @@ -293,7 +295,7 @@ func (cli *cli) createInputIter(args []string) (iter inputIter) {
if len(args) == 0 {
return newIter(cli.inStream, "<stdin>")
}
return newFilesInputIter(newIter, args)
return newFilesInputIter(newIter, args, cli.inStream)
}

func (cli *cli) process(iter inputIter, code *gojq.Code) error {
Expand Down
33 changes: 22 additions & 11 deletions cli/inputs.go
Expand Up @@ -147,13 +147,16 @@ func (i *nullInputIter) Name() string {
type filesInputIter struct {
newIter func(io.Reader, string) inputIter
fnames []string
stdin io.Reader
iter inputIter
file *os.File
file io.Reader
err error
}

func newFilesInputIter(newIter func(io.Reader, string) inputIter, fnames []string) inputIter {
return &filesInputIter{newIter: newIter, fnames: fnames}
func newFilesInputIter(
newIter func(io.Reader, string) inputIter, fnames []string, stdin io.Reader,
) inputIter {
return &filesInputIter{newIter: newIter, fnames: fnames, stdin: stdin}
}

func (i *filesInputIter) Next() (interface{}, bool) {
Expand All @@ -168,11 +171,15 @@ func (i *filesInputIter) Next() (interface{}, bool) {
}
fname := i.fnames[0]
i.fnames = i.fnames[1:]
file, err := os.Open(fname)
if err != nil {
return err, true
if fname == "-" && i.stdin != nil {
i.file, fname = i.stdin, "<stdin>"
} else {
file, err := os.Open(fname)
if err != nil {
return err, true
}
i.file = file
}
i.file = file
if i.iter != nil {
i.iter.Close()
}
Expand All @@ -181,23 +188,27 @@ func (i *filesInputIter) Next() (interface{}, bool) {
if v, ok := i.iter.Next(); ok {
return v, ok
}
i.file.Close()
if r, ok := i.file.(io.Closer); ok && i.file != i.stdin {
r.Close()
}
i.file = nil
}
}

func (i *filesInputIter) Close() error {
if i.file != nil {
i.file.Close()
if r, ok := i.file.(io.Closer); ok && i.file != i.stdin {
r.Close()
}
i.file = nil
i.err = io.EOF
}
return nil
}

func (i *filesInputIter) Name() string {
if i.file != nil {
return i.file.Name()
if i.iter != nil {
return i.iter.Name()
}
return ""
}
Expand Down
44 changes: 44 additions & 0 deletions cli/test.yaml
Expand Up @@ -4197,6 +4197,20 @@
"testdata/1.json"
"testdata/2.json"
- name: input_filename function with hyphen for stdin
args:
- 'input_filename'
- '-'
- 'testdata/1.json'
- '-'
- 'testdata/2.json'
- '-'
input: '0'
expected: |
"<stdin>"
"testdata/1.json"
"testdata/2.json"
- name: input_filename function with yaml input option
args:
- --yaml-input
Expand Down Expand Up @@ -5566,6 +5580,18 @@
"bar"
]
- name: yaml input option with hyphen for stdin
args:
- --yaml-input
- '.'
- '-'
input: |
foo: 1
expected: |
{
"foo": 1
}
- name: yaml input option error
args:
- --yaml-input
Expand Down Expand Up @@ -5915,6 +5941,14 @@
"bar": []
}
- name: hyphen for stdin
args:
- '.'
- '-'
input: '{}'
expected: |
{}
- name: json file arguments
args:
- '.'
Expand Down Expand Up @@ -6324,6 +6358,16 @@
.foo.bar
^ invalid character '.' looking for beginning of value
- name: slurpfile option error
args:
- --slurpfile
- 'foo'
- '-'
- '{ $foo }'
input: 'null'
error: |
open -: no such file or directory
- name: rawfile option
args:
- --rawfile
Expand Down

0 comments on commit ec1d097

Please sign in to comment.