-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathabaco-common.sh
91 lines (71 loc) · 1.91 KB
/
abaco-common.sh
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
#!/bin/bash
if [[ ! -x $( which jq ) ]]; then
echo "Error: jq was not found."
echo "This CLI requires jq. Please install it first."
echo " - https://stedolan.github.io/jq/download/"
exit 1
fi
AGAVE_AUTH_CACHE=
if [ ! -z "${AGAVE_CACHE_DIR}" ] && [ -d "${AGAVE_CACHE_DIR}" ]; then
if [ -f "${AGAVE_CACHE_DIR}/current" ]; then
AGAVE_AUTH_CACHE="${AGAVE_CACHE_DIR}/current"
fi
else
AGAVE_AUTH_CACHE="$HOME/.agave/current"
fi
if [ ! -f "${AGAVE_AUTH_CACHE}" ]; then
echo "Error: API credentials are not configured."
exit 1
fi
BASE_URL=$(jq -r .baseurl ${AGAVE_AUTH_CACHE})
CLIENT_SECRET=$(jq -r .apisecret ${AGAVE_AUTH_CACHE})
CLIENT_KEY=$(jq -r .apikey ${AGAVE_AUTH_CACHE})
USERNAME=$(jq -r .username ${AGAVE_AUTH_CACHE})
TOKEN=$(jq -r .access_token ${AGAVE_AUTH_CACHE})
TENANTID=$(jq -r .tenantid ${AGAVE_AUTH_CACHE})
function build_json_from_array() {
local myarray=("$@")
local var_count=${#myarray[@]}
if [ $var_count -eq 0 ]; then
echo "{}"
exit 0
fi
local last_index=$((${#myarray[@]}-1))
local json="{"
for i in $(seq 0 $last_index); do
local key_value=${myarray[$i]}
local key=${key_value%=*}
local value=${key_value#*=}
# add key-value pair
json="${json}\"$key\":\"$value\""
# if last pair, close with curly brace
# otherwise, add comma for next value
if [ $i -eq $last_index ]; then
json="${json}}"
else
json="${json}, "
fi
done
echo "$json"
}
function is_json() {
echo "$@" | jq -e . >/dev/null 2>&1
}
function single_quote() {
local str="$1"
local first_char="$(echo "$str" | head -c 1)"
if ! [ "$first_char" == "'" ]; then
str="'$str'"
fi
echo "$str"
}
function die() {
echo "[CRITICAL] $1"
exit 1
}
function warn() {
echo "[WARNING] $1"
}
function info() {
echo "[INFO] $1"
}