-
-
Notifications
You must be signed in to change notification settings - Fork 93
Add stacktrace
standard library
#635
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
03b7856
- Add `stacktrace` standard library
DannyBen 1f17079
change `echo -e` to `printf`
DannyBen b94baf2
specs pass
DannyBen 52073cc
- Add examples/stacktrace
DannyBen 80f2031
fix stacktrace example readme
DannyBen cac2b1f
fix shfmt violation
DannyBen ed3da9d
let stacktrace exit code pass through
DannyBen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
download |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# Stack Trace Example | ||
|
||
Demonstrates how to show stack trace on error. | ||
|
||
This example was generated with: | ||
|
||
```bash | ||
$ bashly init --minimal | ||
$ bashly add stacktrace | ||
$ bashly generate | ||
# ... now create and edit src/initialize.sh to match the example ... | ||
# ... now edit src/root_command.sh to match the example ... | ||
$ bashly generate | ||
``` | ||
|
||
<!-- include: src/initialize.sh src/root_command.sh --> | ||
|
||
----- | ||
|
||
## `bashly.yml` | ||
|
||
````yaml | ||
name: download | ||
help: Sample minimal application without commands | ||
version: 0.1.0 | ||
|
||
args: | ||
- name: source | ||
required: true | ||
help: URL to download from | ||
- name: target | ||
help: "Target filename (default: same as source)" | ||
|
||
flags: | ||
- long: --force | ||
short: -f | ||
help: Overwrite existing files | ||
|
||
examples: | ||
- download example.com | ||
- download example.com ./output -f | ||
```` | ||
|
||
## `src/initialize.sh` | ||
|
||
````bash | ||
## initialize hook | ||
## | ||
## Any code here will be placed inside the `initialize()` function and called | ||
## before running anything else. | ||
## | ||
## You can safely delete this file if you do not need it. | ||
trap 'stacktrace' ERR | ||
set -o errtrace | ||
|
||
```` | ||
|
||
## `src/root_command.sh` | ||
|
||
````bash | ||
## trigger an error by calling a function that does not exist | ||
no_such_command | ||
|
||
```` | ||
|
||
|
||
## Output | ||
|
||
### `$ ./download` | ||
|
||
````shell | ||
missing required argument: SOURCE | ||
usage: download SOURCE [TARGET] [OPTIONS] | ||
|
||
|
||
```` | ||
|
||
### `$ ./download --help` | ||
|
||
````shell | ||
download - Sample minimal application without commands | ||
|
||
Usage: | ||
download SOURCE [TARGET] [OPTIONS] | ||
download --help | -h | ||
download --version | -v | ||
|
||
Options: | ||
--force, -f | ||
Overwrite existing files | ||
|
||
--help, -h | ||
Show this help | ||
|
||
--version, -v | ||
Show version number | ||
|
||
Arguments: | ||
SOURCE | ||
URL to download from | ||
|
||
TARGET | ||
Target filename (default: same as source) | ||
|
||
Examples: | ||
download example.com | ||
download example.com ./output -f | ||
|
||
|
||
|
||
```` | ||
|
||
### `$ ./download something` | ||
|
||
````shell | ||
./download: line 15: no_such_command: command not found | ||
./download:15 in `root_command`: no_such_command | ||
|
||
Stack trace: | ||
from ./download:15 in `root_command` | ||
from ./download:254 in `run` | ||
from ./download:260 in `main` | ||
|
||
|
||
```` | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: download | ||
help: Sample minimal application without commands | ||
version: 0.1.0 | ||
|
||
args: | ||
- name: source | ||
required: true | ||
help: URL to download from | ||
- name: target | ||
help: "Target filename (default: same as source)" | ||
|
||
flags: | ||
- long: --force | ||
short: -f | ||
help: Overwrite existing files | ||
|
||
examples: | ||
- download example.com | ||
- download example.com ./output -f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## initialize hook | ||
## | ||
## Any code here will be placed inside the `initialize()` function and called | ||
## before running anything else. | ||
## | ||
## You can safely delete this file if you do not need it. | ||
trap 'stacktrace' ERR | ||
set -o errtrace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
## Stack trace functions [@bashly-upgrade stacktrace] | ||
## This file is a part of Bashly standard library | ||
## | ||
## Usage: | ||
## This function is designed to be called on error. | ||
## | ||
## To enable this functionality, add these lines to your `src/initialize.sh` | ||
## file (Run `bashly add hooks` to add this file): | ||
## | ||
## trap 'stacktrace' ERR | ||
## set -o errtrace | ||
## | ||
## Note that this functionality also requires `set -e`, which is enabled by | ||
## default in bashly generated scripts. | ||
## | ||
stacktrace() { | ||
local i=0 | ||
local caller_output line func file | ||
|
||
printf "%s:%s in \`%s\`: %s\n" "${BASH_SOURCE[0]}" "${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$BASH_COMMAND" | ||
printf "\nStack trace:\n" | ||
while caller_output="$(caller $i)"; do | ||
read -r line func file <<<"$caller_output" | ||
printf "\tfrom %s:%s in \`%s\`\n" "$file" "$line" "$func" | ||
i=$((i + 1)) | ||
done | ||
exit 1 | ||
} >&2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## trigger an error by calling a function that does not exist | ||
no_such_command |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -x | ||
|
||
bashly add stacktrace --force | ||
bashly generate | ||
|
||
### Try Me ### | ||
|
||
./download | ||
./download --help | ||
./download something |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
## Stack trace functions [@bashly-upgrade stacktrace] | ||
## This file is a part of Bashly standard library | ||
## | ||
## Usage: | ||
## This function is designed to be called on error. | ||
## | ||
## To enable this functionality, add these lines to your `src/initialize.sh` | ||
## file (Run `bashly add hooks` to add this file): | ||
## | ||
## trap 'stacktrace' ERR | ||
## set -o errtrace | ||
## | ||
## Note that this functionality also requires `set -e`, which is enabled by | ||
## default in bashly generated scripts. | ||
## | ||
stacktrace() { | ||
local i=0 | ||
local caller_output line func file | ||
|
||
printf "%s:%s in \`%s\`: %s\n" "${BASH_SOURCE[0]}" "${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$BASH_COMMAND" | ||
printf "\nStack trace:\n" | ||
while caller_output="$(caller $i)"; do | ||
read -r line func file <<<"$caller_output" | ||
printf "\tfrom %s:%s in \`%s\`\n" "$file" "$line" "$func" | ||
i=$((i + 1)) | ||
done | ||
exit 1 | ||
} >&2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
+ bashly add stacktrace --force | ||
created src/lib/stacktrace.sh | ||
|
||
The stacktrace function is designed to be called automatically on error. | ||
|
||
To enable this functionality, add these lines to your `initialize.sh`: | ||
|
||
trap 'stacktrace' ERR | ||
set -o errtrace | ||
|
||
+ bashly generate | ||
creating user files in src | ||
skipped src/root_command.sh (exists) | ||
created ./download | ||
run ./download --help to test your bash script | ||
+ ./download | ||
missing required argument: SOURCE | ||
usage: download SOURCE [TARGET] [OPTIONS] | ||
+ ./download --help | ||
download - Sample minimal application without commands | ||
|
||
Usage: | ||
download SOURCE [TARGET] [OPTIONS] | ||
download --help | -h | ||
download --version | -v | ||
|
||
Options: | ||
--force, -f | ||
Overwrite existing files | ||
|
||
--help, -h | ||
Show this help | ||
|
||
--version, -v | ||
Show version number | ||
|
||
Arguments: | ||
SOURCE | ||
URL to download from | ||
|
||
TARGET | ||
Target filename (default: same as source) | ||
|
||
Examples: | ||
download example.com | ||
download example.com ./output -f | ||
|
||
+ ./download something | ||
./download: line 15: no_such_command: command not found | ||
./download:15 in `root_command`: no_such_command | ||
|
||
Stack trace: | ||
from ./download:15 in `root_command` | ||
from ./download:254 in `run` | ||
from ./download:260 in `main` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.