Skip to content

Instantly share code, notes, and snippets.

@dmikusa
Created June 24, 2019 13:54
Show Gist options
  • Save dmikusa/cf1425881f36453c03510269734408c0 to your computer and use it in GitHub Desktop.
Save dmikusa/cf1425881f36453c03510269734408c0 to your computer and use it in GitHub Desktop.
Find an app on Cloud Foundry with the exact app name given. Searches through all apps visible to the currently logged in user.
#!/bin/bash
#
# Find an app with an exact name, app must be visible to the logged in user
#
set -e
function load_all_pages {
URL="$1"
DATA=""
until [ "$URL" == "null" ]; do
RESP=$(cf curl "$URL")
DATA+=$(echo "$RESP" | jq .resources)
URL=$(echo "$RESP" | jq -r .next_url)
done
# dump the data
echo "$DATA" | jq .[] | jq -s
}
function load_all_apps {
load_all_pages "/v2/apps"
}
function main {
if [ "$1" == "" ]; then
echo
echo "USAGE:"
echo " ./find_app.sh <name>"
echo
exit 255
fi
APPS=$(load_all_apps)
APP=$(echo "$APPS" | jq -r ".[] | select(.entity.name == \"$1\")")
if [ "$APP" != "" ]; then
SPACE_URL=$(echo "$APP" | jq -r ".entity.space_url")
SPACE=$(cf curl "$SPACE_URL")
SPACE_NAME=$(echo "$SPACE" | jq -r ".entity.name")
ORG_URL=$(echo "$SPACE" | jq -r ".entity.organization_url")
ORG=$(cf curl "$ORG_URL")
ORG_NAME=$(echo "$ORG" | jq -r ".entity.name")
echo "Found App [$1] in org [$ORG_NAME] and space [$SPACE_NAME]"
else
echo "App does not exist or is not visible to the currently logged in user."
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment