generated from finos/software-project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 29
/
morphir-cli
executable file
·127 lines (105 loc) · 4.96 KB
/
morphir-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env sh
# This is a wrapper script, that automatically downloads morphir-cli from maven
# It tries to read the required morphir-cli version in this order:
# 1) --morphir-cli-version parameter
# 2) MORPHIR_CLI_VERSION env variable
# 3) reads from .morphir-cli-version file
# 4) reads from .config/morphir-cli-version file
# 5) tries to fetch newest from Github
# 6) DEFAULT_MORPHIR_CLI_VERSION set in this file
#
# Script Version: 0.4.10
#
set -e
if [ -z "${DEFAULT_MORPHIR_CLI_VERSION}" ] ; then
DEFAULT_MORPHIR_CLI_VERSION="0.4.0-RC08"
fi
MORPHIR_CLI_REPO_URL="https://github.com/finos/morphir-scala"
if [ -z "${CURL_CMD}" ] ; then
CURL_CMD=curl
fi
# Explicit commandline argument takes precedence over all other methods
if [ "$1" = "--morphir-cli-version" ] ; then
shift
if [ "x$1" != "x" ] ; then
MORPHIR_CLI_VERSION="$1"
echo "1 - Will use MORPHIR_CLI_VERSION=$MORPHIR_CLI_VERSION" 1>&2
shift
else
echo "You specified --morphir-cli-version without a version." 1>&2
echo "Please provide a version that matches one provided on" 1>&2
echo "${MORPHIR_CLI_REPO_URL}/releases" 1>&2
false
fi
fi
# Please note, that if a MORPHIR_CLI_VERSION is already set in the environment,
# We reuse it's value and skip searching for a value.
# If not already set, read .morphir-cli-version file
if [ -z "${MORPHIR_CLI_VERSION}" ] ; then
if [ -f ".morphir-cli-version" ] ; then
MORPHIR_CLI_VERSION="$(head -n 1 .morphir-cli-version 2> /dev/null)"
echo "2 - Will use MORPHIR_CLI_VERSION=$MORPHIR_CLI_VERSION" 1>&2
elif [ -f ".config/morphir-cli-version" ] ; then
MORPHIR_CLI_VERSION="$(head -n 1 .config/morphir-cli-version 2> /dev/null)"
echo "3 - Will use MORPHIR_CLI_VERSION=$MORPHIR_CLI_VERSION" 1>&2
fi
fi
MORPHIR_CLI_USER_CACHE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/morphir-cli"
if [ -z "${MORPHIR_CLI_DOWNLOAD_PATH}" ] ; then
MORPHIR_CLI_DOWNLOAD_PATH="${MORPHIR_CLI_USER_CACHE_DIR}/download"
fi
# If not already set, try to fetch newest from Github
if [ -z "${MORPHIR_CLI_VERSION}" ] ; then
# TODO: try to load latest version from release page
echo "No morphir-cli version specified." 1>&2
echo "You should provide a version via '.morphir-cli-version' file or --morphir-cli-version option." 1>&2
mkdir -p "${MORPHIR_CLI_DOWNLOAD_PATH}"
LANG=C touch -d '1 hour ago' "${MORPHIR_CLI_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || (
# we might be on OSX or BSD which don't have -d option for touch
# but probably a -A [-][[hh]mm]SS
touch "${MORPHIR_CLI_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MORPHIR_CLI_DOWNLOAD_PATH}/.expire_latest"
) || (
# in case we still failed, we retry the first touch command with the intention
# to show the (previously suppressed) error message
LANG=C touch -d '1 hour ago' "${MORPHIR_CLI_DOWNLOAD_PATH}/.expire_latest"
)
# POSIX shell variant of bash's -nt operator, see https://unix.stackexchange.com/a/449744/6993
# if [ "${MORPHIR_CLI_DOWNLOAD_PATH}/.latest" -nt "${MORPHIR_CLI_DOWNLOAD_PATH}/.expire_latest" ] ; then
if [ -n "$(find -L "${MORPHIR_CLI_DOWNLOAD_PATH}/.latest" -prune -newer "${MORPHIR_CLI_DOWNLOAD_PATH}/.expire_latest")" ]; then
# we know a current latest version
MORPHIR_CLI_VERSION=$(head -n 1 "${MORPHIR_CLI_DOWNLOAD_PATH}"/.latest 2> /dev/null)
echo "4 - Will use MORPHIR_CLI_VERSION=$MORPHIR_CLI_VERSION" 1>&2
fi
if [ -z "${MORPHIR_CLI_VERSION}" ] ; then
# we don't know the current latest version
echo "Retrieving latest morphir-cli version ..." 1>&2
LANG=C ${CURL_CMD} -s -i -f -I ${MORPHIR_CLI_REPO_URL}/releases/latest 2> /dev/null | grep --ignore-case Location: | sed s'/^.*tag\///' | sed 's/^v//' | tr -d '\r\n' > "${MORPHIR_CLI_DOWNLOAD_PATH}/.latest"
MORPHIR_CLI_VERSION=$(head -n 1 "${MORPHIR_CLI_DOWNLOAD_PATH}"/.latest 2> /dev/null)
echo "5 - Will use MORPHIR_CLI_VERSION=$MORPHIR_CLI_VERSION" 1>&2
fi
if [ -z "${MORPHIR_CLI_VERSION}" ] ; then
# Last resort
MORPHIR_CLI_VERSION="${DEFAULT_MORPHIR_CLI_VERSION}"
echo "6 - Will use fall back to hardcoded MORPHIR_CLI_VERSION=$MORPHIR_CLI_VERSION" 1>&2
else
echo "7 - Will use MORPHIR_CLI_VERSION=$MORPHIR_CLI_VERSION" 1>&2
fi
fi
MORPHIR_CLI="${MORPHIR_CLI_DOWNLOAD_PATH}/${MORPHIR_CLI_VERSION}"
# If not already downloaded, download it
if [ ! -s "${MORPHIR_CLI}" ] ; then
DOWNLOAD_FILE=$(mktemp morphir-cli.XXXXXX)
DOWNLOAD_URL="https://repo1.maven.org/maven2/org/finos/morphir/morphir-main_3/${MORPHIR_CLI_VERSION}/morphir-main_3-${MORPHIR_CLI_VERSION}-assembly.jar"
echo "${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${DOWNLOAD_URL}""
echo "Downloading morphir-cli ${MORPHIR_CLI_VERSION} from ${DOWNLOAD_URL} ..."
${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${DOWNLOAD_URL}"
chmod +x "${DOWNLOAD_FILE}"
mkdir -p "${MORPHIR_CLI_DOWNLOAD_PATH}"
mv "${DOWNLOAD_FILE}" "${MORPHIR_CLI}"
unset DOWNLOAD_FILE
unset DOWNLOAD_SUFFIX
fi
unset MORPHIR_CLI_DOWNLOAD_PATH
unset MORPHIR_CLI_VERSION
unset MORPHIR_CLI_REPO_URL
java -jar "${MORPHIR_CLI}" "$@"