Skip to content

Commit 6984f9e

Browse files
Zsh ize (#1)
Co-authored-by: grzesuav <[email protected]>
1 parent b95f569 commit 6984f9e

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ A collection of plugins for kubectl integration (for Kubectl versions >= 1.12.0)
1414
- Requires Bash.
1515

1616
## Install on Linux/Mac
17+
### bash
1718
```bash
1819
git clone https://github.com/jordanwilson230/kubectl-plugins.git
1920
cd kubectl-plugins
2021
./install-plugins.sh
2122
source ~/.bash_profile
2223
```
24+
25+
### Zsh
26+
```zsh
27+
git clone https://github.com/jordanwilson230/kubectl-plugins.git
28+
cd kubectl-plugins
29+
./install-plugins.sh
30+
source ~/.zshrc
31+
```
2332
#### To Uninstall
33+
##### bash
2434
```
2535
rm -rf ~/.kube/plugins/jordanwilson230
2636
ex '+g/jordanwilson230/d' -cwq ~/.bash_profile
@@ -35,6 +45,21 @@ ex '+g/function kubectl()/d' -cwq ~/.bash_profile
3545
ex '+g/KUBECTL_\(.*\)_PROMPT/d' -cwq ~/.bash_profile
3646
```
3747

48+
##### Zsh
49+
```
50+
rm -rf ~/.kube/plugins/jordanwilson230
51+
ex '+g/jordanwilson230/d' -cwq ~/.zshrc
52+
```
53+
Remove the ```image``` plugin:
54+
```
55+
ex '+g/IMG_REGISTRY=/d' -cwq ~/.zshrc
56+
```
57+
Remove the ```prompt``` plugin:
58+
```
59+
ex '+g/function kubectl()/d' -cwq ~/.zshrc
60+
ex '+g/KUBECTL_\(.*\)_PROMPT/d' -cwq ~/.zshrc
61+
```
62+
3863

3964
### kubectl ssh
4065
![kapssh](https://user-images.githubusercontent.com/22456127/46683069-4152c100-cbbd-11e8-9db5-9fb319bb320b.gif)

install-plugins.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#!/bin/bash
22

3+
SHELL_PROFILE=$(. ./which-shell.sh)
4+
5+
echo "Detected shell: $SHELL"
6+
echo "Installing kubectl-plugins to ~/${SHELL_PROFILE}"
37

48
# Move plugins and add location to path (required for kubectl 1.12+)
59
chmod +x ./kubectl-*
610
rm -rf ~/.kube/plugins/jordanwilson230
711
mkdir -p ~/.kube/plugins/jordanwilson230
812
cp -r ./kubectl-* ~/.kube/plugins/jordanwilson230/
9-
grep 'PATH=$PATH:~/.kube/plugins/jordanwilson230' ~/.bash_profile 1>/dev/null
10-
[ $? -ne 0 ] && echo 'export PATH=$PATH:~/.kube/plugins/jordanwilson230' >> ~/.bash_profile
13+
grep 'PATH=$PATH:~/.kube/plugins/jordanwilson230' ~/${SHELL_PROFILE} 1>/dev/null
14+
[ $? -ne 0 ] && echo 'export PATH=$PATH:~/.kube/plugins/jordanwilson230' >> ~/${SHELL_PROFILE}
1115

1216
# Removes old installer function if exists
13-
ex '+g/function kubectl()/d' -cwq ~/.bash_profile >/dev/null 2>&1
17+
ex '+g/function kubectl()/d' -cwq ~/${SHELL_PROFILE} >/dev/null 2>&1
1418

1519
# Finished
16-
echo -e "\nDone.\nPlease open a new terminal or run: source ~/.bash_profile\n"
20+
echo -e "\nDone.\nPlease open a new terminal or run: source ~/${SHELL_PROFILE}\n"
1721
kubectl plugin list --name-only | sed 's|-| |g'

kubectl-image

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
22

3+
SHELL_PROFILE=$(. ./which-shell.sh)
34

45
usage() { echo -e "Search for docker images.\nUsage: kubectl img <options> -i <image>" && grep " .)\ #" $0; exit 0; }
56
[ $# -eq 0 ] && echo -e "\nERROR: Missing image name [-i].\n" && usage
@@ -15,9 +16,9 @@ while getopts ":i:cph" arg; do
1516
read -p "Save ${REGISTRY} for future reference? [y/n]: " save
1617
if [[ "$save" == "y" ]]; then
1718
# Overwrite or add default registry
18-
ex '+g/IMG_REGISTRY=/d' -cwq ~/.bash_profile
19-
echo 'export IMG_REGISTRY="'$REGISTRY'"' >> ~/.bash_profile
20-
echo -e "\nSaved to ~/.bash_profile. Source or open a new terminal to take effect."
19+
ex '+g/IMG_REGISTRY=/d' -cwq ~/${SHELL_PROFILE}
20+
echo 'export IMG_REGISTRY="'$REGISTRY'"' >> ~/${SHELL_PROFILE}
21+
echo -e "\nSaved to ~/${SHELL_PROFILE}. Source or open a new terminal to take effect."
2122
fi
2223
IMG_REGISTRY="$REGISTRY"
2324
;;
@@ -58,7 +59,7 @@ if [[ "$IMAGE" ]]; then
5859
echo -e "\nSearching Docker Hub for $IMAGE."
5960
available_images=($(docker search --limit=25 "${IMAGE}" | sed -e 's|TAG||g; s|\[OK\]||g; s|AUTOMATED||g; s|OFFICIAL||g' | sed 's/[[:blank:]]*$//'))
6061
[ "${#available_images[@]}" -le 1 ] && echo -e "\nNo images found for $IMAGE.\n" && exit 1
61-
count=0
62+
count=0
6263
;;
6364
esac
6465

kubectl-prompt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LBLUE=$(echo -en '\033[01;34m')
55
YELLOW=$(echo -en '\033[00;33m')
66
RESTORE=$(echo -en '\033[0m')
77

8+
SHELL_PROFILE=$(. ./which-shell.sh)
89

910
COMMAND="${@}"
1011
export COMMAND="$COMMAND"
@@ -54,23 +55,23 @@ function add_prompt() {
5455
if [[ "$NAMESPACE" ]]; then
5556
read -p "${YELLOW}* Add the namespace ${NAMESPACE} to prompt list [y/n]? ${RESTORE}" answer
5657
[[ "$answer" != "y" ]] && exit 0
57-
echo -e "\n${YELLOW}* Adding ${CLUSTER}${NAMESPACE} to \${KUBECTL_NAMESPACE_PROMPT} in ~/.bash_profile\n- The changes will be applied after starting a new terminal session or after running: source ~/.bash_profile ${RESTORE}"
58+
echo -e "\n${YELLOW}* Adding ${CLUSTER}${NAMESPACE} to \${KUBECTL_NAMESPACE_PROMPT} in ~/${SHELL_PROFILE}\n- The changes will be applied after starting a new terminal session or after running: source ~/${SHELL_PROFILE} ${RESTORE}"
5859
KUBECTL_NAMESPACE_PROMPT="${KUBECTL_NAMESPACE_PROMPT[@]} ${NAMESPACE}"
59-
ex '+g/KUBECTL_NAMESPACE_PROMPT=/d' -cwq ~/.bash_profile
60-
echo "export KUBECTL_NAMESPACE_PROMPT=\"${KUBECTL_NAMESPACE_PROMPT}\"" >> ~/.bash_profile
60+
ex '+g/KUBECTL_NAMESPACE_PROMPT=/d' -cwq ~/${SHELL_PROFILE}
61+
echo "export KUBECTL_NAMESPACE_PROMPT=\"${KUBECTL_NAMESPACE_PROMPT}\"" >> ~/${SHELL_PROFILE}
6162
elif [[ "$CLUSTER" ]]; then
6263
read -p "${YELLOW}* Add the cluster ${CLUSTER} to prompt list [y/n]? ${RESTORE}" answer
6364
[[ "$answer" != "y" ]] && exit 0
64-
echo -e "\n${YELLOW}* Adding ${CLUSTER} to \${KUBECTL_CLUSTER_PROMPT} in ~/.bash_profile\n- The changes will be applied after starting a new terminal session or after running: source ~/.bash_profile ${RESTORE}"
65+
echo -e "\n${YELLOW}* Adding ${CLUSTER} to \${KUBECTL_CLUSTER_PROMPT} in ~/${SHELL_PROFILE}\n- The changes will be applied after starting a new terminal session or after running: source ~/${SHELL_PROFILE} ${RESTORE}"
6566
KUBECTL_CLUSTER_PROMPT+="${CLUSTER/%/ }"
66-
ex '+g/KUBECTL_CLUSTER_PROMPT=/d' -cwq ~/.bash_profile
67-
echo "export KUBECTL_CLUSTER_PROMPT=\"${KUBECTL_CLUSTER_PROMPT}\"" >> ~/.bash_profile
67+
ex '+g/KUBECTL_CLUSTER_PROMPT=/d' -cwq ~/${SHELL_PROFILE}
68+
echo "export KUBECTL_CLUSTER_PROMPT=\"${KUBECTL_CLUSTER_PROMPT}\"" >> ~/${SHELL_PROFILE}
6869
fi
6970

7071
# Add function to check user context when kubectl is invoked.
7172
if [[ ! -z "$NAMESPACE" ]] || [[ ! -z "$CLUSTER" ]]; then
72-
ex '+g/function kubectl/d' -cwq ~/.bash_profile
73-
echo 'function kubectl() { kube=$(which kubectl); $kube prompt "${@}" && command $kube "${@}" ; }' >> ~/.bash_profile
73+
ex '+g/function kubectl/d' -cwq ~/${SHELL_PROFILE}
74+
echo 'function kubectl() { command kubectl prompt "$@" && command kubectl "$@" }' >> ~/${SHELL_PROFILE}
7475
exit 0
7576
fi
7677
}
@@ -79,9 +80,9 @@ function add_prompt() {
7980

8081
### Clear prompt list
8182
function remove_prompt() {
82-
ex '+g/KUBECTL_NAMESPACE_PROMPT=/d' -cwq ~/.bash_profile
83-
ex '+g/KUBECTL_CLUSTER_PROMPT=/d' -cwq ~/.bash_profile
84-
ex '+g/function kubectl/d' -cwq ~/.bash_profile
83+
ex '+g/KUBECTL_NAMESPACE_PROMPT=/d' -cwq ~/${SHELL_PROFILE}
84+
ex '+g/KUBECTL_CLUSTER_PROMPT=/d' -cwq ~/${SHELL_PROFILE}
85+
ex '+g/function kubectl/d' -cwq ~/${SHELL_PROFILE}
8586
echo -e "\nCleared prompt list.\nPlease open a new terminal for it to take affect.\n"
8687
exit 0
8788
}

which-shell.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
case $SHELL in
2+
*zsh* )
3+
echo .zshrc
4+
;;
5+
* )
6+
echo .bash_profile
7+
;;
8+
esac

0 commit comments

Comments
 (0)