-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.cf
145 lines (120 loc) · 3.49 KB
/
lib.cf
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
#!bin/bash
export CF_PROMPT_ENABLED=0
# function cf() {
# local l_args=( "$@" )
# local l_cf=$(which cf)
# true >/tmp/cf-err
# ${l_cf} "${l_args[@]}" 1> >(tee /tmp/cf-err)
# # shellcheck disable=SC2181
# if [ $? -ne 0 ]; then
# grep -q "Authentication has expired" /tmp/cf-err && {
# ${l_cf} login && {
# ${l_cf} "${l_args[@]}"
# }
# }
# grep -q "Not logged in" /tmp/cf-err && {
# ${l_cf} login && {
# ${l_cf} "${l_args[@]}"
# }
# }
# fi
# }
function __cf_get_org_space() {
local l_file="$(readlink -m ${HOME}/.cf/config.json)"
if [ ! -f "${l_file}" ]; then
return 0
fi
jq -r '. | "\(.OrganizationFields.Name):\(.SpaceFields.Name)"' 2>/dev/null < ${l_file}
return $?
}
function __cf_has_changes() {
local l_current="$(readlink -m ${HOME}/.cf/config.json)"
local l_lastsave="$(readlink -m ${HOME}/.cf/targets/current)"
if [ ! -f "${l_current}" ] || [ ! -f "${l_lastsave}" ]; then
return 1
fi
cmp -s ${l_current} ${l_lastsave}
return $?
}
function __cf_update_target() {
test ${CF_PROMPT_ENABLED} -eq 0 && {
return 0
}
local l_file="$(readlink -m ${HOME}/.cf/targets/current)"
if [ ! -f "${l_file}" ]; then
return 0
fi
local l_name=$(basename ${l_file})
local l_target=${l_name%%.*}
# local l_context=$(__cf_get_org_space)
local l_type="c"
local l_label=${l_target}
# if [ "${l_context}" = ":" ]; then
# l_context="unset:unset"
# fi
# if [ ! -z "${l_context}" ]; then
# l_label="${l_label}:${l_context}"
# fi
local l_color=""
if [[ "${l_name}" == *"prod"* ]]; then
l_color="lightred"
elif [[ "${l_name}" == *"preprod"* ]]; then
l_color="lightmagenta"
elif [[ "${l_name}" == *"rec"* ]]; then
l_color="lightyellow"
elif [[ "${l_name}" == *"dev"* ]]; then
l_color="lightyellow"
elif [[ "${l_name}" == *"beta"* ]]; then
l_color="lightyellow"
elif [[ "${l_name}" == *"local"* ]]; then
l_color="lightgreen"
else
l_color="lightblue"
fi
__cf_has_changes
# if [ $? -eq 1 ]; then
# promptcmd_add_labels "*${l_type}:${l_label}*|${l_color}"
# else
promptcmd_add_labels "${l_type}:${l_label}|${l_color}"
# fi
}
function cf_enable_prompt() {
CF_PROMPT_ENABLED=1
promptcmd_push "__cf_update_target"
}
function cf_disable_prompt() {
CF_PROMPT_ENABLED=0
}
__cf_complete_cli() {
# All arguments except the first one
local args
args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
# Only split on newlines
local IFS=$'\n'
# Call completion (note that the first element of COMP_WORDS is
# the executable itself)
COMPREPLY+=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}"))
return 0
}
__cf_complete_targets() {
# shellcheck disable=SC2034
local cur prev words cword split
_init_completion -s || return
local l_cmd="${words[*]:0:${cword}}"
if [[ "${l_cmd}" =~ ^.*(set-target|delete-target)( -f)?$ ]];then
local targets=$(find ${HOME}/.cf/targets/*.config.json -print0 | \
xargs -0 -n1 basename | \
sed 's/.config.json//g')
local IFS=$'\n'
COMPREPLY+=($(compgen -W "${targets}" -- ${cur}))
if [ "${prev}" != "-f" ]; then
COMPREPLY+=($(compgen -W "-f" -- ${cur}))
fi
elif [ "${prev}" == "cf" ]; then
__cf_complete_cli
COMPREPLY+=($(compgen -W "manifest-generator targets save-target set-target delete-target" -- ${cur}))
else
__cf_complete_cli
fi
}
complete -F __cf_complete_targets cf