-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmblog.sh
executable file
·149 lines (125 loc) · 3.01 KB
/
mblog.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
#!/bin/sh
set -e
# POSIX Bourne Shell to support as many clients as possible
help() {
cat <<-EOF
usage: mblog.sh create COHORT
example:
mblog.sh create text/luke
EOF
}
create() {
[ $# -gt 0 ] || withusage fail 'too few arguments\n'
[ $# -lt 2 ] || withusage fail 'too many arguments\n'
path="${1%/}" # remove any trailing /
[ -d "$path" ] || withusage fail 'cohort "%s" not found\n' "$1"
IFS=- read -r name last <<-EOF
$(find "$path" -type d -name '*[0-9]' -prune -print \
| sed 's,.*/,,' | sort -t - -k 2 -n | tail -n1)
EOF
[ -n "$name" ] || name=$(basename "$path")
next=$(( ${last:-0} + 1 ))
dir="$path/$name-$next"
dryrun mkdir "$dir"
mdfile | tofile "$dir/index.md"
ednfile \
"$(date +%Y-%m-%d)" \
"$(uuid4)" \
"$(git config user.email)" \
| tofile "$dir/meta.edn"
[ -n "$DRYRUN" ] || printf 'created %s\n' "$dir/index.md"
[ -n "$DRYRUN" ] || [ -z "$EDITOR" ] || "$EDITOR" "$dir/index.md"
}
mdfile() {
cat <<-EOF
<!-- Hva gjør du akkurat nå? -->
<!-- Finner du kvalitet i det? -->
<!-- Hvorfor / hvorfor ikke? -->
<!-- Call to action—hva ønsker du kommentarer på fra de som leser? -->
EOF
}
ednfile() {
cat <<-EOF
{:doc/created "$1",
:doc/uuid "$2",
:git.user/email "$3"}
EOF
}
uuid4() {
awk 'BEGIN {
srand()
variant = substr("89ab", 1 + int(rand() * 4), 1)
print f(8) "-" f(4) "-4" f(3) "-" variant f(3) "-" f(12)
}
function f(len) {
r = ""
while (len-- > 0) {
n = int(rand() * 16)
r = r sprintf("%c", (n > 9 ? n+87 : n+48))
}
return r
}'
}
fail() {
[ -t 1 ] && notty=false || notty=true
{
$notty || printf '\033[31m' # red
# shellcheck disable=SC2059 # be careful with %
printf "$@"
$notty || printf '\033[m' # reset
} >&2
return 1
}
withusage() {
rc=0
"$@" || rc=$?
help
return $rc
}
tofile() {
if [ -n "$DRYRUN" ]; then
printf '> %s\n' "$1"
cat
else
cat > "$1"
fi
}
dryrun() {
if [ -n "$DRYRUN" ]; then
set -- echo "$@"
fi
"$@"
}
test() {
# The variant field consists of a variable number of
# the most significant bits of octet 8 of the UUID. —RFC 4122
# 1 0 _ _ => 1 0 0 0 .. 1 0 1 1 => 8..11 => [89ab]
pattern='^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
for i in $(seq 20); do
value=$(uuid4)
printf %s "$value" | grep -Eq "$pattern" \
|| fail 'uuid %s did not match pattern %s\n' "$value" "$pattern"
done
echo OK
}
# shellcheck disable=SC2086 # integers only
seq() { # [first] last
last=${2:-$1}
if [ $# -gt 1 ]; then i=$1; else i=1; fi
while [ $i -le $last ]; do
printf '%d\n' $i
i=$((i + 1))
done
}
case "$1" in
create|help|uuid4|test)
"$@"
;;
"")
help
exit 1
;;
*)
withusage fail 'unknown argument: %s\n' "$1"
;;
esac