Skip to content

Instantly share code, notes, and snippets.

@imme-emosol
Last active June 9, 2022 22:58
Show Gist options
  • Save imme-emosol/8a13ee7f2a32529d0fb2410d38d3e600 to your computer and use it in GitHub Desktop.
Save imme-emosol/8a13ee7f2a32529d0fb2410d38d3e600 to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
(
download_git_clone_idempotent_to() {
# Download file to BIN_PATH
# default BIN_PATH to the first entry of the PATH environment variable that ends on 'local/bin'.
BIN_PATH="${1:-$(
BIN_PATH="${PATH%${PATH#*local/bin}}"
printf "${BIN_PATH##*:}/git-clone-idempotent"
)}"
test ! -e "${BIN_PATH?}" || { printf 'File already exists.\n' >&2 ; exit 1 ; }
# test "${PATH:-}" != "${PATH#*${BIN_PATH%/*}}" || { printf 'BIN_PATH is not in PATH.\n' >&2 ; exit 1 ; }
$(
command -v curl && printf ' --output' ||
{
command -v wget && printf ' --output-document' ||
printf 'eval >&2 printf '\''Could not download "%%s" from "%%s".\\n'\'
}
) "${BIN_PATH?}" https://raw.githubusercontent.com/imme-emosol/git-clone-idempotent/patch-1/index.sh
test -e "${BIN_PATH?}" || exit 1
# Look at what was downloaded before using it.
$( command -v sensible-editor || printf cat ) "${BIN_PATH?}"
printf 'Type y<Enter> to make the file executable.\n' >&2
read -r CONFIRM
# Make the file executable.
test "confirmed_${CONFIRM}" != "confirmed_y" || chmod u+x "${BIN_PATH?}"
}
printf 'Press <Enter> to download to default location.
Anything else will be the path to downloaded to.\n' >&2
read -r LOCATION
# Remove the "git-clone-idempotent" to download the file into PATH.
download_git_clone_idempotent_to "${LOCATION:-}"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment