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

Support piping into set #14

Closed
nodesocket opened this issue May 15, 2016 · 5 comments
Closed

Support piping into set #14

nodesocket opened this issue May 15, 2016 · 5 comments

Comments

@nodesocket
Copy link
Owner

nodesocket commented May 15, 2016

How to support piping into set?

echo "{}" | jsonlite set
@evuez
Copy link

evuez commented May 15, 2016

A simple while read...done should work:

function jsonlite_set {
    while read value; do
        echo "$value" | json_reformat > "$JSONLITE_PATH/$uuid"
    done;
}
echo '{"a": "b"}' | jsonlite_set

@nodesocket
Copy link
Owner Author

nodesocket commented May 15, 2016

Seems like changing the set command to start with:

local value="$1"

if [[ -z "$value" ]]; then
  while read -r piped; do
    value=$piped
  done;
fi

if [[ -z "$value" ]]; then
  echo "Missing required argument json document" 1>&2
  exit 4
fi

Seems to work. Can you think of a better way to write this?

@evuez
Copy link

evuez commented May 15, 2016

The problem here is that (haven't tested it, correct me if I'm wrong) if you do something like for i in $(seq 1 3); do echo '{"a": "b"}'; done | jsonlite set you'll only get the last echo.
Also, with a cat file.json | jsonlite set you would get only the last line of the file in $value.
Maybe a better way would be to do value+="$piped" so that you store the whole file. But then you would get {"a": "b"}{"a": "b"}{"a": "b"} with the first example... I don't know what's best...

@nodesocket
Copy link
Owner Author

I actually got something working:

if [[ -z "$value" && ! -t 0 ]]; then
    while read -r piped; do
      value+=$piped
    done;
fi

The trick was using += and also checking ! -t 0 which checks if we are piping.

@nodesocket nodesocket changed the title Support piping commands that output json to set Support piping into set May 15, 2016
@evuez
Copy link

evuez commented May 16, 2016

That's nice, I didn't know about the ! -t 0!

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

No branches or pull requests

2 participants