-
Notifications
You must be signed in to change notification settings - Fork 0
/
til-add
executable file
·77 lines (67 loc) · 2.01 KB
/
til-add
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
SCRIPT="$(readlink --canonicalize-existing "$0")"
SCRIPTPATH="$(dirname "$SCRIPT")"
SCRIPTNAME="$(basename "$SCRIPT")"
# Thanks https://dev.to/thiht/shell-scripts-matter :)
#/ Usage: til-add category title sourceURL
#/ Description: Create a new til in specified category
#/ Examples: til-add sysadmin "create encrypted partition" https://stackoverflow.com...
#/ Options:
#/ --help: Display this help message
#/ --version: Display programm version
#/ Version: 0.1.0
usage() { grep '^#/' "$0" | cut -c4- ; exit 0 ; }
version() { grep '^#/ Version:' "$0" | cut -c13- ; exit 0 ; }
expr "$*" : ".*--help" > /dev/null && usage
expr "$*" : ".*--version" > /dev/null && version
readonly LOG_FILE="/tmp/$SCRIPTNAME.log"
info() { echo " [INFO] $*" | tee -a "$LOG_FILE" >&2 ; }
warning() { echo " [WARNING] $*" | tee -a "$LOG_FILE" >&2 ; }
error() { echo " [ERROR] $*" | tee -a "$LOG_FILE" >&2 ; }
fatal() { echo " [FATAL] $*" | tee -a "$LOG_FILE" >&2 ; exit 1 ; }
# cleanup() {}
# Parse command line options.
# while getopts ab: OPT; do
# case "$OPT" in
# a)
# echo "set a"
# b)
# echo $OPTARG
# ;;
# \?)
# # getopts issues an error message
# usage
# exit 1
# ;;
# esac
# done
# Remove the switches we parsed above.
# shift `expr $OPTIND - 1`
sanitize() {
local string
string=$1
string="${string// /_}"
string="${string,,}"
echo $string
}
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
# trap cleanup EXIT
# Script goes here
[ $# -lt 3 ] && usage
category=$1
title=$2
source=$3
info "Creating '$title' in category '$category'"
category=$(sanitize $category)
title=$(sanitize $title)
info "Til dest: ${category}/${title}"
til_path=$(ruby scripts/create-page.rb "${category}/${title}" "${source}")
vim $til_path
ruby scripts/create-readme.rb
git add tils/${category}
git add .pages.yml README.md
git commit -m "add ${category}/${title}"
exit 0
fi