-
Notifications
You must be signed in to change notification settings - Fork 6
/
update_readme
executable file
·65 lines (50 loc) · 1.4 KB
/
update_readme
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
#!/bin/bash
set -euo pipefail
ROOT="$(git rev-parse --show-toplevel)"
rdm:usage() {
cat <<EOF
Usage: $(basename "$0") FILE
Updates a given markdown file by injecting the output of \`go run main.go --help\` into it.
Arguments:
FILE Markdown file to update.
EOF
}
# Updates a given markdown file by running a number of injectors on it.
# Args:
# $1: Markdown file to update.
rdm:update() {
if [[ $# -ne 1 ]]; then
rdm:usage
exit 1
fi
local file="${1:?Specify file}"
local tmp_file
tmp_file="$(mktemp --suffix='.md')"
cp "$file" "$tmp_file"
rdm:inject_usage "$tmp_file"
mv "$tmp_file" "$file"
}
# Injects the output of `go run main.go --help` into the provided markdown file.
# Args:
# $1: Markdown file to update.
rdm:inject_usage() {
local begin_marker='<!-- BEGIN CFDDNS_USAGE -->'
local end_marker='<!-- END CFDDNS_USAGE -->'
local file="${1:?Specify file}"
local tmp_file
tmp_file="$(mktemp --suffix='.usage.md')"
sed -n "1,/$begin_marker/p" "$file" >"$tmp_file"
echo '<pre>' >>"$tmp_file"
(
cd "$ROOT"
go run main.go --help >>"$tmp_file"
)
echo '</pre>' >>"$tmp_file"
sed -n "/$end_marker/,\$p" "$file" >>"$tmp_file"
cp "$tmp_file" "$file"
rm "$tmp_file"
}
# Execute the main function if the script is not being sourced.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
rdm:update "$@"
fi