-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgsw.sh
More file actions
166 lines (149 loc) · 4.95 KB
/
Copy pathgsw.sh
File metadata and controls
166 lines (149 loc) · 4.95 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
#!/bin/bash
# gsw: Google Switch
# Easily switch between gcloud configurations
GSW_VERSION="v0.3.0"
function gsw() {
# Check if gcloud is installed
if ! command -v gcloud >/dev/null 2>&1; then
echo "Error: 'gcloud' command not found."
echo "Please install the Google Cloud SDK first: https://cloud.google.com/sdk/docs/install"
return 1
fi
local is_global=false
local config_name=""
while [[ $# -gt 0 ]]; do
case "$1" in
--version|-v)
echo "gsw version $GSW_VERSION"
return 0
;;
--help|-h)
echo "Usage: gsw [options] <config_name>"
echo ""
echo " Switches the gcloud configuration for the current session by default."
echo ""
echo "Options:"
echo " <config_name> Name of the gcloud configuration to activate"
echo " -g, --global Switch the GLOBAL active configuration"
echo " -h, --help Show this help message"
echo " -v, --version Show version information"
echo ""
echo "💡 Tip: Run 'gsw' without arguments to see available configurations."
return 0
;;
-g|--global)
is_global=true
shift
;;
-*)
echo "Error: Unknown option $1"
return 1
;;
*)
config_name="$1"
shift
;;
esac
done
if [ -z "$config_name" ]; then
echo "Available gcloud configurations:"
local session_config="$CLOUDSDK_ACTIVE_CONFIG_NAME"
local list_output
list_output=$(gcloud config configurations list 2>/dev/null)
if [ -n "$session_config" ]; then
# If session config is active, highlight it in the list
echo "$list_output" | while IFS= read -r line; do
# Extract the first column (NAME) and compare literally
local name="${line%% *}"
if [ "$name" = "$session_config" ]; then
echo "* $line (session active)"
else
echo " $line"
fi
done
else
# shellcheck disable=SC2001
echo "$list_output" | sed 's/^/ /'
fi
echo ""
echo "Usage: gsw [options] <config_name>"
echo " (Default switches only for this terminal session)"
echo ""
echo "Options:"
echo " -g, --global Switch the GLOBAL active configuration"
return
fi
# Security: Validate configuration name (allow only [a-z0-9_-])
# This prevents command injection or other malicious input.
if [[ ! "$config_name" =~ ^[a-z0-9_-]+$ ]]; then
echo "Error: Invalid configuration name '$config_name'."
echo "Configuration names can only contain lowercase letters, numbers, hyphens, and underscores."
return 1
fi
# Check if the configuration exists
if ! gcloud config configurations describe "$config_name" > /dev/null 2>&1; then
echo "Error: Configuration '$config_name' does not exist."
echo "Available configurations:"
gcloud config configurations list
return 1
fi
if [ "$is_global" = true ]; then
gcloud config configurations activate "$config_name"
echo "✅ Switched GLOBALLY to configuration: $config_name"
else
export CLOUDSDK_ACTIVE_CONFIG_NAME="$config_name"
echo "✅ Switched SESSION to configuration: $config_name"
echo "(This change only affects this terminal tab)"
fi
echo "Current Identity:"
gcloud config list --format="value(core.account,core.project)" | tr '\t' '\n' | sed 's/^/- /'
}
if [ -n "$BASH_VERSION" ]; then
# Bash Completion
_gsw_bash_autocomplete() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Flags
opts="-g --global -h --help -v --version"
case "${prev}" in
-g|--global)
# Only configurations after global flag
local configs
configs=$(gcloud config configurations list --format="value(name)" 2>/dev/null)
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${configs}" -- "${cur}") )
return 0
;;
esac
if [[ ${cur} == -* ]] ; then
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
# Default: configurations + flags
local configs
configs=$(gcloud config configurations list --format="value(name)" 2>/dev/null)
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${opts} ${configs}" -- "${cur}") )
return 0
}
complete -F _gsw_bash_autocomplete gsw
elif [ -n "$ZSH_VERSION" ]; then
# Zsh Completion
_gsw() {
_arguments \
'(-g --global)'{-g,--global}'[Switch the GLOBAL active configuration]' \
'(-h --help)'{-h,--help}'[Show help message]' \
'(-v --version)'{-v,--version}'[Show version information]' \
'*:gcloud configuration:_gsw_configs'
}
_gsw_configs() {
local -a configs
# shellcheck disable=SC2034,SC2296
configs=("${(@f)$(gcloud config configurations list --format="value(name)" 2>/dev/null)}")
_describe -t configurations 'gcloud configuration' configs
}
compdef _gsw gsw
fi