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

Add ability to handle go-script output #233

Open
3 tasks done
nkakouros opened this issue Jan 12, 2018 · 2 comments
Open
3 tasks done

Add ability to handle go-script output #233

nkakouros opened this issue Jan 12, 2018 · 2 comments

Comments

@nkakouros
Copy link

Due diligence

Framework, Bash, and operating system version information

go-script: latest master
bash: 4.4.14
Arch linux

Description

In my custom commands, I call a lot of external commands, from apt-get to ansible. I want to have control over command output in order to present to users of my scripts only information that is useful. For instance, they don't care about apt-get output. But they do care about some informational messages that I am echo-ing at some points in the code.

On a custom command level, I can implement the desired behavior by redirecting output of each command that I want to silence to /dev/null. But if there are many commands in a script and, even more importantly, many custom scirpts, it is cumbersome to do that each and every time.

Before moving to go-script-bash, I would do the following at the beginning of my entry-script which would apply to all of the rest of my scripts:

exec 3>&1                                                                                                                                                                          
STDOUT=3                                                                                                                                                                                         
exec 4>&2                                                                                                                                                                          
STDERR=4                                                                                                                                                                            
exec >/dev/null

and have these functions to output my informational messages:

function info() {
  echo "$1" >&${STDOUT}
}
function abort() {
  echo "$1" >&${STDERR}
}

If I do the above while using the go-script, the downside is that all output from go-script, eg help text, autocompletions, etc, are sent to /dev/null.

It would be nice to have go-script redirect its output. There could be a @go.stdout_set function that does:

function @go.stdout() {
  exec 3>&1
}

and go-script outputs to &3. The descriptor could also be in a variable.

Another approach, much slower but more nice-looking to me would be to redirect output to a function that by default simply prints the arguments. Eg:

function -() {
  # for stdout
  echo "$1"
}
function =() {
  # for stderr 
  echo $1 >&2
}

This would need to add a > >(-) after echo's/@go.printf's/... in the code.

In both cases, the user can override these functions and format/edit/silence/... go-script output. Plus, &1 and &2 are left to the user to do however they please (in my case silence them).

If you find this useful, I could implement either of the two approaches.

@mbland
Copy link
Owner

mbland commented Jan 12, 2018

Yes, this is a great idea, which I can appreciate now that I grok fd 3 in Bats. I'd also vote for the first approach, and documenting the specialness of fd 3 (since, per the linked issue, Bash < 4.1 can't use a variable to redirect).

Sorry for the delayed responses here and in bats-core/bats-core. Still reeling from the end-of-year crunch and new year's rentry at work.

@nkakouros
Copy link
Author

Doing a quick scan of the code, I found ~1000 lines that need to be looked into. This will take a while...

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