Skip to content

Commit

Permalink
feat: add slient setup option
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Oct 5, 2024
1 parent c2abc86 commit 794a8c8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
28 changes: 19 additions & 9 deletions bashdep
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
BASHDEP_DIR="lib"
BASHDEP_DEV_DIR="lib/dev"
BASHDEP_DEV_SUFFIX="@dev"
BASHDEP_SILENT=false

# Configure destination directories.
# Configure destination directories and silent mode.
#
# Usage:
# bashdep::setup [dir=DIR] [dev-dir=DEV_DIR]
# bashdep::setup [dir=DIR] [dev-dir=DEV_DIR] [silent=true|false]
#
# Parameters:
# dir - Set the default destination directory.
# dev-dir - Set the development destination directory.
# dir - Set the default destination directory.
# dev-dir - Set the development destination directory.
# silent - If true, no progress text will be shown during installation.
#
# Example:
# bashdep::setup dir="vendor" dev-dir="src/dev"
# bashdep::setup dir="vendor" dev-dir="src/dev" silent=true
function bashdep::setup() {
for param in "$@"; do
case $param in
dir=*) BASHDEP_DIR="${param#*=}" ;;
dev-dir=*) BASHDEP_DEV_DIR="${param#*=}" ;;
dir=*) BASHDEP_DIR="${param#*=}" ;;
dev-dir=*) BASHDEP_DEV_DIR="${param#*=}" ;;
silent=*) BASHDEP_SILENT="${param#*=}" ;;
*)
echo "Error: Invalid parameter '$param'"
return 1
Expand Down Expand Up @@ -67,14 +70,21 @@ function bashdep::download_url() {
file_name=$(basename "$url")
local destination_file="$destination_dir/$file_name"

echo "Downloading '$file_name' to '$destination_dir'..."
if ! bashdep::is_silent; then
echo "Downloading '$file_name' to '$destination_dir'..."
fi

if curl -s -L "$url" -o "$destination_file"; then
echo "> $file_name installed successfully in '$destination_dir'"
if ! bashdep::is_silent; then
echo "> $file_name installed successfully in '$destination_dir'"
fi
chmod +x "$destination_file"
else
echo "Error: Failed to install $file_name from $url"
return 1
fi
}

function bashdep::is_silent() {
[ "$BASHDEP_SILENT" == true ]
}
2 changes: 1 addition & 1 deletion example/demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ DEPENDENCIES=(
"https://github.com/Chemaclass/bash-dumper/releases/download/0.1/dumper.sh@dev"
)

bashdep::setup dir="vendor" dev-dir="local/dev"
bashdep::setup dir="vendor" dev-dir="local/dev" #silent=true
bashdep::install "${DEPENDENCIES[@]}"
16 changes: 15 additions & 1 deletion tests/unit/bashdep_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@ function set_up() {
source "$(current_dir)/../../bashdep"
}

function test_bashdep_install() {
function test_bashdep_install_custom_setup() {
mock bashdep::setup_directory "/dev/null"
mock bashdep::download_url "echo mocked download_url"
bashdep::setup dir="vendor" dev-dir="src/dev" silent=true

local DEPENDENCIES=(
"https://github.com/TypedDevs/bashunit/releases/download/0.17.0/bashunit"
"https://github.com/Chemaclass/create-pr/releases/download/0.6/create-pr"
"https://github.com/Chemaclass/bash-dumper/releases/download/0.1/dumper.sh:lib/dev"
)

assert_match_snapshot "$(bashdep::install "${DEPENDENCIES[@]}")"
}

function test_bashdep_install_default_setup() {
mock bashdep::setup_directory "/dev/null"
mock bashdep::download_url "echo mocked download_url"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mocked download_url
mocked download_url
mocked download_url

0 comments on commit 794a8c8

Please sign in to comment.