forked from johnfonner/abaco-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
/
abaco-aliases-create.sh
102 lines (84 loc) · 2.14 KB
/
abaco-aliases-create.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
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
THIS=$(basename $0)
THIS=${THIS%.sh}
THIS=${THIS//[-]/ }
HELP="
Usage: ${THIS} [OPTION] [ACTORID] [ALIAS]
Create an alias for an actor.
Options:
-h show help message
-z oauth access token
-p skip pre-flight check of actor and alias
-v verbose output
-V very verbose output
"
#function usage() { echo "$0 usage:" && grep " .)\ #" $0; exit 0; }
function usage() {
echo "$HELP"
exit 0
}
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$DIR/abaco-common.sh"
tok=
preflight=1
function do_preflight() {
resp1=$(${DIR}/abaco ls -v ${1} | jq -r .result)
if [[ "$resp1" == "null" ]]; then
die "Actor ${1} was not found"
fi
resp2=$(${DIR}/abaco aliases ls ${2} | jq -r .result)
if [[ "$resp2" != "null" ]]; then
die "Alias ${2} already exists"
fi
}
# print usage if no arguments were given
if [[ "$#" -lt "1" ]]; then
usage
fi
while getopts ":hpvz:V" o; do
case "${o}" in
z) # custom token
tok=${OPTARG}
;;
p) # no preflight
preflight=0
;;
v) # verbose
verbose="true"
;;
V) # verbose
very_verbose="true"
;;
h | *) # print help text
usage
;;
esac
done
shift $((OPTIND - 1))
if [ ! -z "$tok" ]; then TOKEN=$tok; fi
if [[ "$very_verbose" == "true" ]]; then
verbose="true"
fi
actor_id="$1"
alias_str="$2"
if ((preflight)); then
do_preflight ${actor_id} ${alias_str}
fi
if [ -z "${alias_str}" ] || [ -z "${actor_id}" ]; then
echo "Please specify an actor ID followed by an alias"
usage
fi
data="{\"actorId\":\"${actor_id}\", \"alias\":\"${alias_str}\"}"
curlCommand="curl -XPOST -sk -H \"Authorization: Bearer $TOKEN\" -H \"Content-Type: application/json\" --data '$data' '$BASE_URL/actors/v2/aliases'"
function filter() {
# eval $@ | jq -r '.result | .[] | [.name, .id, .status] | @tsv' | column -t
eval $@ | jq -r '.result | [.alias, .actorId, .owner] | "\(.[0]) \(.[1]) \(.[2])"' | column -t
}
if [[ "$very_verbose" == "true" ]]; then
echo "Calling $curlCommand"
fi
if [[ "$verbose" == "true" ]]; then
eval $curlCommand
else
filter $curlCommand
fi