-
Notifications
You must be signed in to change notification settings - Fork 8
/
manuel
executable file
·148 lines (121 loc) · 2.82 KB
/
manuel
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
#! /usr/bin/env bash
# __ __ _
# | \/ | | |
# | \ / | __ _ _ __ _ _ ___| |
# | |\/| |/ _` | '_ \| | | |/ _ \ |
# | | | | (_| | | | | |_| | __/ |
# |_| |_|\__,_|_| |_|\__,_|\___|_|
# Usage: manuel [task]
# -- # -- # -- # -- # -- # -- # -- #
# Setup
# -- #
# Set IFS to just newline and tab.
IFS="$(printf '\n\t')"
# -- # -- # -- # -- # -- # -- # -- #
# Globals
# -- #
MANUEL_VERSION="1.0.0"
MANUEL_DIR=$(
[[ ! -z "$MANUEL_DIR" ]] && echo "$MANUEL_DIR" || echo "$HOME/.manuel.d"
)
MANUEL_PLUGIN_DIR="$MANUEL_DIR/plugins"
# -- # -- # -- # -- # -- # -- # -- #
# Utility functions
# -- #
function _manuel_usage {
cat <<EOF
__ __ _
| \/ | | |
| \ / | __ _ _ __ _ _ ___| |
| |\/| |/ _\` | '_ \| | | |/ _ \ |
| | | | (_| | | | | |_| | __/ |
|_| |_|\__,_|_| |_|\__,_|\___|_|
Version: $MANUEL_VERSION
Usage: manuel [task] [params]
Default Tasks:
- init: create a fresh manuelfile in this directory
- list: list available tasks
EOF
exit 1
}
# bootstrap required file and folders for manuel
function _bootstrap {
if [[ ! -d $MANUEL_PLUGIN_DIR ]]; then
echo ">> Initializing plugin directory $plugin_dir"
mkdir -p "$MANUEL_PLUGIN_DIR"
if [[ $? != "0" ]]; then
echo ">> Could not create plugin directory $plugin_dir, exiting"
exit 1
fi
fi
}
# -- # -- # -- # -- # -- # -- # -- #
# Built-in tasks
# -- #
function load_plugin {
plugin_name=$1
if [[ -z $plugin_name ]]; then
echo ">> Warning: load_plugin called with no argument"
return
fi
plugin_file=$(
find -L "$MANUEL_PLUGIN_DIR" -type f -name "$plugin_name.manuel" | head -1
)
if [[ -z $plugin_file ]]; then
echo ">> Warning: plugin $plugin_name not found in $MANUEL_PLUGIN_DIR"
else
source "$plugin_file"
fi
}
function list {
tasks=($(declare -F))
for t in "${tasks[@]}"
do
local task_name=$(printf "%s" "$t" | awk '{ print $3 }')
if ! [[ "$task_name" =~ ^_(.*) ]]; then
echo "$task_name"
fi
done
}
function init {
if [[ -f manuelfile ]]; then
echo ">> manuelfile already exists, nothing to do"
exit 0
else
echo ">> Creating new manuelfile in $(pwd)"
echo '#! /usr/bin/env bash
# run this task with: $ manuel hello
function hello {
echo ">> Hello from manuel!"
}
' >> manuelfile
exit 0
fi
}
# -- # -- # -- # -- # -- # -- # -- #
# Main function
# -- #
function _manuel_main {
if [[ $# = 0 ]]; then
_manuel_usage
fi
_bootstrap
if [[ "$1" == "init" ]]; then
init
fi
if [[ "$1" == "ke" || "$1" == "que" ]]; then
echo ">> Que?"
_manuel_usage
fi
if [[ -f manuelfile ]]; then
source manuelfile
"$@"
exit 0
else
echo -e "No manuelfile found, exiting"
exit 1
fi
exit 0
}
# execute main function
_manuel_main "$@"