forked from ro14nd-talks/shell-ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdeploy
executable file
·119 lines (95 loc) · 2.54 KB
/
kdeploy
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
# Set options:
# Fail on a single failed command in a pipeline
set -o pipefail
# Save global script args
ARGS="$@"
# Fail on error and undefined vars (please don't use global vars, but
# evaluation of functions for return values)
set -eu
# Main loop evaluating sub commands
run() {
local first_arg=${1:-}
local cmd_dir="$(basedir)/commands"
local command
if [ -n "$first_arg" ] && [[ ${first_arg} != -* ]]; then
command="$first_arg"
if [ ! -f "$cmd_dir/$command" ]; then
echo
echo ">>>> Unknown command '$command'"
echo
display_help
exit 1
fi
else
command="help"
echo "No command given"
echo
fi
if [ "${command}" = "help" ] || $(hasflag --help -h); then
display_help ${command:-}
exit 0
fi
source "$cmd_dir/$command"
eval "${command}::run"
}
display_help() {
local command=${1:-}
local cmd_dir="$(basedir)/commands"
if [ -z "${command}" ] || [ "$command" = "help" ]; then
cat << EOT
Usage: kdeploy <subcommand> <opts>
Kubernetes deployment helper
Commands:
EOT
for cmd in $(ls $cmd_dir); do
if [ -f $cmd_dir/$cmd ]; then
source $cmd_dir/$cmd
printf " %-15s %s\n" $cmd "$($cmd::description)"
fi
done
printf " %-15s %s\n" "help" "Print this help message"
else
source $cmd_dir/$command
cat <<EOT
$($command::description)
Usage: kdeploy $command [... options ...]
EOT
echo "Options for $command:"
echo -e "$($command::usage)"
fi
cat <<EOT
Global Options:
-h --help Print this help message
EOT
}
# Directory where this script is located
basedir() {
# Default is current directory
local script=${BASH_SOURCE[0]}
# Resolve symbolic links
if [ -L $script ]; then
if readlink -f $script >/dev/null 2>&1; then
script=$(readlink -f $script)
elif readlink $script >/dev/null 2>&1; then
script=$(readlink $script)
elif realpath $script >/dev/null 2>&1; then
script=$(realpath $script)
else
echo "ERROR: Cannot resolve symbolic link $script"
exit 1
fi
fi
local dir=$(dirname "$script")
local full_dir=$(cd "${dir}" && pwd)
echo ${full_dir}
}
# ===========================================================
# Startup ...
# Read in some helpers
source $(basedir)/helpers.sh
if $(hasflag --verbose -v); then
export PS4='+($(basename ${BASH_SOURCE[0]}):${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -x
fi
run $ARGS