-
Notifications
You must be signed in to change notification settings - Fork 15
/
kcs
executable file
·63 lines (57 loc) · 1.99 KB
/
kcs
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
#!/usr/bin/env bash
# Set some variables that will be used later
KUBEDIR="$HOME/.kube"
KUBECFG="config"
# Assign help text to variable for user later
HELP="Usage: kcs [ context-name | none | list ]"
# Ensure that only a single parameter was passed
# Display help text if more than 1 parameter was supplied
if [ "$#" -ne 1 ]; then
echo "Error: Incorrect number of parameters; please supply only a valid Kubernetes context or valid command"
echo
echo $HELP
exit 1
fi
# If the user supplied "--help", then provide help text
if [ "$1" = "--help" ]; then
echo $HELP
exit 0
fi
# Assign supplied parameter to variable for use later
KCFG="$1"
# Take action based on the input supplied by the user
case "$KCFG" in
# If user supplied 'none', then remove default config file and exit cleanly
# Report an error if no active context
none)
if [ ! -f "$KUBEDIR/$KUBECFG" ]; then
echo "No active Kubernetes context"
exit 1
else
rm -f "$KUBEDIR/$KUBECFG"
exit 0
fi
;;
# If user supplied 'list', then list files in the default directory but hide
# files matching the default config file name as defined earlier
list)
echo $(ls -l --hide="$KUBECFG" "$KUBEDIR" | tail -n +2 | grep -v '^d' | awk '{print $9}')
exit 0
;;
# If user supplied 'config', report an error and exit with an error code
"$KUBECFG")
echo "Not a selectable Kubernetes context; use 'kcs list' to see available contexts"
exit 1
;;
# If the user supplied anything else, check to see if it is a valid config file
# If it is a valid config file, copy it to the default config file
# Otherwise, report an error and exit with an error code
*)
if [ -f "$KUBEDIR/$KCFG" ]; then
cp "$KUBEDIR/$KCFG" "$KUBEDIR/$KUBECFG"
exit 0
else
echo "Invalid Kubernetes context; use 'kcs list' to see available contexts"
exit 1
fi
esac