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

Exec as argument of next Exec calls #160

Open
ifraixedes opened this issue Dec 14, 2022 · 2 comments
Open

Exec as argument of next Exec calls #160

ifraixedes opened this issue Dec 14, 2022 · 2 comments

Comments

@ifraixedes
Copy link

Writing #159 I thought that it could be good to have a way to use the stdout of a command executed with Exec as parameter of a following Exec call.

For being even more useful, it could be possible to support several Exec calls.
And to even go further (sorry, I'm blowing my mind), they could also be used as values to replace on a specific format string.

NOTE I'm not familiar with the script implementation, so I don't know if this is feasible, or if it's hard to implement or not.

Let me write some example of an hypothetical API for each of the above wacky ideas.

Right now, script allows to do this

script.Exec("echo hello world").Exec("cut -d ' ' -f 1").Output()

This translate to the shell script

echo hello world | cut -d ' ' -f 1

I'm exposing an example with shell script that I think that I cannot translate using script to a pipe execution as before; each numbered item relates, in the same order, to the three things that I exposed at the beginning of this issue.
For keeping the examples concise, they are ilustrative examples rather than a specific use case that I'm having.

  1.  ls $(lsb_release -cs)
  2.  ls $(lsb_release -cs) $(dpkg --print-architecture)
  3.  echo  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

In script isn't currently possible without having to execute each of them speartelly. I'm going to show how script could offer it with some unexisting methods without stating if their names are good nor if it's a good idea to have them.

  1. There should be somehow to indicates that the output of the previous Exec is passed as an argument on the following Exec call.
script.Exec("lsb_release -cs").AsArg().Exec("ls").Output()
  1. There should be somehow as 1 but with the possibility to pipe several Exec calls.
script.Exec("lsb_release -cs").AsArg().Exec("dpkg --print-architecture").AsArg().Exec("ls").Output()
  1. There should be somehow to indicates that the outputs are kept as an internal value that can be referenced in the formatter.
script.Exec("lsb_release -cs").AsFmt().Exec("dpk --print-architecture").AsFmt().Exec(`echo  "deb [arch=@{1} signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian @{2} stable"`).Output()

In this case I used echo inside of an Exec, but it could be extended to each source functions that receive a string as a parameter, hence it could be also done as

script.Exec("lsb_release -cs").AsFmt().Exec("dpk --print-architecture").AsFmt().Echo("deb [arch=@{1} signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian @{2} stable").Output()
@bitfield
Copy link
Owner

It may help to know that you can use ExecForEach with a Go template. For example:

script.Exec("lsb_release -cs").ExecForEach("ls {{.}}").Stdout()

@ifraixedes
Copy link
Author

ifraixedes commented Dec 15, 2022

Thank you, I missed that method.

Can I also use it for the 3rd case?

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

No branches or pull requests

2 participants