Skip to content

Commit

Permalink
clean up script
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Jun 14, 2024
1 parent c82a604 commit b1b2730
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 112 deletions.
208 changes: 108 additions & 100 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ INSTALL_SCRIPT_URL="https://dotenvx.sh/install.sh"
# | |
# |_________________________________________________________________________________________________|

# usage
# usage ---------------------------------
usage() {
echo "Usage: $0 [options] [command]"
echo ""
Expand All @@ -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
Expand All @@ -120,26 +85,14 @@ 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

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)"

Expand Down Expand Up @@ -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
Expand All @@ -250,6 +245,7 @@ which_dotenvx() {
return 0
}

# warnings* -----------------------------
warn_of_any_conflict() {
local dotenvx_path="$(which_dotenvx)"

Expand All @@ -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}"
Expand All @@ -313,7 +320,7 @@ install_dotenvx() {
return 0
}

main() {
run() {
# parse arguments
for arg in "$@"; do
case $arg in
Expand All @@ -336,6 +343,7 @@ main() {
esac
done

# machine checks
is_directory_writable
is_curl_installed
is_os_supported
Expand Down Expand Up @@ -367,6 +375,6 @@ if ! is_test_mode; then
echo "piping script"
fi

main "$@"
run "$@"
exit $?
fi
Loading

0 comments on commit b1b2730

Please sign in to comment.