forked from SierraSoftworks/bash-cli
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli
More file actions
executable file
·169 lines (138 loc) · 5.1 KB
/
cli
File metadata and controls
executable file
·169 lines (138 loc) · 5.1 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env bash
# Minimum Bash version. Centos 7 Docker container comes with 4.2.46(2)-release,
# and we will use that as the standard.
MINIMAL_BASH_VERSION="4.2.0"
# check_dependencies returns non-zero code when one or more dependencies are
# not fulfilled
check_dependencies() {
_check_bash_version
_check_coreutils
_check_grep
}
# _check_bash_version returns non-zero code when the bash version used is
# too old
_check_bash_version() {
local lowest_vesion=$(echo -e "$BASH_VERSION\n$MINIMAL_BASH_VERSION" | sort -V | head -n1)
if [ "$lowest_vesion" == "$BASH_VERSION" ]; then
echo "Installed Bash version is too old (installed=$BASH_VERSION," \
"required=$MINIMAL_BASH_VERSION)" >&2
if [ $(uname -s) == "Darwin" ]; then
echo "You can install newer Bash on Mac OS with:" >&2
echo " $ brew install bash" >&2
echo " $ sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'" >&2
echo " $ chsh -s /usr/local/bin/bash " >&2
fi
return 1
fi
}
# _check_coreutils returns non-zero code if coreutils is not installed via APT
# or brew
_check_coreutils() {
local readlink_bin="readlink"
if [ $(uname -s) == "Darwin" ]; then
readlink_bin="greadlink"
fi
# If readlink or greadlink is not available, then coreutils is not installed
if ! which "$readlink_bin" > /dev/null; then
echo "System package \"coreutils\" is not installed." \
"You can install coreutils with:" >&2
if [ $(uname -s) == "Linux" ]; then
echo " $ sudo apt-get install coreutils" >&2
elif [ $(uname -s) == "Darwin" ]; then
echo " $ brew install coreutils" >&2
fi
return 1
fi
}
# _check_grep returns non-zero code if GNU grep is not installed via brew in
# Mac OS
_check_grep() {
if [ $(uname -s) != "Darwin" ]; then
return 0
fi
if ! which ggrep > /dev/null; then
echo "System package GNU grep is not installed." \
"You can install GNU grep with:" >&2
echo " $ brew install grep" >&2
return 1
fi
}
if ! check_dependencies; then
echo "BCL dependencies not installed" >&2
exit 1
fi
# _readlink wraps readlink in Linux or greadlink in Mac OS. We need this here
# since we can't import app/.helper/system yet.
_readlink() {
if [ $(uname -s) == "Linux" ]; then
readlink "$@"
return $?
elif [ $(uname -s) == "Darwin" ]; then
greadlink "$@"
return $?
fi
echo "Invalid kernel name $(uname -s)" && return 1
}
export ROOT_DIR=`dirname "$(_readlink -f $0)"`
CLI_ENTRYPOINT=`basename $0`
. "$ROOT_DIR/utils"
# Locate the correct command to execute by looking through the app directory
# for folders and files which match the arguments provided on the command line.
CMD_FILE="$ROOT_DIR/app/"
CMD_ARG_START=1
while [[ -d "$CMD_FILE" && $CMD_ARG_START -le $# ]]; do
# If the user provides help as the last argument on a directory, then
# show them the help for that directory rather than continuing
if [[ "${!CMD_ARG_START}" == "help" ]]; then
# Strip off the "help" portion of the command
ARGS=("$@")
unset "ARGS[$((CMD_ARG_START-1))]"
ARGS=("${ARGS[@]}")
"$ROOT_DIR/help" $0 ${ARGS[@]}
exit 3
fi
CMD_FILE="$CMD_FILE/${!CMD_ARG_START}"
CMD_ARG_START=$(($CMD_ARG_START+1))
done
# Place the arguments for the command in their own list
# to make future work with them easier.
CMD_ARGS=("${@:CMD_ARG_START}")
# If we hit a directory by the time we run out of arguments, then our user
# hasn't completed their command, so we'll show them the help for that directory
# to help them along.
if [ -d "$CMD_FILE" ]; then
"$ROOT_DIR/help" $0 $@
exit 3
fi
# If we didn't couldn't find the exact command the user entered then warn them
# about it, and try to be helpful by displaying help for that directory.
if [[ ! -f "$CMD_FILE" ]]; then
"$ROOT_DIR/help" $0 ${@:1:$(($CMD_ARG_START-1))}
>&2 echo -e "\033[31mWe could not find the command \033[36m$CLI_ENTRYPOINT ${@:1:$CMD_ARG_START}\033[39m"
>&2 echo -e "To help out, we've shown you the help docs for \033[36m$CLI_ENTRYPOINT ${@:1:$(($CMD_ARG_START-1))}\033[39m"
exit 3
fi
# If --help is passed as one of the arguments to the command then show
# the command's help information.
arg_i=0 # We need the index to be able to strip list indices
for arg in "${CMD_ARGS[@]}"; do
if [[ "${arg}" == "--help" ]]; then
# Strip off the `--help` portion of the command
unset "CMD_ARGS[$arg_i]"
CMD_ARGS=("${CMD_ARGS[@]}")
# Pass the result to the help script for interrogation
"$ROOT_DIR/help" $0 ${@:1:$((CMD_ARG_START - 1))} ${CMD_ARGS[@]}
exit 3
fi
arg_i=$((arg_i+1))
done
# Run the command and capture its exit code for introspection
"$CMD_FILE" ${CMD_ARGS[@]}
EXIT_CODE=$?
# If the command exited with an exit code of 3 (our "show help" code)
# then show the help documentation for the command.
if [[ $EXIT_CODE == 3 ]]; then
"$ROOT_DIR/help" $0 $@
fi
# Exit with the same code as the command
exit $EXIT_CODE