Skip to content
Snippets Groups Projects

Adds creation of $HOME/.local/bin directory if not existing

Closed orestis.malaspin requested to merge create_local_bin into main
1 file
+ 143
137
Compare changes
  • Side-by-side
  • Inline
+ 143
137
@@ -3,193 +3,199 @@
@@ -3,193 +3,199 @@
#
#
# Distributed under terms of the GPLv3 license.
# Distributed under terms of the GPLv3 license.
{ \unalias command; \unset -f command; } >/dev/null 2>&1
{
 
\unalias command
 
\unset -f command
 
} >/dev/null 2>&1
tdir=''
tdir=''
gitedu_release_url="https://gitedu.hesge.ch/dojo_project/projects/ui/dojocli/-/releases/Latest"
gitedu_release_url="https://gitedu.hesge.ch/dojo_project/projects/ui/dojocli/-/releases/Latest"
gitedu_prealpha_url="https://gitedu.hesge.ch/dojo_project/projects/ui/dojocli/-/releases/Latest"
gitedu_prealpha_url="https://gitedu.hesge.ch/dojo_project/projects/ui/dojocli/-/releases/Latest"
gitedu_url=$gitedu_release_url
gitedu_url=$gitedu_release_url
cleanup_success() {
cleanup_success() {
printf "The dojo is installed in %s\nMake sure %s is in your path\n" "$dest" "$dest"
printf "The dojo is installed in %s\nMake sure %s is in your path\n" "$dest" "$destdir"
cleanup
printf "If you are using bash you may want to append:\n"
 
printf "export PATH=\"\$PATH:%s\" to your .bashrc file.\n" "$destdir"
 
cleanup
}
}
cleanup() {
cleanup() {
[ -n "$tdir" ] && {
[ -n "$tdir" ] && {
printf "Cleaning temporary files in %s\n" "$tdir"
printf "Cleaning temporary files in %s\n" "$tdir"
command rm -rf "$tdir"
command rm -rf "$tdir"
tdir=''
tdir=''
}
}
}
}
die() {
die() {
cleanup
cleanup
printf "\033[31m%s\033[m\n\r" "$*" > /dev/stderr;
printf "\033[31m%s\033[m\n\r" "$*" >/dev/stderr
exit 1;
exit 1
}
}
detect_jq_tool() {
detect_jq_tool() {
if command -v jq 2> /dev/null > /dev/null; then
if command -v jq 2>/dev/null >/dev/null; then
get_version() {
get_version() {
command jq -r '."assets"."links"[0].url' | command awk -F '/' '{print $(NF-1)}'
command jq -r '."assets"."links"[0].url' | command awk -F '/' '{print $(NF-1)}'
}
}
else
else
die "jq not available on your system cannot get dojo version"
die "jq not available on your system cannot get dojo version"
fi
fi
}
}
detect_network_tool() {
detect_network_tool() {
if command -v curl 2> /dev/null > /dev/null; then
if command -v curl 2>/dev/null >/dev/null; then
fetch() {
fetch() {
command curl -fL "$1"
command curl -fL "$1"
}
}
fetch_and_get_version() {
fetch_and_get_version() {
command curl -fL "$1" | get_version
command curl -fL "$1" | get_version
}
}
fetch_quiet() {
fetch_quiet() {
command curl -fsSL "$1"
command curl -fsSL "$1"
}
}
fetch_and_get_version_quiet() {
fetch_and_get_version_quiet() {
command curl -fsSL "$1" | get_version
command curl -fsSL "$1" | get_version
}
}
elif command -v wget 2> /dev/null > /dev/null; then
elif command -v wget 2>/dev/null >/dev/null; then
fetch() {
fetch() {
command wget -O- "$1"
command wget -O- "$1"
}
}
fetch_and_get_version() {
fetch_and_get_version() {
command wget -O- "$1" | get_version
command wget -O- "$1" | get_version
}
}
fetch_quiet() {
fetch_quiet() {
command wget --quiet -O- "$1"
command wget --quiet -O- "$1"
}
}
fetch_and_get_version_quiet() {
fetch_and_get_version_quiet() {
command wget --quiet -O- "$1" | get_version
command wget --quiet -O- "$1" | get_version
}
}
else
else
die "Neither curl nor wget available. Cannot download dojo. Install any of the or see $gitedu_url to download manually"
die "Neither curl nor wget available. Cannot download dojo. Install any of the or see $gitedu_url to download manually"
fi
fi
}
}
detect_os() {
detect_os() {
arch=""
arch=""
case "$(command uname)" in
case "$(command uname)" in
'Darwin') OS="macos"
'Darwin')
case "$(command uname -m)" in
OS="macos"
amd64|x86_64) arch="x64";;
case "$(command uname -m)" in
aarch64*) arch="arm64";;
amd64 | x86_64) arch="x64" ;;
armv8*) arch="arm64";;
aarch64*) arch="arm64" ;;
arm64) arch="arm64";;
armv8*) arch="arm64" ;;
*) die "dojo binaries are not available for $(command uname -m) architecture. See $gitedu_url to download manually";;
arm64) arch="arm64" ;;
esac
*) die "dojo binaries are not available for $(command uname -m) architecture. See $gitedu_url to download manually" ;;
;;
esac
'Linux')
;;
OS="linux"
'Linux')
case "$(command uname -m)" in
OS="linux"
amd64|x86_64) arch="x64";;
case "$(command uname -m)" in
aarch64*) arch="arm64";;
amd64 | x86_64) arch="x64" ;;
armv8*) arch="arm64";;
aarch64*) arch="arm64" ;;
arm64) arch="arm64";;
armv8*) arch="arm64" ;;
*) die "dojo binaries are not available for $(command uname -m) architecture. See $gitedu_url to download manually";;
arm64) arch="arm64" ;;
esac
*) die "dojo binaries are not available for $(command uname -m) architecture. See $gitedu_url to download manually" ;;
;;
esac
*) die "dojo binaries are not available for $(command uname). See $gitedu_url to download manually"
;;
esac
*) die "dojo binaries are not available for $(command uname). See $gitedu_url to download manually" ;;
 
