memo/memo

222 lines
3.1 KiB
Plaintext
Raw Normal View History

2017-05-30 20:36:38 +02:00
#!/bin/bash
2017-06-02 00:10:06 +02:00
# Configuration
#
# Directory where to hold the memos
# MEMO_DIR=~/memo
# Script
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
MEMO_FOLDER="${MEMO_DIR:-$HOME/memo}"
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
function precondition::provide_memo_folder(){
if [[ ! -d ${MEMO_FOLDER} ]]
then
echo "create memo folder : ${MEMO_FOLDER}"
mkdir -p ${MEMO_FOLDER}
fi
}
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
function precondition::provide_topic(){
topic_name=$1
topic_path=${MEMO_FOLDER}/${topic_name}
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
if [[ ! -d ${topic_path} ]]
then
echo "create memo topic : ${topic_name}"
mkdir -p ${topic_path}
echo "# ${topic_name}" > ${topic_path}/memo.md
fi
2017-05-30 20:36:38 +02:00
}
2017-06-02 00:10:06 +02:00
#
# add command
#
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
function add_memo(){
topic_name=$1
shift
memo=$@
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
precondition::provide_topic ${topic_name}
topic_path=${MEMO_FOLDER}/${topic_name}
echo "append memo to ${topic_name}"
cat >>${topic_path}/memo.md <<EOF
${memo}
2017-05-30 20:36:38 +02:00
EOF
}
2017-06-02 00:10:06 +02:00
#
# copy command
#
function copy_file(){
topic_name=$1
2017-05-30 20:36:38 +02:00
shift
path=$@
2017-06-02 00:10:06 +02:00
if [[ -z "${path}" ]]
then
show_help
echo
echo "!! path not given"
echo
exit 1
fi
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
if [[ ! -e "${path}" ]]
2017-05-30 20:36:38 +02:00
then
2017-06-02 00:10:06 +02:00
echo "path '${path}' does not exist"
exit 1
2017-05-30 20:36:38 +02:00
fi
2017-06-02 00:10:06 +02:00
precondition::provide_topic ${topic_name}
topic_path=${MEMO_FOLDER}/${topic_name}
echo "copy '${path}' to '${topic_name}'"
cp -r "${path}" ${topic_path}/
2017-05-30 20:36:38 +02:00
}
2017-06-02 00:10:06 +02:00
#
# search command
#
2017-05-30 20:36:38 +02:00
function search_term(){
search_terms=$@
2017-06-02 00:10:06 +02:00
if [[ -z "${search_terms}" ]]
then
show_help
echo
echo "!! no search terms given"
echo
exit 1
fi
2017-05-30 20:36:38 +02:00
cd ${MEMO_FOLDER}
ack "${search_terms}"
}
2017-06-02 00:10:06 +02:00
#
# show command
#
function show_topic(){
topic_name=$1
topic_path=${MEMO_FOLDER}/${topic_name}
topic_file=${topic_path}/memo.md
if [[ ! -d ${topic_path} ]]
then
echo "${topic_name} does not exist"
exit 1
fi
if [[ -f ${topic_file} ]]
then
echo
cat ${topic_file}
echo
fi
tree -a ${topic_path}
}
#
# list command
#
function list_topics(){
echo
echo "topics : "
echo
tree -L 1 ${MEMO_FOLDER}
}
#
# show help command
#
function show_help() {
2017-05-30 20:36:38 +02:00
cat <<EOF
2017-06-02 00:10:06 +02:00
/\/\ ___ /\/\ ___ /__ \___ ___ / /
/ \ / _ \/ \ / _ \ / /\/ _ \ / _ \ / /
/ /\/\ \ __/ /\/\ \ (_) | / / | (_) | (_) / /___
\/ \/\___\/ \/\___/ \/ \___/ \___/\____/
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
Usage:
memo <command> arguments
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
Commands:
add <topic> [memo]
adds a memo.md in ${MEMO_FOLDER}/<topic>/
containing the text
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
copy <topic> <file to copy>
copy a file to ${MEMO_FOLDER}/<topic>/
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
search <term to search>
search for a term in
${MEMO_FOLDER}/<memo-title>/
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
show <topic>
show topic file and list all file in the
${MEMO_FOLDER}/<topic>/
2017-05-30 20:36:38 +02:00
2017-06-02 00:10:06 +02:00
list
prints a list of all topics
2017-05-30 20:36:38 +02:00
EOF
}
#
# main
#
command=$1
shift
2017-06-02 00:10:06 +02:00
precondition::provide_memo_folder
2017-05-30 20:36:38 +02:00
case $command in
2017-06-02 00:10:06 +02:00
add) add_memo $@
2017-05-30 20:36:38 +02:00
;;
2017-06-02 00:10:06 +02:00
copy)
copy_file $@
2017-05-30 20:36:38 +02:00
;;
search)
search_term $@
;;
2017-06-02 00:10:06 +02:00
show)
show_topic $@
;;
list)
list_topics
;;
*) show_help
2017-05-30 20:36:38 +02:00
;;
esac