Last active
May 19, 2023 02:53
-
-
Save holly/7ed983b00af2c6c8df8a8c49449bbc35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
set -u | |
set -o pipefail | |
set -C | |
WORK_DIR=$HOME/.cache | |
DOWNLOAD_URL=https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip | |
ZIP_FILE=awscliv2.zip | |
INSTALL_DIR=/usr/local/aws-cli | |
_is_root() { | |
if [[ $(id -u) -eq 0 ]]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
_execute() { | |
local BASH=$(which bash) | |
if ! _is_root; then | |
BASH="sudo $BASH" | |
fi | |
$BASH -c "$@" | |
} | |
awscli_install() { | |
if [[ -d $INSTALL_DIR ]]; then | |
echo "$INSTALL_DIR is already exists." | |
exit 1 | |
fi | |
curl -s $DOWNLOAD_URL -o $ZIP_FILE | |
unzip -o $ZIP_FILE | |
_execute "./aws/install" | |
} | |
awscli_forceinstall() { | |
awscli_uninstall | |
echo "re:install awscliv2" | |
awscli_install | |
echo "" | |
echo "awscli force install." | |
} | |
awscli_uninstall() { | |
if [[ ! -d $INSTALL_DIR ]]; then | |
echo "$INSTALL_DIR is not exists." | |
exit 1 | |
fi | |
_execute "rm -frv $INSTALL_DIR /usr/local/bin/aws* ./aws" | |
echo "" | |
echo "awscli uninstall." | |
} | |
awscli_update() { | |
if [[ ! -d $INSTALL_DIR ]]; then | |
echo "$INSTALL_DIR is not exists." | |
exit 1 | |
fi | |
if [[ ! -d "./aws" ]]; then | |
echo "aws extract directory is not exists." | |
exit 1 | |
fi | |
_execute "./aws/install --update" | |
} | |
usage() { | |
echo "Usage: $0 [install|update|forceinstall|uninstall]" | |
exit 1 | |
} | |
if [ $# -ne 1 ]; then | |
usage | |
fi | |
COMMAND=$1 | |
if [[ ! -d $WORK_DIR ]]; then | |
mkdir $WORK_DIR | |
fi | |
cd $WORK_DIR | |
case "$COMMAND" in | |
"install" ) awscli_install ;; | |
"forceinstall" ) awscli_forceinstall ;; | |
"uninstall" ) awscli_uninstall ;; | |
"update" ) awscli_update ;; | |
* ) usage;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment