source .sh to the rescue!
Leverage source
to fetch remote shell scripts and optionally bundle them into one file.
Status: ALPHA
sosh a shell tool that uses source
declaration in shell scripts to fetch remote scripts and/or bundle them into one file.
Example: let's assume you need some color definitions and functions to serialize arrays in a Bash script. You can take them adding following source
declaration:
source "${ROOT_DIR}/.github_deps/rynkowsg/shell-gr@v0.2.2/lib/color.bash"
source "${ROOT_DIR}/.https_deps/gist.githubusercontent.com/TekWizely/c0259f25e18f2368c4a577495cd566cd/raw/b9e87c74565fb90a39bb7a1033f950773201dbf7/serialize_array.bash"
If you call sosh fetch
on the script, sosh will get these files for you.
If you like to bundle the script into one file, you can use sosh pack
and sosh will save the bundled file to expected location.
That's that simple.
So, it all started when I was trying to share code between CircleCI orbs and their commands. The thing is, orbs only let you use one bash script per command. That's a bummer because if your commands share similarities or worse, they are kind of the same just with different defaults, you end up copying and pasting the same stuff over and over.
After a couple of days, it hit me that it'd be super handy to share not just bits and pieces between orb commands, but also with all the other bash scripts I had lying around. I'm talking about the stuff we use all the time, like logging, handling errors, and other goodies.
curl -s https://raw.githubusercontent.com/rynkowsg/sosh/v0.2.0/main/src/pl/rynkowski/sosh.cljc -o ~/.bin/sosh
chmod +x ~/.bin/sosh
The line above installs the script in ~/.bin
. That installation directory needs to be added to PATH
.
Warning
The tool requires Babashka to work. If you like to see this a standalone binary rise your voice HERE.
FETCH
sosh fetch ./test/res/test_suite/4_import_remote/entry.bash
PACK
# pack the script on input to the path on output
sosh pack -i ./test/res/test_suite/3_import_with_variables/entry.bash -o ./bundled.bash
# optionally you can set current working directory (useful if the entry script doesn't use absolute path for sourced files)
sosh pack -i ./entry.bash -o ./bundled.bash
Features
- recursive dependency resolution (your script requires A, A requires B, B requires C)
- command to bundle script and sourced deps, either local or remote, into one file
- option to ignore
source
line if contains# sosh: skip
at the end - support for BATS files1
Some aspects are better explained in CHANGELOG notes to version 0.1.0.
Examples
I use this tools in my own repos:
- rynkowsg/asdf-orb - CircleCI orb providing asdf ()
- rynkowsg/checkout-orb - CircleCI orb providing advanced checkout
- rynkowsg/shell-gr- my library of bash snippets (lib examples)
Examples:
- source remote scripts from github:
- install_asdf.bash - script behind rynkowsg/asdf-orb
- clone_git_repo.bash - script behind rynkowsg/checkout-orb
- source remote scripts by URL:
- bats_assert.bash - wrapper for bats_assert to fetch all necessary files at once
- path initialization in a remote script that requires yet another scripts2:
- error.bash
- most of the files in shell-gr repository
Copyright © 2024 Greg Rynkowski
Released under the MIT license.