Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions kcs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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
if [ "$#" -ne 1 ] && [ "$#" -ne 2 ]; then
echo "Error: Incorrect number of parameters; please supply only a valid Kubernetes context or valid command"
echo
echo $HELP
Expand All @@ -25,29 +25,39 @@ fi
# Assign supplied parameter to variable for use later
KCFG="$1"

#Assign new "home" directory
if [ "$#" -eq 2 ] && [ -d "$2" ]; then
KUBEDIR="$2"
else
echo "Error: Incorrect directory"
echo
echo $HELP
exit 1
fi

# If user specified 'none', then remove the file and exit the script cleanly
if [ $KCFG == "none" ]; then
if [ ! -f $KUBEDIR/$KUBECFG ]; then
if [ ! -f "$KUBEDIR/$KUBECFG" ]; then
echo "No Kubernetes context active"
exit 0
else
rm $KUBEDIR/$KUBECFG
rm "$KUBEDIR/$KUBECFG"
exit 0
fi
fi

# If user specified 'list', then list the files in the ~/.kube directory
# If user specified 'list', then list the files in the config directory
if [ $KCFG == "list" ]; then
echo $(ls -l "$KUBEDIR" | tail -n +2 | grep -v '^d' | awk '{print $9}')
exit 0
fi

# Check to ensure user supplied a valid filename of a file
# in the ~/.kube directory
if [ ! -f $KUBEDIR/$KCFG ]; then
# in the config directory
if [ ! -f "$KUBEDIR/$KCFG" ]; then
echo "Invalid Kubernetes context specified; use 'kcs list' to see available contexts"
exit 1
fi

# Copy supplied filename to ~/.kube/config
cp $KUBEDIR/$KCFG $KUBEDIR/$KUBECFG
cp "$KUBEDIR/$KCFG" "$KUBEDIR/$KUBECFG"