-
Notifications
You must be signed in to change notification settings - Fork 10
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
Major version change: task_helper.sh takes full output control #2
Conversation
9d653fc
to
d65c894
Compare
|
commenting here instead of code since it's more design based.
This is beneficial when things go wrong but I think it is a worst practice.
A primary message from a task is what |
|
@adreyer thanks for the feedback. Trying to translate that into suggested changes, here's what I'm coming up with.
Is this a worst practice for a helper, or is that just commentary on bash script writing practices? "Avoid unnecessary output" is already one of Eric Raymond's 17 Unix Rules, and shell script writers should ideally already be following that. I assert, however, that the helper should faithfully relay whatever output a task writer chooses to permit. It shouldn't be up to the helper to try and suppress anything, and therefore "all script output in an _output key" is helper-correct. Note that as written, if a user doesn't have any output, _output will be an empty string. #!/bin/bash
source "$(dirname $0)/../../bash_task_helper/files/task_helper.sh"
task-output "key" "value"will currently result in {
"key": "value",
"_output": ""
}Adding
If that's the case, perhaps @@ -45,7 +45,7 @@
#
task-fail() {
task-output "status" "error"
- task-output "message" "${1:-(no message given)}"
+ echo -n "$1"
exit ${2:-1}
}
@@ -63,7 +63,7 @@ task-fail() {
#
task-succeed() {
task-output "status" "success"
- task-output "message" "${1:-(no message given)}"
+ echo -n "$1"
exit 0
}
That would change the output from the above, to this: {
"key": "value",
"status": "success",
"_output": ""
}If the user calls {
"key": "value",
"status": "success",
"_output": "we did it!"
}Would that be better aligned with intended idiomatic approach to task return data? |
|
I think it's a worst practice for the helper for the two reasons I outlined. I think these trivial examples are misleading yum install dependent library
rsync
make
task-output "successfully built new app version `bin/app --version`"The output of the yum, rsync, and make commands should be thrown away during successful runs not displayed to the user as an important message. |
files/task_helper.sh
Outdated
| # | ||
| task-succeed() { | ||
| task-output "status" "success" | ||
| task-output "message" "${1:-(no message given)}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO the message should be returned to _output that is what the _output special key is for. In that case (no message given) should not be the default so the user can let task runners print the full data if they don't have an important message
files/task_helper.sh
Outdated
| for i in "${!_task_output_keys[@]}"; do | ||
| printf ' "%s": "%s",\n' "${_task_output_keys[$i]}" "${_task_output_values[$i]}" | ||
| done | ||
| printf ' "_output": "%s"\n' "$(task-json-escape < "$_output_tmpfile")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic should only be called on failure by default.
I think a reasonable API for this would be to have a full-script-output function that returns this output so users can opt into setting on success if they want. either as _output with task-succeed full-script-output or setting it to the variable they want.
|
@adreyer made the following changes:
|
|
After thinking it over, the Personally, if I were looking into this module and saw that it takes |
|
Hey @m0dular, Okay. How about making The documentation then changes from:
To:
|
|
That's a good question, and maybe it would help to answer it if we thought about what we want in terms of exiting and output messages at a high level? Currently,
Otherwise, they just call So I think my main question is: we know |
|
@m0dular I've updated the PR description with the following stanza, which I think clarifies the proposed value of the helper in whole, as well as
In this model, the user does not worry about either stdout or stderr. Both The current implementation only supports strings (and will quote + escape them), but could be expanded in the future to support passing through a "raw" value (user must make sure it is valid JSON) when setting a key. |
This commit refactors task_helper.sh to take full and complete control of output, by trapping EXIT. See commit for details.
This helps keep it very clear when a task helper function is being used.
We have two functions, one for when a task fails and one for when it succeeds. We should standardize on whether our function names are nouns or verbs. This commit makes the names verbs. If we do nouns, it would be failure/success.
Using the special key _output, we can coerce Bolt into displaying terminal output in a human-friendly way when printing to the terminal. Bolt will un-escape the text string for us, showing the end user exactly what they expect to see when they run the script by hand.
Matches better with the _output key this content populates
Make it so that task-output handles re-setting output variables, removing the risk of duplicate keys in the output.
Seems cleaner.
This commit ensures that on task success, _output is minimal, and contains a value only if the user has intentionally set one. On task failure, _output will contain all output for the entire task, as well as any user-set value. On success, if there is no user-set value, the _output key will not be included.
This permits consumers to set their tasks to always return full output
The 2.x API is not fully backwards compatible with the 1.x API, but providing deprecated stubs for these functions should help.
Permit the traps by documenting that consumers must call `task-exit` in their EXIT trap, if they set one.
Otherwise sed will only replace the _first_ instance of that character type, rather than all of them.
This function permits advanced users to set output keys to pre-formatted JSON values.
This commit refactors task_helper.sh to take full and complete control of output, by trapping EXIT. Review code changes for details.
This is a proposed significant change, and so would mandate a major version bump. After this change, the user experience is as follows.
${parameter_name}(rather than e.g.${PT_parameter_name})task-output key valuetask-succeed,task-fail,task-exit, or just let the script run out (rely on the default EXIT trap)Regardless of how the user exits the script, the helper will ensure valid JSON output is printed to stdout, with:
task-output $key $valuetask-succeed:statuskey containing "success"_outputkey containing a user message$1task-fail:statuskey containing "error"_outputkey containing all stdout/stderr output, ending with a user message$1task-succeednortask-failis called but the script terminates with a non-zero exit code:_outputkey containing all stdout/stderr outputThis PR also adds a
task-json-escapehelper function which should work on any of the supported platforms. Design intent is lowest-common-denominator posix tools only. Has been tested on OSX.