-
-
Save pcolunga/3711443ea31b58a23de2efd61cdedba4 to your computer and use it in GitHub Desktop.
AMF CLI installer
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 | |
# Copyright 2018 MuleSoft All rights reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# The install script is based off of the MIT-licensed script from glide, | |
# the package manager for Go: https://github.com/Masterminds/glide.sh/blob/master/get | |
PROJECT_NAME="amf" | |
DESIRED_VERSION="1.2.0" | |
AMF_DIST="amf-cli-${DESIRED_VERSION}.jar" | |
DOWNLOAD_URL="https://repository-master.mulesoft.org/nexus/content/repositories/releases/org/mule/amf/amf-cli/${DESIRED_VERSION}/${AMF_DIST}" | |
: ${AMF_INSTALL_DIR:="/usr/local/bin"} | |
# runs the given command as root (detects if we are root already) | |
runAsRoot() { | |
local CMD="$*" | |
if [ $EUID -ne 0 ]; then | |
CMD="sudo $CMD" | |
fi | |
$CMD | |
} | |
# downloadFile downloads the latest binary package and also the checksum | |
# for that binary. | |
downloadFile() { | |
AMF_TMP_ROOT="$(mktemp -dt amf-installer-XXXXXX)" | |
AMF_TMP_FILE="$AMF_TMP_ROOT/$AMF_DIST" | |
echo "Downloading $DOWNLOAD_URL" | |
if type "curl" > /dev/null 2>&1; then | |
curl -SsL "$DOWNLOAD_URL" -o "$AMF_TMP_FILE" | |
elif type "wget" > /dev/null; then | |
wget -q -O "$AMF_TMP_FILE" "$DOWNLOAD_URL" | |
fi | |
} | |
convertToBash() { | |
AMF_BASH_EXECUTABLE=$AMF_TMP_ROOT/${PROJECT_NAME} | |
cat << 'EOF' > ${AMF_BASH_EXECUTABLE} | |
#!/bin/sh | |
MYSELF=`which "$0" 2>/dev/null` | |
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0" | |
java=java | |
if test -n "$JAVA_HOME"; then | |
java="$JAVA_HOME/bin/java" | |
fi | |
exec "$java" $java_args -jar $MYSELF "$@" | |
exit 1 | |
EOF | |
cat ${AMF_TMP_FILE} >> ${AMF_BASH_EXECUTABLE} | |
} | |
installFile() { | |
echo "Preparing to install into ${AMF_INSTALL_DIR}" | |
runAsRoot cp "${AMF_BASH_EXECUTABLE}" "$AMF_INSTALL_DIR" | |
runAsRoot chmod +x "${AMF_INSTALL_DIR}/${PROJECT_NAME}" | |
} | |
# fail_trap is executed if an error occurs. | |
fail_trap() { | |
result=$? | |
if [ "$result" != "0" ]; then | |
if [[ -n "$INPUT_ARGUMENTS" ]]; then | |
echo "Failed to install $PROJECT_NAME with the arguments provided: $INPUT_ARGUMENTS" | |
help | |
else | |
echo "Failed to install $PROJECT_NAME" | |
fi | |
echo -e "\tFor support, go to https://github.com/mulesoft/amf." | |
fi | |
cleanup | |
exit $result | |
} | |
# testVersion tests the installed client to make sure it is working. | |
testVersion() { | |
set +e | |
echo "$PROJECT_NAME installed into $AMF_INSTALL_DIR/$PROJECT_NAME" | |
AMF="$(which $PROJECT_NAME)" | |
if [ "$?" = "1" ]; then | |
echo "$PROJECT_NAME not found. Is $AMF_INSTALL_DIR on your "'$PATH?' | |
exit 1 | |
fi | |
set -e | |
echo "You can run '$PROJECT_NAME' now." | |
} | |
# help provides possible cli installation arguments | |
help () { | |
echo "AMF installer:" | |
echo "Accepted cli arguments are:" | |
echo -e "\t[--help|-h ] ->> prints this help" | |
} | |
# cleanup temporary files | |
cleanup() { | |
rm -rf "$AMF_TMP_ROOT" | |
} | |
# Execution | |
#Stop execution on any error | |
trap "fail_trap" EXIT | |
set -e | |
# Parsing input arguments (if any) | |
export INPUT_ARGUMENTS="${@}" | |
set -u | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
'--help'|-h) | |
help | |
exit 0 | |
;; | |
*) exit 1 | |
;; | |
esac | |
shift | |
done | |
set +u | |
downloadFile | |
convertToBash | |
installFile | |
testVersion | |
cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment