Skip to content

Instantly share code, notes, and snippets.

@vromero
Last active April 4, 2018 18:30
Show Gist options
  • Save vromero/583f79dab40a55b42437501105b312b8 to your computer and use it in GitHub Desktop.
Save vromero/583f79dab40a55b42437501105b312b8 to your computer and use it in GitHub Desktop.
AMF CLI installer
#!/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.3.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
@vromero
Copy link
Author

vromero commented Mar 17, 2018

AMF_INSTALLER_URL="https://gist.githubusercontent.com/vromero/583f79dab40a55b42437501105b312b8/raw/46baab44971bfb3ece5c6f43c885182cc3160e68/installer.sh" && bash <(curl -s ${AMF_INSTALLER_URL} 2>/dev/null || wget -q -O - ${AMF_INSTALLER_URL} 2>/dev/null

@pcolunga
Copy link

AMF_INSTALLER_URL="https://gist.githubusercontent.com/vromero/583f79dab40a55b42437501105b312b8/raw/46baab44971bfb3ece5c6f43c885182cc3160e68/installer.sh" && bash <(curl -s ${AMF_INSTALLER_URL} 2>/dev/null || wget -q -O - ${AMF_INSTALLER_URL} 2>/dev/null)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment