Based on the idea of b-log. Which is based on the idea of log.sh.
Call this dependency from another bash script to have a decent logger with minimal boiler plate code in the other repo.
Instructions on how to use the logger can be found here. The instructions below explain how to include this dependency in other repos.
- In your other repo, include a file named:
.gitmodules
that includes:
[submodule "dependencies/bash-log"]
path = dependencies/bash-log
url = https://github.com/hiveminds/bash-log
- Create a file named
install-dependencies.sh
with content:
# Remove the submodules if they were still in the repo.
git rm --cached dependencies/bash-log/bash-log
# Remove and create a directory for the dependencies.
rm -r "$SCRIPT_PATH/dependencies"
mkdir -p "$SCRIPT_PATH/dependencies"
# (Re) add the BATS submodules to this repository.
git submodule add --force https://github.com/HiveMinds/bash-log dependencies/bash-log
# Update all submodules
git submodule update --remote --recursive
- Install the submodule with:
chmod +x install-dependencies.sh
./install-dependencies.sh
After including this dependency you can use the functions in this module like:
#!/bin/bash
# Load the installer dependency.
source "$REPO_ROOT_PATH/"/bash-log/src/main.sh
# set log level to all, otherwise, NOTICE, INFO, DEBUG, TRACE won't be logged.
LOG_LEVEL_ALL
# You can make it log to file using:
B_LOG --file log/multiple-outputs.txt --file-prefix-enable --file-suffix-enable
# You can silence the console logging with (by default it is true):
#B_LOG --stdout false # disable logging over stdout
FATAL "fatal level"
ERROR "error level"
WARN "warning level"
NOTICE "notice level"
INFO "info level"
DEBUG "debug level"
TRACE "trace level"
That outputs:
and creates a file with that log at: log/multiple-outputs.txt
Put your unit test files (with extension .bats) in folder: /test/
(Re)-install the required submodules with:
chmod +x install-dependencies.sh
./install-dependencies.sh
Install:
sudo gem install bats
sudo apt install bats -y
sudo gem install bashcov
sudo apt install shfmt -y
pre-commit install
pre-commit autoupdate
Run pre-commit with:
pre-commit run --all
Run the tests with:
bats test
If you want to run particular tests, you could use the test.sh
file:
chmod +x test.sh
./test.sh
bashcov bats test
- Include bash code coverage in GitLab CI.
- Add additional (relevant) pre-commit hooks.
- Develop Bash documentation checks here, and add them to this pre-commit.