forked from SAP-archive/devtoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btpguid.myscript
executable file
·51 lines (35 loc) · 1.25 KB
/
btpguid.myscript
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
# btpguid - return BTP subaccount/directory GUIDs
# Usage: btpguid [-t|--target] displayname
# Returns the GUID for the given subaccount or directory, which is
# specified by name. If the option -t or --target is specified, it
# will also set that subaccount or directory as the target.
# Requires the btp CLI. Will direct you to log in first if you're
# not already logged in.
# It uses the detail from the output of this command:
# btp get accounts/global-account --show-hierarchy
# Uses the "${2:-$1}" technique seen in fff - see
# https://qmacro.org/autodidactics/2021/09/03/exploring-fff-part-1-main/
# for details.
gethier ()
{
btp get accounts/global-account --show-hierarchy 2>/dev/null
}
main() {
local hierarchy subtype guid displayname rc=0
local event=Devtoberfest
displayname="${2:-$1}"
[[ -z displayname ]] && {
echo "No display name specified"
exit 1
}
hierarchy="$(gethier)" || { btp login && hierarchy="$(gethier)"; }
read subtype guid <<< "$(grep -P -o "^(subaccount|directory)\s+(\S+)(?=\s+$displayname)" <<< $hierarchy)"
# Set the subtype as target if requested
[[ $1 == -t ]] || [[ $1 == --target ]] && {
btp target "--${subtype}" "$guid" &> /dev/null
rc=$?
}
echo $guid
return $rc
}
main "$@"