-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge.sh
executable file
·40 lines (27 loc) · 902 Bytes
/
merge.sh
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
#!/bin/bash
set -eo pipefail
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# shellcheck source=/dev/null
source "$dir/.function"
after=$(merge_kubeconfig 2>/dev/null)
before=$(kubectl config view --raw)
diff <(echo "$before") <(echo "$after") && echo "Same as current config. just quit.." && exit 0
echo
read -r -p "Change will be applied. Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
then
tmpfile=$(mktemp /tmp/kubeconfig.XXXXXX)
echo "$after" > "$tmpfile"
# make sure
mkdir -p "${HOME}/.kube"
targetfile="${HOME}/.kube/config"
if [[ -r "$targetfile" ]] && [[ -f "$targetfile" ]]; then
cp "$targetfile" "$targetfile".bak
echo "Current config will back up to $targetfile.bak"
fi
cp "$tmpfile" "$targetfile"
echo "Replace complete"
rm "$tmpfile"
else
echo "No change"
fi