-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensure-kube-context-stern.plugin.zsh
43 lines (38 loc) · 1.12 KB
/
ensure-kube-context-stern.plugin.zsh
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
# Function: enforce_context_stern
# This function ensures that the `--context` flag is included in `stern` commands
# except for help, version, and other safe subcommands that don't need it.
function enforce_context_stern() {
local safe_subcommands=(
"help"
"version"
"completion"
)
local found_context=false
local first_arg=$1
# Check if the first argument is in the list of safe subcommands
for subcommand in "${safe_subcommands[@]}"; do
if [[ "$first_arg" == "$subcommand" ]]; then
# If the subcommand is safe, run the command without checking --context
command stern "$@"
return
fi
done
# Check if the --context flag is provided
for arg in "$@"; do
if [[ "$arg" == "--context" ]]; then
found_context=true
break
fi
done
# If --context is not found, print an error and exit
if [[ "$found_context" == false ]]; then
echo "Error: The --context flag must be provided for this stern command."
return 1
fi
# Run the command with all provided arguments
command stern "$@"
}
# Override the stern function
function stern() {
enforce_context_stern "$@"
}