Skip to content
/ sosh Public

Dependency resolver and bundler for shell scripts

License

Notifications You must be signed in to change notification settings

rynkowsg/sosh

Repository files navigation

sosh

CircleCI Build Status License

source .sh to the rescue!

Leverage source to fetch remote shell scripts and optionally bundle them into one file.

Status: ALPHA

Briefly

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/[email protected]/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.

Motivation

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.

Install

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.

Usage

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

What's there

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:

Examples:

License

Copyright © 2024 Greg Rynkowski

Released under the MIT license.

Footnotes

  1. But script paths need to consider they can be called not only by bats, but also by Bash (example).

  2. Path initialization can be complicated, especially when writing bits sourcing other bits.