memo/memo

367 lines
5.8 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
2018-05-17 19:50:01 +02:00
# commands
ack_cmd=ack
man_cmd=man
2018-05-23 22:14:44 +02:00
tree_cmd=tree
2018-08-08 20:03:48 +02:00
git_cmd=git
2018-05-23 22:14:44 +02:00
# comment pandoc_cmd to fall back to cat
pandoc_cmd=pandoc
2018-05-17 19:50:01 +02:00
2018-08-08 20:03:48 +02:00
2017-06-02 00:10:06 +02:00
# 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
2018-12-28 16:52:54 +01:00
function precondition:provide_memo_folder(){
2017-06-02 00:10:06 +02:00
if [[ ! -d ${MEMO_FOLDER} ]]
then
echo "create memo folder : ${MEMO_FOLDER}"
mkdir -p ${MEMO_FOLDER}
fi
}
2017-05-30 20:36:38 +02:00
2018-12-28 16:52:54 +01:00
function precondition:provide_topic(){
2017-06-02 00:10:06 +02:00
topic_name=$1
topic_path=${MEMO_FOLDER}/${topic_name}
topic_file=${topic_path}/memo.md
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 "# Memo" > ${topic_file}
2017-06-02 00:10:06 +02:00
fi
2017-05-30 20:36:38 +02:00
}
2018-08-08 20:03:48 +02:00
#
# git commands
#
IS_GIT_REPO=0
# check if it is a gitrepository
# use the IS_GIT_REPO variable to
# verify later if it is a git repo
function git:status(){
cd ${MEMO_FOLDER}
${git_cmd} status &> /dev/null
if [[ $? -eq 0 ]]
then
IS_GIT_REPO=1
else
IS_GIT_REPO=0
fi
}
2017-05-30 20:36:38 +02:00
2018-08-08 20:03:48 +02:00
# run before commit
function git:add_all_topics(){
if [[ ${IS_GIT_REPO} -eq 1 ]]
then
cd ${MEMO_FOLDER}
${git_cmd} add .
fi
}
# make a commit
function git:commit(){
local message="${1:-blind commit}"
if [[ ${IS_GIT_REPO} -eq 1 ]]
then
cd ${MEMO_FOLDER}
${git_cmd} commit -m "${message}"
fi
}
function git:run_command(){
2018-08-08 20:03:48 +02:00
cd ${MEMO_FOLDER}
${git_cmd} $@
}
2017-05-30 20:36:38 +02:00
#
# remove command
#
function remove_topic (){
topic_name=$1
2018-12-28 16:52:54 +01:00
#precondition:provide_topic ${topic_name}
topic_path=${MEMO_FOLDER}/${topic_name}
if [[ -d ${topic_path} ]]
then
rm -rf ${topic_path}
fi
git:add_all_topics
git:commit "edit : ${topic_name}"
}
#
# edit command
#
function edit_topic () {
topic_name=$1
2019-07-12 19:27:14 +02:00
if [[ -z $topic_name ]]
then
echo "you have to specify a topic"
exit 1
fi
2018-12-28 16:52:54 +01:00
precondition:provide_topic ${topic_name}
topic_path=${MEMO_FOLDER}/${topic_name}
if [[ -z ${EDITOR} ]]
then
echo "you have to define the ${EDITOR} variable"
exit 1
fi
${EDITOR} ${topic_file}
2018-08-08 20:03:48 +02:00
git:add_all_topics
git:commit "edit : ${topic_name}"
}
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
2018-12-28 16:52:54 +01:00
precondition:provide_topic ${topic_name}
2017-06-02 00:10:06 +02:00
topic_path=${MEMO_FOLDER}/${topic_name}
topic_file=${topic_path}/memo.md
2017-06-15 23:18:34 +02:00
shift
if [[ $# -gt 0 ]]
then
memo=$@
echo "append memo to ${topic_name}"
cat >>${topic_file} <<EOF
2017-06-02 00:10:06 +02:00
${memo}
2017-05-30 20:36:38 +02:00
EOF
2017-06-15 23:18:34 +02:00
else
echo "append input to ${topic_name}"
echo "" >>${topic_file}
cat >>${topic_file} -
2017-06-15 23:18:34 +02:00
fi
2018-08-08 20:03:48 +02:00
git:add_all_topics
git:commit "add : ${topic_name}"
2017-05-30 20:36:38 +02:00
}
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
2018-12-28 16:52:54 +01:00
precondition:provide_topic ${topic_name}
2017-06-02 00:10:06 +02:00
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
2018-08-08 20:03:48 +02:00
git:add_all_topics
git:commit "copy $( dirname ${path} ): ${topic_name}"
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}
2018-05-17 19:50:01 +02:00
${ack_cmd} "${search_terms}"
2017-05-30 20:36:38 +02:00
}
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
2018-05-23 22:14:44 +02:00
if [[ -z ${pandoc_cmd} ]]
then
cat ${topic_file}
echo
else
cat <(
echo "% ${topic_name}" && \
echo "% $( whoami )" && \
echo "% $( date +%Y-%m-%d )" && \
cat ${topic_file} && \
echo && \
echo "# Folders" && \
echo && \
find ${topic_path} -printf "* %p\n") | \
2018-05-17 19:50:01 +02:00
${pandoc_cmd} - -s -t man | \
${man_cmd} -l -
2018-05-23 22:14:44 +02:00
fi
2017-06-02 00:10:06 +02:00
fi
}
#
# list command
#
function list_topics(){
echo
echo "topics : "
echo
2018-05-17 19:50:01 +02:00
${tree_cmd} -L 1 ${MEMO_FOLDER}
2017-06-02 00:10:06 +02:00
}
#
# 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-06-15 23:18:34 +02:00
if memo is not given it will read from input
so piping is possible.
Example: cat file.txt | memo add topic
2017-05-30 20:36:38 +02:00
rm <topic>
remove ${MEMO_FOLDER}/<topic> (with all its content)
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
edit <topic>
edit topic ${MEMO_FOLDER}/<topic>.md
2017-06-02 00:10:06 +02:00
list
prints a list of all topics
2017-05-30 20:36:38 +02:00
git <command>
run git <command> in ${MEMO_FOLDER}
2017-05-30 20:36:38 +02:00
EOF
}
#
# main
#
command=$1
shift
2018-12-28 16:52:54 +01:00
precondition:provide_memo_folder
2018-08-08 20:03:48 +02:00
git:status
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 $@
;;
edit)
edit_topic $@
;;
2017-06-02 00:10:06 +02:00
list)
list_topics
;;
2018-08-08 20:03:48 +02:00
git)
git:run_command $@
;;
rm)
remove_topic $@
2018-08-08 20:03:48 +02:00
;;
2017-06-02 00:10:06 +02:00
*) show_help
2017-05-30 20:36:38 +02:00
;;
esac