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

Allow to pass rendered template as stdin to post-template command #581

Closed
ProvoK opened this issue Aug 15, 2019 · 6 comments · Fixed by #643
Closed

Allow to pass rendered template as stdin to post-template command #581

ProvoK opened this issue Aug 15, 2019 · 6 comments · Fixed by #643
Assignees

Comments

@ProvoK
Copy link

ProvoK commented Aug 15, 2019

As discussed in #578 with @hairyhenderson , it might be useful a flag to permit passing the rendered template as stdin to the command executed when gomplate finishes succesfully its rendering.

Eg:

gomplate -f my_template.tpl -exec-pipe -- my_stdin_reading_application

Other possible flags names:

  • stdin
  • command-stdin
  • pipe
  • ...others?

This would be a great addition since the post-process command is executed only on gomplate success, and will avoid (sometimes) unnecessary writes to file.

⚠️ This features may not be needed if gotemplate could generate output only when it's sure the template could be rendered (alias buffering the output till complete rendering). In that case, a simple shell pipe could do the work.

EDIT: changed stdin to exec-pipe

@charles-dyfis-net
Copy link
Contributor

@ProvoK, ...so, is what you're looking for akin to the following?

maybe_pipe() {
  local -a cmd=( )
  local output
  while [[ $1 != -- ]]; do
    cmd+=( "$1" )
    shift
  done
  [[ $1 = -- ]] && shift
  output=$( "${cmd[@]}" ) && [[ $output ]] && "$@" <<<"$output"
}

maybe_pipe gomplate -f my-template.tpl -stdin -- my_stdin_reading_application

@hairyhenderson
Copy link
Owner

Possible flags names:

  • stdin
  • command-stdin
  • pipe
  • ...others?

How about --exec-pipe? As in "pipe the output to the post-run exec command"... 🤔

In that case, a simple shell pipe could do the work.

TBH I think this feature is still useful - there are users who don't run gomplate in an environment where any shells are installed (such as in a FROM scratch Docker container).

@ProvoK
Copy link
Author

ProvoK commented Aug 18, 2019

@hairyhenderson

How about --exec-pipe? As in "pipe the output to the post-run exec command"... thinking

Seems fine to me :D

TBH I think this feature is still useful - there are users who don't run gomplate in an environment where any shells are installed (such as in a FROM scratch Docker container).

Good call 👍

@ProvoK
Copy link
Author

ProvoK commented Aug 18, 2019

@ProvoK, ...so, is what you're looking for akin to the following?

maybe_pipe() {
  local -a cmd=( )
  local output
  while [[ $1 != -- ]]; do
    cmd+=( "$1" )
    shift
  done
  [[ $1 = -- ]] && shift
  output=$( "${cmd[@]}" ) && [[ $output ]] && "$@" <<<"$output"
}

maybe_pipe gomplate -f my-template.tpl -stdin -- my_stdin_reading_application

@charles-dyfis-net
Seems a bit complicated but it's should be equivalent to
$ rendered_template=$(gomplate -f template.tpl) && echo "$rendered_template" | my_binary
#578 (comment)

The point is.
It's very useful that gomplate can call a command on success: less useful (imho, for use cases such as the one in the comment above) that for consuming gomplate output you have to save it to a file (or use shell tricks).

@charles-dyfis-net
Copy link
Contributor

I believe ifne from moreutils would do the same thing, but more efficiently than any of the above-proposed shell approaches (lauching binary after the first byte of stdin input is seen, rather than collecting all of gomplate's output):

gomplate -f my-template.tpl -stdin | ifne my_binary

@hairyhenderson
Copy link
Owner

@charles-dyfis-net ifne doesn't really help, since it still outputs when the return code is non-zero

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

Successfully merging a pull request may close this issue.

3 participants