Skip to content

Commit

Permalink
Merge pull request #5 from dotenvx/download-url
Browse files Browse the repository at this point in the history
display download_url
  • Loading branch information
motdotla authored Jun 13, 2024
2 parents e199553 + 1f66b11 commit 6681de2
Show file tree
Hide file tree
Showing 2 changed files with 378 additions and 32 deletions.
213 changes: 183 additions & 30 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ directory() {

is_directory_writable() {
# check installation directory is writable
if [ ! -w "$(directory)" ] && [ "$(id -u)" -ne 0 ]; then
if [ ! -w "$(directory)" ]; then
echo "[INSTALLATION_FAILED] the installation directory [$(directory)] is not writable by the current user"
echo "? run as root [sudo $0] or choose a writable directory like your current directory [$0 directory=.]"
echo "? run as root [sudo $0] or choose a writable directory like your current directory [$0 --directory=.]"

return 1
fi
Expand All @@ -49,11 +49,13 @@ is_directory_writable() {
}

is_curl_installed() {
if ! command -v curl >/dev/null 2>&1; then
echo "[INSTALLATION_FAILED] curl is required and appears to not be installed"
echo "? install curl and try again"
local curl_path="$(which_curl)"

exit 1
if [ -z "$curl_path" ]; then
echo "[INSTALLATION_FAILED] curl is required and is not installed"
echo "? install curl [$(install_curl_command)] and try again"

return 1
fi

return 0
Expand Down Expand Up @@ -113,29 +115,180 @@ os_arch() {
return 0
}

# parse arguments
for arg in "$@"; do
case $arg in
directory=* | --directory=*)
DIRECTORY="${arg#*=}"
;;
help | --help)
usage
exit 0
;;
*)
# Unknown option
echo "Unknown option: $arg"
usage
exit 1
;;
esac
done
filename() {
echo "dotenvx-$VERSION-$(os_arch).tar.gz"

return 0
}

download_url() {
echo "https://github.com/dotenvx/dotenvx/releases/download/v$VERSION/$(filename)"

return 0
}

is_ci() {
[ -n "$CI" ] && [ $CI != 0 ]
}

progress_bar() {
if $(is_ci); then
echo "--no-progress-meter"
else
echo "--progress-bar"
fi

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"
else
echo "install curl manually"
fi

return 0
}

which_curl() {
local result
result=$(command -v curl 2>/dev/null) # capture the output without displaying it on the screen

echo "$result"

return 0
}

which_dotenvx() {
local result
result=$(command -v dotenvx 2>/dev/null) # capture the output without displaying it on the screen

echo "$result"

return 0
}

warn_of_any_conflict() {
local dotenvx_path="$(which_dotenvx)"

if [ "$dotenvx_path" != "" ] && [ "$dotenvx_path" != "$(directory)/dotenvx" ]; then
echo "[DOTENVX_CONFLICT] conflicting dotenvx found at $dotenvx_path" >&2
echo "? we recommend updating your path to include $(directory)" >&2
fi

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
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
}

install_dotenvx() {
# 0. override version
VERSION="${1:-$VERSION}"

# 1. setup tmpdir
local tmpdir=$(command mktemp -d)

# 2. download
curl $(progress_bar) --fail -L --proto '=https' -o "$tmpdir/$(filename)" "$(download_url)"

# 3. decompress to install directory
tar xz -C $(directory) -f "$tmpdir/$(filename)"

is_directory_writable
is_curl_installed
is_os_supported
is_arch_supported
# 4. clean up
rm -r "$tmpdir"

# warn of any conflict
warn_of_any_conflict

# let user know
echo "[dotenvx@$VERSION] installed successfully ($(directory)/dotenvx)"

return 0
}

main() {
# parse arguments
for arg in "$@"; do
case $arg in
version=* | --version=*)
VERSION="${arg#*=}"
;;
directory=* | --directory=*)
DIRECTORY="${arg#*=}"
;;
help | --help)
usage
return 0
;;
*)
# Unknown option
echo "Unknown option: $arg"
usage
return 1
;;
esac
done

is_directory_writable
is_curl_installed
is_os_supported
is_arch_supported

# install logic
if [ -n "$VERSION" ]; then
# Check if the specified version is already installed
if is_installed "$VERSION"; then
echo "[dotenvx@$VERSION] already installed ($(directory)/dotenvx)"

return 0
else
install_dotenvx "$VERSION"
fi
else
if is_installed; then
echo "[dotenvx@$VERSION] already installed ($(directory)/dotenvx)"

return 0
else
install_dotenvx
fi
fi
}

# echo "os: $(os) arch: $(arch)"
echo "hello"
# execute main only if the script is run directly, not sourced
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
main "$@"
exit $?
fi
Loading

0 comments on commit 6681de2

Please sign in to comment.