1+ #! /bin/bash
2+
3+ # ==============================
4+ # COMMON FUNCTIONS
5+ # ==============================
6+
7+ RED=" \033[31m"
8+ BLUE=" \033[94m"
9+ GREEN=" \033[32m"
10+ ENDCOLOR=" \033[0m"
11+
12+ function print_error {
13+ echo -e " ${RED} \n==========================================${ENDCOLOR} "
14+ echo -e " ${RED} FAILED${ENDCOLOR} "
15+ echo -e " ${RED} ==========================================\n${ENDCOLOR} "
16+ echo -e " ${RED} $1 ${ENDCOLOR} "
17+ echo -e " "
18+ }
19+ function print_msg {
20+ echo -e " ${BLUE} $1 ${ENDCOLOR} "
21+ }
22+ function print_success {
23+ echo -e " ${GREEN} $1 ${ENDCOLOR} "
24+ }
25+
26+ # Helper function to check whether prerequisites are installed
27+ function check_prerequisites {
28+ # Ensure that jq tool is installed
29+ if ! command -v jq & > /dev/null; then
30+ print_error " 'jq' tool is not installed"
31+ exit 1
32+ fi
33+ }
34+
35+ # ==============================
36+ # COMMON IBMCLOUD HELPERS
37+ # ==============================
38+
39+ # helper function to check whether IBM Cloud CLI plugins should get updated, or not
40+ function ensure_plugin_is_up_to_date() {
41+ echo " Checking $1 ..."
42+ # check whether plugin is installed
43+ if ! ibmcloud plugin show $1 -q > /dev/null; then
44+ # install it
45+ ibmcloud plugin install $1 -f --quiet
46+ else
47+ # check whether there is an update available
48+ ibmcloud plugin update $1 -f --quiet
49+ fi
50+ }
51+
52+ function target_region {
53+ print_msg " \nTargetting IBM Cloud region '$1 ' ..."
54+ current_region=$( ibmcloud target --output JSON | jq -r ' .region|.name' )
55+ if [[ " $current_region " != " $1 " ]]; then
56+ ibmcloud target -r $1 --quiet
57+ fi
58+ }
59+
60+ function target_resource_group {
61+ print_msg " \nTargetting resource group '$1 ' ..."
62+ current_resource_group_guid=$( ibmcloud target --output JSON | jq -r ' .resource_group|.guid' )
63+ new_resource_group_guid=$( ibmcloud resource group $1 -output json| jq -r ' .[0].id' )
64+ if [[ " $current_resource_group_guid " != " $new_resource_group_guid " ]]; then
65+ ibmcloud target -g $1 --quiet
66+ fi
67+ }
0 commit comments