Skip to content

Instantly share code, notes, and snippets.

@al4
Last active July 15, 2016 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save al4/3fafd7636d2c2ada3b945f6cb9df5779 to your computer and use it in GitHub Desktop.
Save al4/3fafd7636d2c2ada3b945f6cb9df5779 to your computer and use it in GitHub Desktop.
Prompt to select openstack environment
#!/usr/bin/env bash
function osauth() {
osauth_dir="~/.osauth"
# List files in ~/.osauth
tenant_paths=(~/.osauth/*.sh)
# Strip the leading path
tenant_files=(${tenant_paths[@]#~/.osauth/})
# Strip the trailing file name
tenants=(${tenant_files[@]/-openrc.sh/})
if [ ${#tenants[@]} -eq 1 ]; then
mytenant=${tenants[0]}
else
osauth_chooser
fi
echo "$mytenant selected"
for file in ${tenant_paths[@]}; do
# Determine if $file contains $mytenant
if contains "$file" "$mytenant"; then
source $file
fi
done
}
function osauth_chooser() {
PS3="Select an Openstack tenant: "
select option in ${tenants[@]}; do
mytenant=$option
break
done
}
function contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment