-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_post.sh
executable file
·49 lines (39 loc) · 939 Bytes
/
new_post.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
#!/usr/bin/env bash
set -euo pipefail
function main {
if [[ "${#}" -eq 1 ]]; then
output_dir="./_posts"
title="${1}"
elif [[ "${#}" -eq 2 && -d "${1}" ]]; then
output_dir=$(realpath "${1}")
title="${2}"
else
die "USAGE: ./new_post.sh (<title>) | (<existing dir> <title>)"
fi
current_date=$(date +"%Y-%m-%d")
current_time=$(date +"%H:%M:%S")
new_post_content=(
"---"
"layout: post"
"title: ${title}"
"date: ${current_date} ${current_time} -0600"
"categories: "
"---"
)
printf '%s\n' "${new_post_content[@]}" > "${output_dir}/${current_date}-${title}.markdown"
}
function installed {
cmd=$(command -v "${1}")
[[ -n "${cmd}" ]] && [[ -f "${cmd}" ]]
return ${?}
}
function die {
>&2 echo "Fatal: ${@}"
exit 1
}
# Check for all required dependencies
deps=(realpath)
for dep in "${deps[@]}"; do
installed "${dep}" || die "Missing '${dep}'"
done
main "$@"; exit