-
Notifications
You must be signed in to change notification settings - Fork 1
/
nman.sh
executable file
·172 lines (144 loc) · 2.83 KB
/
nman.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
#!/bin/bash
VERSION="0.0.2"
## sets optional variable from environment
opt () { eval "if [ -z "\${$1}" ]; then ${1}=${2}; fi"; }
opt TMPDIR "/tmp/"
opt CACHE_DIR="${TMPDIR}nman-cache"
opt OUTPUT="ronn"
opt API_URL "https://raw.githubusercontent.com/nodejs/node/main/doc/api"
opt DOC_EXT "md"
## output usage
usage () {
{
echo "usage: nman [-hV]"
echo ""
echo "examples:"
echo " $ nman fs"
echo " $ nman stream"
echo " $ nman buffer"
} >&2
}
## output error
error () {
{
printf "error: %s\n" "${@}"
} >&2
}
## main
nman () {
local mod="$1"
local let is_cached=0
local url="${API_URL}/${mod}.${DOC_EXT}"
local mdfile="${CACHE_DIR}/${mod}.${DOC_EXT}"
local manfile="${CACHE_DIR}/${mod}.man"
local md=""
local man=""
## ensure cache dir exists
if ! test -d "${CACHE_DIR}"; then
mkdir -p "${CACHE_DIR}"
fi
## if it exists in cache dir then
## use it with output method
if test -f "${mdfile}" && test -f "${manfile}"; then
md="$(cat ${mdfile})"
is_cached=1
else
## fetch new copy
md="$(curl -s -L "${url}")"
fi
## fail on bad rc
if [ "$?" -gt "0" ]; then
return 1
fi
## fail on empty buf
if [ -z "${md}" ]; then
return 1
fi
if [ "0" == "${is_cached}" ]; then
## store cache
touch "${mdfile}"
{
echo "node - ${mod}"
echo "===================="
echo ""
echo "${md}"
} >> "${mdfile}"
fi
if [ "stdout" = "${OUTPUT_METHOD}" ]; then
cat "${mdfile}"
return 0
fi
if test -f "${manfile}"; then
man "${manfile}"
return $?
fi
## determine output method
case "${OUTPUT_METHOD}" in
ronn)
man="$(ronn -W -r --pipe ${mdfile} 2>/dev/null)"
;;
curl)
man="$(curl -s -# -F page=@${mdfile} http://mantastic.herokuapp.com)"
;;
*) return 1 ;;
esac
rm -f "${manfile}"
touch "${manfile}"
echo "${man}" >> "${manfile}"
man "${manfile}"
}
## feature test
features () {
case "${OUTPUT}" in
stdout)
OUTPUT_METHOD="stdout"
;;
curl)
OUTPUT_METHOD="curl"
;;
*)
OUTPUT_METHOD="ronn"
;;
esac
if [ "stdout" != "${OUTPUT}" ]; then
if ! type -f "${OUTPUT_METHOD}" > /dev/null 2>&1; then
OUTPUT_METHOD="curl"
if ! type -f curl > /dev/null 2>&1; then
error "Unable to determine a suitable output method"
exit -1
fi
fi
fi
}
## parse opts
{
while true; do
arg="$1"
if [ "" = "${arg}" ]; then
usage
exit 1
elif [ "-" != "${arg:0:1}" ]; then
break;
fi
case "${arg}" in
-V|--version)
echo "${VERSION}"
exit 0
;;
-h|--help)
usage
exit 0
;;
*)
error "Unknown option: \`${arg}'"
usage
exit 1
;;
esac
shift
done
}
## detect feature output
features
nman "$@"
exit $?