Skip to content

Commit 356560c

Browse files
committed
refactor: Convert bash-core to one file
1 parent c28c32f commit 356560c

File tree

6 files changed

+86
-118
lines changed

6 files changed

+86
-118
lines changed

Bakefile.sh

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,5 @@ task.test() {
55
}
66

77
task.docs() {
8-
shdoc > './docs/api.md' < <(
9-
for f in ./pkg/lib/public/*.sh; do
10-
cat "$f"
11-
done
12-
)
8+
shdoc < './pkg/lib/public/bash-core.sh' > './docs/api.md'
139
}

README.md

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# bash-core
22

3-
Useful functions for any Bash program. Often vital for Basalt programs
3+
Core functions for any Bash program
44

55
## Summary
66

7+
The following are functions available for use. See the [api.md](./docs/api.md) for more details
8+
79
### `init`
810

911
- `core.init`
@@ -17,28 +19,11 @@ Add and remove traps. With these, multiple packages can add or remove traps hand
1719
- `core.trap_add`
1820
- `core.trap_remove`
1921

20-
Example
21-
22-
```sh
23-
some_handler() { printf '%s\n' 'This was called on USR1! ^w^'; }
24-
core.trap_add 'some_handler' 'USR1'
25-
kill -USR1 $$
26-
core.trap_remove 'some_handler' 'USR1'
27-
```
28-
2922
### `shopt`
3023

3124
- `core.shopt_push`
3225
- `core.shopt_pop`
3326

34-
Example
35-
36-
```sh
37-
core.shopt_push -s extglob
38-
[[ 'variable' == @(foxtrot|golf|echo|variable) ]] && printf '%s\n' 'Woof!'
39-
core.shopt_pop
40-
```
41-
4227
### `err`
4328

4429
I suppose it can look redundant (compared to `if ! fn; then :; fi`), but it can help make errors a bit more safe in larger applications, since you don't have to worry about a caller forgetting to `if ! fn` or `fn ||` (and terminating the script if `set -e`). It also makes it easier to communicate specific error codes and helps separate between calculated / expected errors and unexpected errors (fatal / faults)
@@ -47,23 +32,12 @@ I suppose it can look redundant (compared to `if ! fn; then :; fi`), but it can
4732
- `core.err_clear`
4833
- `core.err_exists`
4934

50-
## `stacktrace`
35+
### `stacktrace`
5136

5237
Prints the stack trace. Recommended to use with `core.trap_add`
5338

5439
- `core.stacktrace_print`
5540

56-
Example
57-
58-
```sh
59-
create_some_error() { core.err_clear; core.err_set 'Some error' }
60-
61-
create_some_error || fatal "Did not expect an error"
62-
if core.err_exists; then
63-
printf '%s\n' 'Something happened'
64-
fi
65-
```
66-
6741
## Installation
6842

6943
Use [Basalt](https://github.com/hyperupcall/basalt), a Bash package manager, to add this project as a dependency

docs/api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
Core functions for any large Bash program
5+
Core functions for any Bash program
66

77
## Index
88

@@ -99,8 +99,8 @@ Sets an error.
9999

100100
#### Arguments
101101

102-
* # @args $1 Error code
103-
* # @args $2 Error message
102+
* **$1** (Error): code
103+
* **$2** (Error): message
104104

105105
#### Variables set
106106

pkg/lib/public/bash-core.sh

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# shellcheck shell=bash
22

33
# @name bash-core
4-
# @description Core functions for any large Bash program
4+
# @description Core functions for any Bash program
55

66
# @description Initiates global variables used by other functions
77
# @noargs
@@ -17,21 +17,6 @@ core.init() {
1717
declare -ag ___global_shopt_stack___=()
1818
}
1919

20-
# @description Get version of the package, from the point of the callsite. In other words, it
21-
# returns the version of the package that has the file containing the direct caller of this
22-
# @set REPLY The full path to the directory
23-
# @internal
24-
core.get_package_dir() {
25-
# local start_dir="${1:-"${BASH_SOURCE[1]}"}"
26-
27-
# while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
28-
# if ! cd ..; then
29-
# return 1
30-
# fi
31-
# done
32-
:
33-
}
34-
3520
# @description Adds a handler for a particular `trap` signal or event. Noticably,
3621
# unlike the 'builtin' trap, this does not override any other existing handlers
3722
# @arg $1 string Function to execute on an event. Integers are forbiden
@@ -214,6 +199,68 @@ core.shopt_pop() {
214199
___global_shopt_stack___=("${___global_shopt_stack___[@]::${#___global_shopt_stack___[@]}-2}")
215200
}
216201

202+
# @description Sets an error.
203+
# @arg $1 Error code
204+
# @arg $2 Error message
205+
# @set number ERRCODE Error code
206+
# @set string ERR Error message
207+
core.err_set() {
208+
if (($# == 1)); then
209+
ERRCODE=1
210+
ERR=$1
211+
elif (($# == 2)); then
212+
ERRCODE=$1
213+
ERR=$2
214+
else
215+
printf '%s\n' "Error: bash-error: Incorrect function arguments"
216+
return 1
217+
fi
218+
219+
if [ -z "$ERR" ]; then
220+
printf '%s\n' "Error: bash-error: Argument for 'ERR' cannot be empty"
221+
return 1
222+
fi
223+
}
224+
225+
# @description Clears any of the global error state (sets to empty string).
226+
# This means any `core.err_exists` calls after this _will_ return `true`
227+
# @noargs
228+
# @set number ERRCODE Error code
229+
# @set string ERR Error message
230+
core.err_clear() {
231+
ERRCODE=
232+
ERR=
233+
}
234+
235+
# @description Checks if an error exists. If `ERR` is not empty, then an error
236+
# _does_ exist
237+
# @noargs
238+
core.err_exists() {
239+
if [ -z "$ERR" ]; then
240+
return 1
241+
else
242+
return 0
243+
fi
244+
}
245+
246+
# @description Prints stacktrace
247+
# @noargs
248+
# @example
249+
# core.trap_add 'err_handler' EXIT
250+
# err_handler() {
251+
# local exit_code=$?
252+
# core.stacktrace_print
253+
# exit $?
254+
# }
255+
core.stacktrace_print() {
256+
printf '%s\n' 'Stacktrace:'
257+
258+
local i=
259+
for ((i=0; i<${#FUNCNAME[@]}-1; ++i)); do
260+
printf '%s\n' " in ${FUNCNAME[$i]} (${BASH_SOURCE[$i]}:${BASH_LINENO[$i-1]})"
261+
done; unset -v i
262+
} >&2
263+
217264
# @description Determine if color should be printed
218265
# @internal
219266
core.should_color_output() {
@@ -236,3 +283,18 @@ core.should_color_output() {
236283
return 0
237284
fi
238285
}
286+
287+
# @description Get version of the package, from the point of the callsite. In other words, it
288+
# returns the version of the package that has the file containing the direct caller of this
289+
# @set REPLY The full path to the directory
290+
# @internal
291+
core.get_package_dir() {
292+
# local start_dir="${1:-"${BASH_SOURCE[1]}"}"
293+
294+
# while [ ! -f 'basalt.toml' ] && [ "$PWD" != / ]; do
295+
# if ! cd ..; then
296+
# return 1
297+
# fi
298+
# done
299+
:
300+
}

pkg/lib/public/error.sh

Lines changed: 0 additions & 45 deletions
This file was deleted.

pkg/lib/public/stacktrace.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)