-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf_home.sh
More file actions
173 lines (169 loc) · 3.93 KB
/
cf_home.sh
File metadata and controls
173 lines (169 loc) · 3.93 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#
# cf_home - a bash function to ease connectivity to cloud foundry
# APIs.
#
# If $HOME/.cf-proxy exists, it will be used for the proxy settings
# for that CF api instance
#
# If you use multiple CF deployments, this allows you to define and
# switch between those easily, avoiding constant "cf logout", "cf api",
# and other commands associated wit CF endpoints.
#
# Usage:
# "source cf_home.sh" - creates a BASH alias to manage CF api endpoints
# cf_home -m <newname>
# make a new "cfhome" instance, copying proxy variables from ~/.cf-proxy
# cf_home -c
# clear CF related environment variables (i.e. turn off this)
# cf_home -l
# list defined CFHOME endpoints
# cf_home -D
# delete a CFHOME endpoint
# cf_home -d
# enable debugging
# cf_home -h
# usage message
# cf_home
# with no parameter, cf_home prints out config and "cf target" info
#
#
# This takes advantage of the cf-cli environment variables (see: "cf help -a"),
# which is why this is a function, and not a script.
# CF_HOME is modified to switch around environments
# CF_PLUGIN_HOME is used to avoid multiple copies of plugins
# CF_COLOR may be set optionally
# CF_PROXY_CFG
# These can be set from DEF_CF_COLOR, DEF_CF_PLUGIN_HOME,
# DEF_CF_PROXY_CFG, or just use the default values
if [[ $( basename ${#-}) = $( basename ${BASH_SOURCE} ) ]];
then
echo "you want to \"source\" this script"
fi
# default values
export CF_COLOR=${DEF_CF_COLOR:-false}
export CF_PLUGIN_HOME=${DEF_CF_PLUGIN_HOME:-$HOME/.cf/plugins}
# where to find the "default" proxy configuration when creating a new CF_HOME
export CF_PROXY_CFG=${DEF_CF_PROXY_CFG:-$HOME/.cf/proxy.cfg}
# cloud foundry specific aliases
function cf_home () {
function usage() {
echo '[-m <make>, -c <clear>, -l <list>, -D <del>]'
}
function log() {
if [ ${do_debug} -ne 0 ];
then
echo "$1"
fi
}
function switch_cfhome () {
log "switch_cfhome"
# switch CFHOME and proxies (if applicable)
if [ -z "$1" ];
then
log "clearing CFHOME"
unset CF_HOME
unset CF_PROXY
else
log "setting CF_HOME to .cf-$1"
export CF_HOME=$HOME/.cf-"$1"
export CF_PROXY=$CF_HOME/proxy.cfg
fi
if [ -e ${CF_PROXY} ];
then
log "setting $1 proxy"
. ${CF_PROXY}
else
[[ ! -z "$http_proxy" ]] && echo '[proxy unset]'
unset http_proxy
unset https_proxy
fi
}
# main part of cf_home function
do_create=0
do_delete=0
do_debug=0
while [ $# -gt 0 ];
do
# process flags
case "$1" in
"-m")
# make the directory
log "-m create flag"
do_create=1
shift ;;
"-D")
# delete the directory
log "-D delete flag"
do_delete=1
shift ;;
"-c")
# clear cf_home flags
log "-c clear variables"
unset CF_HOME
shift ;;
"-l")
# list CF directories
log "-l list"
/bin/ls -1df $HOME/.cf $HOME/.cf-*
return 0 ;;
"-d")
# enable debugging messages
do_debug=1
log "debugging enabled"
shift ;;
"-h*")
usage
return 0 ;;
"-*")
# unknown flag
usage
return 1 ;;
*)
break ;;
esac
done
if [ -z "$1" ];
then
# usage-y type of thing
echo "CF_HOME=\"$CF_HOME\""
cf target
usage
return 0
fi
# if deleting, make sure it exists
if [ $do_delete == 1 ] ;
then
rm -rf $HOME/.cf-"$1"
echo "[$1 deleted]"
return 0
fi
if [ ${do_create} == 1 ] && [ -d $HOME/.cf-"$1" ];
then
# just switch to it, if it already exists
switch_cfhome "$1"
cf target
return 0
fi
if [ $do_create == 1 ] && [ ! -d $HOME/.cf-"$1" ] ;
then
CF_HOME=${HOME}/.cf-"$1"
CF_PROXY=${CF_HOME}/proxy.cfg
mkdir ${CF_HOME}
if [ -f ${CF_PROXY_CFG} ];
then
cp ${CF_PROXY_CFG} ${CF_PROXY}
else
echo "[no default proxy specified in \$CF_PROXY_CFG]"
fi
switch_cfhome "$1"
echo "[$1 created]"
return 0
fi
# all flags processed, just switch home if it exists
if [ ! -d "${HOME}/.cf-$1" ];
then
echo "[no cf_home for $1 -- perhaps you meant -m?]"
return 0
fi
switch_cfhome "$1"
}