esac
}
}
expand_tilde() {
expand_tilde() {
tilde_less="${1#\~/}"
tilde_less="${1#\~/}"
[ "$1" != "$tilde_less" ] && tilde_less="$HOME/$tilde_less"
[ "$1" != "$tilde_less" ] && tilde_less="$HOME/$tilde_less"
printf '%s' "$tilde_less"
printf '%s' "$tilde_less"
}
}
parse_args() {
parse_args() {
dest='~/.local/bin'
destdir='~/.local/bin'
[ "$OS" = "macos" ] && dest="/usr/local/bin"
[ "$OS" = "macos" ] && destdir="/usr/local/bin"
installer=''
installer=''
while :; do
while :; do
case "$1" in
case "$1" in
dest=*) dest="${1#*=}";;
destdir=*) destdir="${1#*=}" ;;
installer=*) installer="${1#*=}";;
installer=*) installer="${1#*=}" ;;
"") break;;
"") break ;;
*) die "Unrecognized command line option: $1";;
*) die "Unrecognized command line option: $1" ;;
esac
esac
shift
shift
done
done
dest=$(expand_tilde "${dest}")
destdir=$(expand_tilde "${destdir}")
dest="$dest/dojo"
if [ ! -d "$destdir" ]; then
 
printf "Directory %s does not exist. Creatring %s now.\n" "$destdir" "$destdir"
 
mkdir -p "$destdir"
 
fi
 
