-
Notifications
You must be signed in to change notification settings - Fork 0
/
grind
executable file
·132 lines (98 loc) · 2.67 KB
/
grind
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
#!/bin/bash
# This is a command trampoline for commands in '$REPO/commands/'
# The goal is to allow us to seperate repository commands from repository code;
# so that actions we may wish to take can be put in a tree organized by the actions;
# and documented appropriately.
# We guarantee that subcommands will have access to:
# - WORKSPACE_ROOT_DIR
# - WORKSPACE_VENV_DIR
# - GRIND
# - MARKDOWN_READER
# Standard trick to resolve the 'real' location of
# the directory this file is run from.
export WORKSPACE_ROOT_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)"
# Where do we keep the python venv?
export WORKSPACE_VENV_DIR="$WORKSPACE_ROOT_DIR/.venv"
# If we want to re-invoke this target; where is it?
export GRIND="$WORKSPACE_ROOT_DIR/grind"
COMMAND_DIR="$WORKSPACE_ROOT_DIR/commands"
if [ -z "$MARKDOWN_READER" ]; then
if command -v glow &> /dev/null; then
export MARKDOWN_READER="glow -w 80"
else
export MARKDOWN_READER=${PAGER:="less"}
fi
fi
function group_help() {
local group_path="$1"
(
cd "$WORKSPACE_ROOT_DIR/$group_path"
cat
echo "# grind $group_path"
if [ -f README.md ]; then
# Directory README
cat README.md
fi
local cmds=( $(find . -mindepth 1 -maxdepth 1 -type f | (while read f; do if [ -x "$f" ]; then echo "$f"; fi; done) | sort | sed 's/^\.\///') )
if (( ${#cmds[@]} )); then
echo "# Commands:"
for cmd in "${cmds[@]}"; do
echo -n " * \`$cmd\`"
local readme="$cmd.md"
if [ -f "$readme" ]; then
echo -n " - "
head -1 "$readme"
else
echo
fi
done
for cmd in "${cmds[@]}"; do
echo "# Command: \`$cmd\`"
if [ -f "$cmd.md" ]; then
echo
cat "$cmd.md"
fi
done
fi
local groups=( $(find . -mindepth 1 -maxdepth 1 -type d | sort | sed 's/^\.\///') )
if (( ${#groups[@]} )); then
echo "# Sub-Groups:"
for g in ${groups[@]}; do
echo -n " * \`${g}\`"
local sub_readme="$g/README.md"
if [ -f "$sub_readme" ]; then
echo -n " - "
head -1 "$sub_readme"
else
echo
fi
done
fi
) | $MARKDOWN_READER -
}
function main() {
local group_path='commands'
local target=''
while true; do
if [ "$#" -eq "0" ]; then
echo -n | group_help "$group_path";
exit 0;
fi
target="$1"
shift;
if [ -d "$WORKSPACE_ROOT_DIR/$group_path/$target" ]; then
group_path="$group_path/$target"
else
break;
fi
done
local cmd_path="$group_path/$target"
if [ -x "$cmd_path" ]; then
exec "$cmd_path" "$@";
else
echo "*ERROR: No Such Command: $cmd_path*" \
| group_help "$group_path" 1>&2;
exit 1;
fi
}
main "$@"