diff --git a/install.sh b/install.sh index e193d01..46ad338 100755 --- a/install.sh +++ b/install.sh @@ -52,7 +52,7 @@ INSTALL_SCRIPT_URL="https://dotenvx.sh/install.sh" # | | # |_________________________________________________________________________________________________| -# usage +# usage --------------------------------- usage() { echo "Usage: $0 [options] [command]" echo "" @@ -67,47 +67,12 @@ usage() { echo " help display help" } -directory() { - local dir=$DIRECTORY - - case "$dir" in - ~*/*) - dir="$HOME/${dir#\~/}" - ;; - ~*) - dir="$HOME/${dir#\~}" - ;; - esac - - echo "${dir}" - return 0 -} - -sudo_install_command() { - if is_piped; then - echo "curl -fsS $INSTALL_SCRIPT_URL | sudo $0" - else - echo "sudo $0" - fi - - return 0 -} - -customize_directory_command() { - if is_piped; then - echo "curl -fsS $INSTALL_SCRIPT_URL | $0 -s -- --directory=." - else - echo "$0 --directory=." - fi - - return 0 -} - +# machine checks ------------------------ is_directory_writable() { # check installation directory is writable if [ ! -w "$(directory)" ]; then echo "[INSTALLATION_FAILED] the installation directory [$(directory)] is not writable by the current user" - echo "? run as root [$(sudo_install_command "$0")] or choose a writable directory like your current directory [$(customize_directory_command "$0")]" + echo "? run as root [$(help_sudo_install_command "$0")] or choose a writable directory like your current directory [$(help_customize_directory_command "$0")]" return 1 fi @@ -120,7 +85,7 @@ is_curl_installed() { if [ -z "$curl_path" ]; then echo "[INSTALLATION_FAILED] curl is required and is not installed" - echo "? install curl [$(install_curl_command)] and try again" + echo "? install curl [$(help_install_curl_command)] and try again" return 1 fi @@ -128,18 +93,6 @@ is_curl_installed() { return 0 } -os() { - echo "$(uname -s | tr '[:upper:]' '[:lower:]')" - - return 0 -} - -arch() { - echo "$(uname -m | tr '[:upper:]' '[:lower:]')" - - return 0 -} - is_os_supported() { local os="$(os)" @@ -176,62 +129,104 @@ is_arch_supported() { return 0 } -os_arch() { - echo "$(os)-$(arch)" +# is_* checks --------------------------- +is_piped() { + [ "$0" = "sh" ] || [ "$0" = "bash" ] +} + +is_ci() { + [ -n "$CI" ] && [ $CI != 0 ] +} + +is_test_mode() { + [ -n "$TEST_MODE" ] && [ $TEST_MODE != 0 ] +} + +is_installed() { + local flagged_version="$1" + local current_version=$("$(directory)/dotenvx" --version 2>/dev/null || echo "0") + + # if --version flag passed + if [ -n "$flagged_version" ]; then + if [ "$current_version" = "$flagged_version" ]; then + # return true since version already installed + return 0 + else + # return false since version not installed + return 1 + fi + fi + + # if no version flag passed + if [ "$current_version" != "$VERSION" ]; then + # return false since latest is not installed + return 1 + fi + echo "[dotenvx@$current_version] already installed ($(directory)/dotenvx)" + + # return true since version already installed return 0 } -filename() { - echo "dotenvx-$VERSION-$(os_arch).tar.gz" +# helpers ------------------------------- +directory() { + local dir=$DIRECTORY + + case "$dir" in + ~*/*) + dir="$HOME/${dir#\~/}" + ;; + ~*) + dir="$HOME/${dir#\~}" + ;; + esac + echo "${dir}" return 0 } -download_url() { - echo "$RELEASES_URL/download/v$VERSION/$(filename)" +os() { + echo "$(uname -s | tr '[:upper:]' '[:lower:]')" return 0 } -is_piped() { - [ "$0" = "sh" ] || [ "$0" = "bash" ] +arch() { + echo "$(uname -m | tr '[:upper:]' '[:lower:]')" + + return 0 } -is_ci() { - [ -n "$CI" ] && [ $CI != 0 ] +os_arch() { + echo "$(os)-$(arch)" + + return 0 } -is_test_mode() { - [ -n "$TEST_MODE" ] && [ $TEST_MODE != 0 ] +filename() { + echo "dotenvx-$VERSION-$(os_arch).tar.gz" + + return 0 } -progress_bar() { - if $(is_ci); then - echo "--no-progress-meter" - else - echo "--progress-bar" - fi +download_url() { + echo "$RELEASES_URL/download/v$VERSION/$(filename)" return 0 } -install_curl_command() { - if command -v apt-get >/dev/null 2>&1; then - echo "sudo apt-get update && sudo apt-get install -y curl" - elif command -v yum >/dev/null 2>&1; then - echo "sudo yum install -y curl" - elif command -v brew >/dev/null 2>&1; then - echo "brew install curl" - elif command -v pkg >/dev/null 2>&1; then - echo "sudo pkg install curl" +progress_bar() { + if $(is_ci); then + echo "--no-progress-meter" else - echo "install curl manually" + echo "--progress-bar" fi return 0 } +# which_* ------------------------------- which_curl() { local result result=$(command -v curl 2>/dev/null) # capture the output without displaying it on the screen @@ -250,6 +245,7 @@ which_dotenvx() { return 0 } +# warnings* ----------------------------- warn_of_any_conflict() { local dotenvx_path="$(which_dotenvx)" @@ -261,33 +257,44 @@ warn_of_any_conflict() { return 0 } -is_installed() { - local flagged_version="$1" - local current_version=$("$(directory)/dotenvx" --version 2>/dev/null || echo "0") - - # if --version flag passed - if [ -n "$flagged_version" ]; then - if [ "$current_version" = "$flagged_version" ]; then - # return true since version already installed - return 0 - else - # return false since version not installed - return 1 - fi +# help text ----------------------------- +help_sudo_install_command() { + if is_piped; then + echo "curl -fsS $INSTALL_SCRIPT_URL | sudo $0" + else + echo "sudo $0" fi - # if no version flag passed - if [ "$current_version" != "$VERSION" ]; then - # return false since latest is not installed - return 1 + return 0 +} + +help_customize_directory_command() { + if is_piped; then + echo "curl -fsS $INSTALL_SCRIPT_URL | $0 -s -- --directory=." + else + echo "$0 --directory=." fi - echo "[dotenvx@$current_version] already installed ($(directory)/dotenvx)" + return 0 +} + +help_install_curl_command() { + if command -v apt-get >/dev/null 2>&1; then + echo "sudo apt-get update && sudo apt-get install -y curl" + elif command -v yum >/dev/null 2>&1; then + echo "sudo yum install -y curl" + elif command -v brew >/dev/null 2>&1; then + echo "brew install curl" + elif command -v pkg >/dev/null 2>&1; then + echo "sudo pkg install curl" + else + echo "install curl manually" + fi - # return true since version already installed return 0 } +# install/run --------------------------- install_dotenvx() { # 0. override version VERSION="${1:-$VERSION}" @@ -313,7 +320,7 @@ install_dotenvx() { return 0 } -main() { +run() { # parse arguments for arg in "$@"; do case $arg in @@ -336,6 +343,7 @@ main() { esac done + # machine checks is_directory_writable is_curl_installed is_os_supported @@ -367,6 +375,6 @@ if ! is_test_mode; then echo "piping script" fi - main "$@" + run "$@" exit $? fi diff --git a/spec/install_spec.sh b/spec/install_spec.sh index f205259..dca1360 100644 --- a/spec/install_spec.sh +++ b/spec/install_spec.sh @@ -1,5 +1,5 @@ export CI=1 # because we are running this on github actions and makes testing output easier -export TEST_MODE=1 # set to test mode in order to not run main() for test purposes +export TEST_MODE=1 # set to test mode in order to not run run() for test purposes Describe 'install.sh' Include install.sh @@ -69,9 +69,9 @@ Describe 'install.sh' End End - Describe 'sudo_install_command' + Describe 'help_sudo_install_command' It 'returns sudo in front of command' - When call sudo_install_command + When call help_sudo_install_command The status should equal 0 The output should equal "sudo $0" End @@ -82,16 +82,16 @@ Describe 'install.sh' } It "returns with curl example" - When call sudo_install_command + When call help_sudo_install_command The status should equal 0 The output should equal "curl -fsS https://dotenvx.sh/install.sh | sudo $0" End End End - Describe 'customize_directory_command' + Describe 'help_customize_directory_command' It 'returns with --directory flag' - When call customize_directory_command + When call help_customize_directory_command The status should equal 0 The output should equal "$0 --directory=." End @@ -102,7 +102,7 @@ Describe 'install.sh' } It "returns with curl example" - When call customize_directory_command + When call help_customize_directory_command The status should equal 0 The output should equal "curl -fsS https://dotenvx.sh/install.sh | $0 -s -- --directory=." End @@ -224,7 +224,7 @@ Commands: When call is_curl_installed The status should equal 1 The output should equal "[INSTALLATION_FAILED] curl is required and is not installed -? install curl [$(install_curl_command)] and try again" +? install curl [$(help_install_curl_command)] and try again" End End End @@ -378,13 +378,13 @@ Commands: End End - Describe 'main()' + Describe 'run()' which_dotenvx() { mock_which_dotenvx_empty } It 'installs dotenvx' - When call main + When call run The status should equal 0 The output should equal "[dotenvx@0.44.1] installed successfully (./spec/tmp/dotenvx)" End @@ -395,7 +395,7 @@ Commands: } It 'installs it but warns' - When call main + When call run The status should equal 0 The output should equal "[dotenvx@0.44.1] installed successfully (./spec/tmp/dotenvx)" The stderr should equal "[DOTENVX_CONFLICT] conflicting dotenvx found at /different/path @@ -407,7 +407,7 @@ Commands: Before 'preinstall_dotenvx' It 'says already installed' - When call main + When call run The status should equal 0 The output should equal "[dotenvx@0.44.1] already installed (./spec/tmp/dotenvx)" End