dest="$destdir/dojo"
}
}
get_file_url() {
get_file_url() {
url="https://gitedu.hesge.ch/api/v4/projects/12972/packages/generic/dojo"
url="https://gitedu.hesge.ch/api/v4/projects/12972/packages/generic/dojo"
if [ "$OS" = "macos" ]; then
if [ "$OS" = "macos" ]; then
url="$url$1_macOS-$arch/$1/dojo$1"
url="$url$1_macOS-$arch/$1/dojo$1"
else
else
url="$url$1_Linux-$arch/$1/dojo$1"
url="$url$1_Linux-$arch/$1/dojo$1"
fi
fi
}
}
get_release_url() {
get_release_url() {
release_version=$(fetch_and_get_version_quiet https://gitedu.hesge.ch/api/v4/projects/12972/releases/Latest)
release_version=$(fetch_and_get_version_quiet https://gitedu.hesge.ch/api/v4/projects/12972/releases/Latest)
[ $? -ne 0 -o -z "$release_version" ] && die "Could not get dojo latest release version"
[ $? -ne 0 -o -z "$release_version" ] && die "Could not get dojo latest release version"
get_file_url "$release_version"
get_file_url "$release_version"
}
}
get_prealpha_url() {
get_prealpha_url() {
gitedu_url=$gitedu_prealpha_url
gitedu_url=$gitedu_prealpha_url
prealpha_version=$(fetch_and_get_version_quiet https://gitedu.hesge.ch/api/v4/projects/12972/releases/Pre-alpha)
prealpha_version=$(fetch_and_get_version_quiet https://gitedu.hesge.ch/api/v4/projects/12972/releases/Pre-alpha)
[ $? -ne 0 -o -z "$prealpha_version" ] && die "Could not get dojo pre-alpha release version"
[ $? -ne 0 -o -z "$prealpha_version" ] && die "Could not get dojo pre-alpha release version"
get_file_url "$prealpha_version"
get_file_url "$prealpha_version"
}
}
get_download_url() {
get_download_url() {
case "$installer" in
case "$installer" in
"pre-alpha") get_prealpha_url ;;
"pre-alpha") get_prealpha_url ;;
"") get_release_url ;;
"") get_release_url ;;
*) ;;
*) ;;
esac
esac
}
}
download_installer() {
download_installer() {
tdir=$(command mktemp -d "/tmp/dojo-install-XXXXXXXXXXXX")
tdir=$(command mktemp -d "/tmp/dojo-install-XXXXXXXXXXXX")
printf '%s\n' "Downloading from: $url"
printf '%s\n' "Downloading from: $url"
if [ "$OS" = "macos" ]; then
if [ "$OS" = "macos" ]; then
installer="$tdir/dojo"
installer="$tdir/dojo"
else
else
installer="$tdir/dojo"
installer="$tdir/dojo"
fi
fi
fetch "$url" > "$installer" || die "Failed to download: $url"
fetch "$url" >"$installer" || die "Failed to download: $url"
printf '%s\n\n' "File saved in: $installer"
printf '%s\n\n' "File saved in: $installer"
}
}
linux_install() {
linux_install() {
command chmod +x "$installer" || die "Failed to chmod +x $installer"
command chmod +x "$installer" || die "Failed to chmod +x $installer"
command mv "$installer" "$dest" || die "Failed to move $installer"
command mv "$installer" "$dest" || die "Failed to move $installer"
}
}
macos_install() {
macos_install() {
command chmod +x "$installer" || die "Failed to chmod +x $installer for MacOS"
command chmod +x "$installer" || die "Failed to chmod +x $installer for MacOS"
command mv "$installer" "$dest" || die "Failed to move $installer to $dest for MacOS"
command mv "$installer" "$dest" || die "Failed to move $installer to $dest for MacOS"
}
}
main() {
main() {
detect_os
detect_os
parse_args "$@"
parse_args "$@"
detect_jq_tool
detect_jq_tool
detect_network_tool
detect_network_tool
get_download_url
get_download_url
download_installer
download_installer
if [ "$OS" = "macos" ]; then
if [ "$OS" = "macos" ]; then
macos_install
macos_install
else
else
linux_install
linux_install
fi
fi
cleanup_success
cleanup_success
exit 0
exit 0
}
}
main "$@"
main "$@"
Loading