-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheli.sh
144 lines (132 loc) · 4.24 KB
/
sheli.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
#!/bin/bash
"${__SHELI__LOADED-false}" && return # If loaded, do not load again
"${__SHELI__LOADING-false}" && return 70 # If stuck loading, something is wrong
export __SHELI__LOADING=true
# Copy of stdout to permit functions to both return values and print in stdout
exec 8<&1
####################
# XXX
# It is possible to return values like this:
# funct() {
# printf 'stdout\n'
# printf 'stderr\n' >&2
# printf 'value\n' >&9
# } 9>&1 >&8
# var="$(funct)"
####################
########################################
# print__error()
# Fallback substitute of print.print__error
########################################
if ! command -v print__error >/dev/null; then
print__error() {
{
printf '%s: error: ' "${BIN_NAME}"
printf "${@}"
printf '%b' '\n'
} >&2
}
fi
########################################
# sheli__main()
# Everything starts from here
########################################
sheli__main() {
argparse__parse "${@}"
if "${usage}"; then
argparse__usage && return "${EX_OK}"
fi
if "${help}"; then
argparse__help && return "${EX_OK}"
fi
font__set "${color}"
argparse__opts
argparse__args
local args="$(printf '%s' "${args%?}" | sed -e "s/'/'\\\\&'/g")"
# FIXME? Is there something better than eval?
eval set -- "$(printf '%s' "${args:+"'${args}'"}" | sed -e "s/\\${FS}/' '/g")"
if command -v print__debug >/dev/null; then
print__debug '%s initiated %s as: %s' "${BIN_NAME}" "$(date)" "${CMD}"
fi
if command -v main >/dev/null; then # Check if main exists
main "${@}" || return "${?}"
else # if not
print__error 'main() function is missing.'
exit "${EX_SOFTWARE}" # error
fi
if command -v print__debug >/dev/null; then
print__debug '%s ended %s' "${BIN_NAME}" "$(date)"
fi
}
########################################
# __sheli__import_core()
# Import all the core libraries
########################################
__sheli__import_core() {
# Core libraries are those libs that make sheli boot properly
local sheli_core_dir="${SHELI_DIR}/core"
. "${sheli_core_dir}/sysexits.sh"
. "${sheli_core_dir}/behaviour.sh"
. "${sheli_core_dir}/magic.sh"
. "${sheli_core_dir}/trap.sh"
. "${sheli_core_dir}/dep.sh"
. "${sheli_core_dir}/font.sh"
}
########################################
# __sheli__import_ess()
# Import all the essential libraries
########################################
__sheli__import_ess() {
# Essential libraries are those libs that make sheli work as intended
local sheli_ess_dir="${SHELI_DIR}/ess"
. "${sheli_ess_dir}/debug.sh"
. "${sheli_ess_dir}/print.sh"
. "${sheli_ess_dir}/argparse.sh"
. "${sheli_ess_dir}/time.sh"
}
########################################
# __sheli__import_util()
# Import all the utility libraries
########################################
__sheli__import_util() {
# Utility libraries are those libs that add features to sheli
local sheli_util_dir="${SHELI_DIR}/util"
. "${sheli_util_dir}/test.sh"
. "${sheli_util_dir}/check.sh"
. "${sheli_util_dir}/config.sh"
. "${sheli_util_dir}/math.sh"
. "${sheli_util_dir}/cast.sh"
. "${sheli_util_dir}/override.sh"
}
__sheli__load() {
export __SHELI_LIB__LOADING='sheli'
local EX_SOFTWARE_=70
# Check sheli root folder
if [ -z "${SHELI_DIR}" ]; then
printf '%s: error: var $SHELI_DIR not set\n' "${__SHELI_LIB__LOADING}.sh" >&2
return "${EX_SOFTWARE_}"
elif ! [ -f "${SHELI_DIR}/sheli.sh" ]; then
printf '%s: error: var $SHELI_DIR not properly set\n' "${__SHELI_LIB__LOADING}.sh" >&2
return "${EX_SOFTWARE_}"
fi
# From now on, sheli root folder is known
# Need to import some things...
__sheli__import_core "${@}"
# From now on, it is possible to declare:
# trap__int()
# trap__quit()
# trap__term()
# trap__cleanup()
# trap__die()
__sheli__import_ess
# From now on, it is possible to declare arguments like python's argparse
# XXX
# argparse__add_argument name='varname' choices='value1,value2'
# argparse__add_argument name='varname' nargs=? const='value1'
# argparse__add_argument name='varname' action='store_true'
__sheli__import_util
unset __SHELI_LIB__LOADING
}
__sheli__load "${@}" || exit "${?}"
export __SHELI__LOADING=false
export __SHELI__LOADED=true