add validation and clean up code

This commit is contained in:
James Tomasino 2018-01-20 03:53:36 -05:00
parent f6fb03ed4b
commit c19a2f0a18
1 changed files with 50 additions and 31 deletions

81
burrow
View File

@ -1,13 +1,16 @@
#!/usr/bin/env bash
# v0.1.0
shopt -s extglob
version="v0.1.0"
configfiles="$HOME/.config/burrow/config $HOME/.config/burrow $HOME/.burrow"
editor=${EDITOR:-vi}
location_gopher="$HOME/gopher"
location_recipebox="${location_gopher}/recipebox"
location_phlog="${location_gopher}/phlog"
custom_editor=""
post_phlog_command=""
post_recipebox_command=""
git_commit=0
git_push=0
verbose=0
@ -37,6 +40,7 @@ function parseopts() {
;;
v) verbose=1 ;;
r) recipe=1 ;;
p) phlog=1 ;;
*)
echo "Invalid option. Try -h for help."
exit 1
@ -74,36 +78,46 @@ function update_gopher() {
sed --in-place='' "s/.*Last\ Updated:.*/ ==== Last Updated: $(date +"%B %d$(day_suffix), %Y") ====/" "${location_gopher}/gophermap"
}
function check_directory() {
if [[ ! -d "$1" ]]
then
echo "Missing directory: $1 aborting."
exit 1
fi
}
function recipe_new() {
read -e -p "What is the name of your recipe: " title
read -r -e -p "What is the name of your recipe: " title
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
title_slug=$(echo ${title} | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr A-Z a-z)
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
post_path="${location_recipebox}/$title_slug.txt"
if [[ -f $post_path ]]
then
echo "$post_path already exists"
$editor $post_path
$editor "$post_path"
else
if [[ -f "${location_recipebox}/.template" ]]
then
cat "${location_recipebox}/.template" > "$post_path"
else
echo "Creating $post_path"
echo "----------------------------------------" >> "$post_path"
echo "$title" >> "$post_path"
echo "----------------------------------------" >> "$post_path"
echo "" >> "$post_path"
echo "" >> "$post_path"
{
echo "Creating $post_path"
echo "----------------------------------------"
echo "$title"
echo "----------------------------------------"
echo ""
echo ""
} >> "$post_path"
fi
$editor $post_path
echo -e "0$title\t$(basename $post_path)\n$(cat "${location_recipebox}/gophermap")" > "${location_recipebox}/gophermap"
$editor "$post_path"
echo -e "0$title\t$(basename "$post_path")\n$(cat "${location_recipebox}/gophermap")" > "${location_recipebox}/gophermap"
sort -fo "${location_recipebox}/gophermap" "${location_recipebox}/gophermap"
update_gopher
@ -128,21 +142,21 @@ function recipe_new() {
}
function phlog_new() {
read -e -p "Enter a title for your post: " title
read -r -e -p "Enter a title for your post: " title
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
title_slug=$(echo ${title} | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr A-Z a-z)
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
post_dir="${location_phlog}/$(date +%Y%m%d)-$title_slug"
post_path="$post_dir/gophermap"
if [[ -f $post_path ]]
then
echo "$post_path already exists"
$editor $post_path
$editor "$post_path"
else
if [[ -f "${location_phlog}/.template" ]]
then
@ -150,27 +164,29 @@ function phlog_new() {
else
echo "Creating $post_path"
mkdir -p "$post_dir"
echo "----------------------------------------" >> "$post_path"
echo "$title" >> "$post_path"
echo "$(date +"%B %d$(day_suffix), %Y")" >> "$post_path"
echo "----------------------------------------" >> "$post_path"
echo "" >> "$post_path"
echo "" >> "$post_path"
echo "" >> "$post_path"
echo "Links:" >> "$post_path"
echo "" >> "$post_path"
{
echo "----------------------------------------"
echo "$title"
date +"%B %d$(day_suffix), %Y"
echo "----------------------------------------"
echo ""
echo ""
echo ""
echo "Links:"
echo ""
} >> "$post_path"
fi
$editor $post_path
$editor "$post_path"
links="$post_dir/links"
sed -n '/^Links:$/,$p' $post_path | tail -n +2 > $links
sed -i '/^Links:$/,$d' $post_path
sed -i 's/^/ /' $post_path
cat "$links" >> $post_path
sed -n '/^Links:$/,$p' "$post_path" | tail -n +2 > "$links"
sed -i '/^Links:$/,$d' "$post_path"
sed -i 's/^/ /' "$post_path"
cat "$links" >> "$post_path"
rm "$links"
echo -e "1$(date +%Y-%m-%d) - $title\t$(basename $post_dir)\n$(cat "${location_phlog}/gophermap")" > "${location_phlog}/gophermap"
echo -e "1$(date +%Y-%m-%d) - $title\t$(basename "$post_dir")\n$(cat "${location_phlog}/gophermap")" > "${location_phlog}/gophermap"
update_gopher
if [[ $post_phlog_command != "" ]]
@ -207,7 +223,6 @@ function main() {
if [[ -f $configfile ]]
then
source "$configfile"
echo "$configfile"
fi
done
@ -216,13 +231,17 @@ function main() {
editor=$custom_editor
fi
check_directory "$location_gopher"
if [[ ${recipe} -gt 0 ]]
then
check_directory "$location_recipebox"
recipe_new
fi
if [[ ${phlog} -gt 0 ]]
then
check_directory "$location_phlog"
phlog_new
fi
}