From 3282754732766cec808e1e01ed3d08e1a4978835 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sat, 5 Oct 2024 23:46:50 +0200 Subject: [PATCH] feat: add slient setup option --- README.md | 2 +- bashdep | 28 +++++++++++++------ example/demo.sh | 2 +- tests/unit/bashdep_test.sh | 16 ++++++++++- ...est_bashdep_install_custom_setup.snapshot} | 0 ...est_bashdep_install_default_setup.snapshot | 3 ++ 6 files changed, 39 insertions(+), 12 deletions(-) rename tests/unit/snapshots/{bashdep_test_sh.test_bashdep_install.snapshot => bashdep_test_sh.test_bashdep_install_custom_setup.snapshot} (100%) create mode 100644 tests/unit/snapshots/bashdep_test_sh.test_bashdep_install_default_setup.snapshot diff --git a/README.md b/README.md index 3868170..9de4208 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ DEPENDENCIES=( ) source lib/bashdep -bashdep::setup dir="lib" dev-dir="src/dev" +bashdep::setup dir="lib" dev-dir="src/dev" silent=false bashdep::install "${DEPENDENCIES[@]}" ``` diff --git a/bashdep b/bashdep index b8b3e77..799db0b 100755 --- a/bashdep +++ b/bashdep @@ -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 @@ -67,10 +70,14 @@ 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" @@ -78,3 +85,6 @@ function bashdep::download_url() { fi } +function bashdep::is_silent() { + [ "$BASHDEP_SILENT" == true ] +} diff --git a/example/demo.sh b/example/demo.sh index 7fc2655..025de7a 100755 --- a/example/demo.sh +++ b/example/demo.sh @@ -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=false bashdep::install "${DEPENDENCIES[@]}" diff --git a/tests/unit/bashdep_test.sh b/tests/unit/bashdep_test.sh index 8a19d5e..1059128 100644 --- a/tests/unit/bashdep_test.sh +++ b/tests/unit/bashdep_test.sh @@ -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" diff --git a/tests/unit/snapshots/bashdep_test_sh.test_bashdep_install.snapshot b/tests/unit/snapshots/bashdep_test_sh.test_bashdep_install_custom_setup.snapshot similarity index 100% rename from tests/unit/snapshots/bashdep_test_sh.test_bashdep_install.snapshot rename to tests/unit/snapshots/bashdep_test_sh.test_bashdep_install_custom_setup.snapshot diff --git a/tests/unit/snapshots/bashdep_test_sh.test_bashdep_install_default_setup.snapshot b/tests/unit/snapshots/bashdep_test_sh.test_bashdep_install_default_setup.snapshot new file mode 100644 index 0000000..f661c6d --- /dev/null +++ b/tests/unit/snapshots/bashdep_test_sh.test_bashdep_install_default_setup.snapshot @@ -0,0 +1,3 @@ +mocked download_url +mocked download_url +mocked download_url