-
Notifications
You must be signed in to change notification settings - Fork 0
/
shoe-xxs.sh
executable file
·221 lines (192 loc) · 9.93 KB
/
shoe-xxs.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/sh
set -e
#/*
# * This script is based on TangoMan Shoe Shell Microframework version 0.12.0-xxs
# *
# * This file is distributed under to the MIT license.
# *
# * Copyright (c) 2024 "Matthias Morin" <[email protected]>
# *
# * Permission is hereby granted, free of charge, to any person obtaining a copy
# * of this software and associated documentation files (the "Software"), to deal
# * in the Software without restriction, including without limitation the rights
# * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# * copies of the Software, and to permit persons to whom the Software is
# * furnished to do so, subject to the following conditions:
# *
# * The above copyright notice and this permission notice shall be included in all
# * copies or substantial portions of the Software.
# *
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# * SOFTWARE.
# *
# * Source code is available here: https://github.com/TangoMan75/shoe
# */
#/**
# * TangoMan Shoe XXS
# *
# * Create self documented shell scripts with argument validation
# * easily. Prefix a comment with two pound signs on the previous
# * line of any function or variable you want documented with the
# * "help" command.
# *
# * XXS version is optimized for performance,
# * does not handle flags, options, validation or docbloc parsing.
# *
# * Features:
# * - [X] colors
# * - [X] commands
# * - [X] help
# * - [ ] docbloc
# * - [ ] parameters
# * - [ ] validation
# * - [ ] flags
# * - [ ] install
# * - [ ] autocomplete
# * - [ ] generate_doc
# *
# * Warning: Functions names should not contain dashes.
# *
# * @author "Matthias Morin" <[email protected]>
# * @version 0.12.0-xxs
# * @link https://github.com/TangoMan75/shoe
# */
#--------------------------------------------------
# Place your constants after this line
#--------------------------------------------------
WHO=World
#--------------------------------------------------
# Place your functions after this line
#--------------------------------------------------
## Say hello to the world
hello() {
echo "Hello ${WHO} !"
}
#--------------------------------------------------
# Place your private functions after this line
#--------------------------------------------------
## Private functions are prefixed with underscore, they cannot be called from the command line and will not appear in the help section.
_private() {
echo "I'm private"
}
## Place here commands you need executed first every time (optional)
_before() {
true
}
## Place here commands you need executed last every time (optional)
_after() {
true
}
############################################################
# TangoMan Shoe Shell Microframework version 0.12.0-xxs
############################################################
#--------------------------------------------------
# Semantic colors set
#--------------------------------------------------
# shellcheck disable=SC2034
{
PRIMARY='\033[97m'; SECONDARY='\033[94m'; SUCCESS='\033[32m'; DANGER='\033[31m'; WARNING='\033[33m'; INFO='\033[95m'; LIGHT='\033[47;90m'; DARK='\033[40;37m'; DEFAULT='\033[0m'; EOL='\033[0m\n';
ALERT_PRIMARY='\033[1;104;97m'; ALERT_SECONDARY='\033[1;45;97m'; ALERT_SUCCESS='\033[1;42;97m'; ALERT_DANGER='\033[1;41;97m'; ALERT_WARNING='\033[1;43;97m'; ALERT_INFO='\033[1;44;97m'; ALERT_LIGHT='\033[1;47;90m'; ALERT_DARK='\033[1;40;37m';
}
# Synopsys: echo_* [string] (indentation) (padding)
echo_primary() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${PRIMARY}" "$3" "$1" "${DEFAULT}"; }
echo_secondary() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${SECONDARY}" "$3" "$1" "${DEFAULT}"; }
echo_success() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${SUCCESS}" "$3" "$1" "${DEFAULT}"; }
echo_danger() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${DANGER}" "$3" "$1" "${DEFAULT}"; }
echo_warning() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${WARNING}" "$3" "$1" "${DEFAULT}"; }
echo_info() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${INFO}" "$3" "$1" "${DEFAULT}"; }
echo_light() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${LIGHT}" "$3" "$1" "${DEFAULT}"; }
echo_dark() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${DARK}" "$3" "$1" "${DEFAULT}"; }
echo_error() { if [ $# -eq 1 ]; then set -- "$1" 0 0; elif [ $# -eq 2 ]; then set -- "$1" "$2" 0; fi; printf "%*b%b%-*b%b" "$2" '' "${DANGER}" "$3" "error: $1" "${DEFAULT}"; }
alert_primary() { printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_PRIMARY}" '' "${ALERT_PRIMARY}" "$1" "${ALERT_PRIMARY}" ''; }
alert_secondary() { printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_SECONDARY}" '' "${ALERT_SECONDARY}" "$1" "${ALERT_SECONDARY}" ''; }
alert_success() { printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_SUCCESS}" '' "${ALERT_SUCCESS}" "$1" "${ALERT_SUCCESS}" ''; }
alert_danger() { printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_DANGER}" '' "${ALERT_DANGER}" "$1" "${ALERT_DANGER}" ''; }
alert_warning() { printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_WARNING}" '' "${ALERT_WARNING}" "$1" "${ALERT_WARNING}" ''; }
alert_info() { printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_INFO}" '' "${ALERT_INFO}" "$1" "${ALERT_INFO}" ''; }
alert_light() { printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_LIGHT}" '' "${ALERT_LIGHT}" "$1" "${ALERT_LIGHT}" ''; }
alert_dark() { printf "${EOL}%b%64s${EOL}%b %-63s${EOL}%b%64s${EOL}\n" "${ALERT_DARK}" '' "${ALERT_DARK}" "$1" "${ALERT_DARK}" ''; }
#--------------------------------------------------
# Self documentation
#--------------------------------------------------
### Help
## Print this help (default)
help() {
alert_primary 'TangoMan Shoe XXS'
echo_warning 'Infos:\n'
echo_success 'author' 2 10; echo_primary '"Matthias Morin" <[email protected]>\n'
echo_success 'version' 2 10; echo_primary '0.12.0-xxs\n'
echo_success 'link' 2 10; echo_primary 'https://github.com/TangoMan75/shoe\n\n'
echo_warning 'Description:\n'
echo_primary ' Create self documented shell scripts with argument validation easily. Prefix a comment with two pound signs on the previous line of any function or variable you want documented with the "help" command.\n\n' | fold -w 64 -s
echo_warning 'Usage:\n'
printf "%b sh $(basename "${0}") [%bcommand%b]\n\n" "${INFO}" "${SUCCESS}" "${INFO}"
_print_commands 10
}
#--------------------------------------------------
_print_commands() {
# $1 = padding
if [ -z "$1" ]; then set -- 12; fi
printf "%bCommands:%b\n" "${WARNING}" "${DEFAULT}"
awk "/^### /{printf\"\n${WARNING}%s:${EOL}\",substr(\$0,5)}
/^(function )? *[a-zA-Z0-9_]+ *\(\) *\{/ {
sub(\"^function \",\"\"); gsub(\"[ ()]\",\"\");
FUNCTION = substr(\$0, 1, index(\$0, \"{\"));
sub(\"{\$\", \"\", FUNCTION);
if (substr(PREV, 1, 3) == \"## \" && substr(\$0, 1, 1) != \"_\")
printf \"${SUCCESS} %-$1s ${DEFAULT}%s\n\", FUNCTION, substr(PREV, 4)
} { PREV = \$0 }" "$0"
printf '\n'
}
#--------------------------------------------------
# Reflexion
#--------------------------------------------------
_get_functions_names() {
if [ -z "$1" ]; then echo_error 'some mandatory parameter is missing.\n'; return 1; fi
# this regular expression matches functions with either bash or sh syntax
awk '/^(function )? *[a-zA-Z0-9_]+ *\(\) *\{/ {
sub("^function ",""); gsub("[ ()]",""); # remove leading "function ", round brackets and extra spaces
FUNCTION = substr($0, 1, index($0, "{")); # truncate string after opening curly brace
sub("{$", "", FUNCTION); # remove trailing curly brace
if (substr(PREV, 1, 3) == "## " && substr($0, 1, 1) != "_") print FUNCTION
} { PREV = $0 }' "$1"
}
#--------------------------------------------------
# Main loop
#--------------------------------------------------
_main() {
if [ $# -lt 1 ]; then
help
exit 0
fi
_execute=''
for _argument in "$@"; do
_is_valid=false
for _function in $(_get_functions_names "$0"); do
# get shorthand character
_shorthand="$(printf '%s' "${_function}" | awk '{$0=substr($0, 1, 1); print}')"
if [ "${_argument}" = "${_function}" ] || [ "${_argument}" = "${_shorthand}" ]; then
# append argument to the execute stack
_execute="${_execute} ${_function}"
_is_valid=true
break
fi
done
# unknown parameters will raise errors
if [ "${_is_valid}" = false ]; then
printf "${DANGER}error: \"%s\" is not a valid command${EOL}" "${_argument}"
exit 1
fi
done
if [ -n "$(command -v _before)" ]; then _before; fi
for _function in ${_execute}; do
eval "${_function}"
done
if [ -n "$(command -v _after)" ]; then _after; fi
}
_main "$@"