/
new-post
executable file
·76 lines (64 loc) · 1.49 KB
/
new-post
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
#! /bin/bash
verbose=false
log() {
if $verbose
then echo $@
fi
}
directory_exists() {
if test -d $1; then
log "Directory $1 exists."
return 0;
fi
return 1;
}
open_with_vscode() {
if command -v code &> /dev/null
then code $1
fi
}
template() {
cat <<EOF
---
title:
excerpt:
modified:
tags: []
image:
path:
thumbnail:
date: $1
---
EOF
}
main() {
while [[ "$#" -gt 0 ]]; do
case $1 in
-t|--type) type="$2"; shift ;;
-n|--name) name="$2"; shift ;;
-v|--verbose) verbose=true ;;
*) echo "Unknown parameter passed in: $1"; exit 1 ;;
esac
shift
done
if [ -z "$type" ] || [ -z "$name" ]; then
echo "Must provide params name (-n) and type (-t)" 1>&2
exit 1
fi
local directory="_posts/$type"
if directory_exists $directory;
then
local today=$(TZ=America/Los_Angeles date '+%Y-%m-%d')
log "today: $today, type: $type, verbose: $verbose"
local filename="$today-$name.md"
local filepath="$directory/$filename"
log "Creating $filename in $directory."
template $today > $filepath
open_with_vscode $filepath
exit 0
else
echo "Directory $directory not found. Please specify either one of the following: [$(ls _posts/ | tr "\n" "\t")]"
exit 1
fi
}
main $@