Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

WEEK2CHALLENGE stefanluettke #160

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions topics/Week2_Best_Practices/challenge/btpguid
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
# 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

displayname="${2:-$1}"

[[ -z $displayname ]] && {
echo "No display name specified"
exit 1
}

hierarchy="$(gethier)" || { btp login && hierarchy="$(gethier)"; }
read -r 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 "$@"