Created
September 29, 2020 15:48
-
-
Save fieldse/81d98e590ffa2a5f6c6551ac0ba50077 to your computer and use it in GitHub Desktop.
Daily todo file migrator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# todos-migrate.sh | |
# Generate a todo file from template | |
template="$HOME/Templates/Todos.template" | |
target_dir="$HOME/Documents" | |
archive_dir="$HOME/Documents/todos" | |
filename="Todos.md" | |
# Today's file | |
fileext=${filename##*.} # grab file extension | |
# The actual file, created inside archive dir | |
new_file="${archive_dir}/todos-$(date +'%Y-%m-%d').${fileext}" | |
# Target we'll create the link at | |
notes_file="${target_dir}/${filename}" | |
function setup_dirs() { | |
mkdir -p "$target_dir" # this is safe if exists | |
mkdir -p "$archive_dir" | |
} | |
function template_exists() { | |
[[ -f "$template" ]] ; return $? | |
} | |
function today_file_exists() { | |
[[ -f $new_file ]] ; return $? | |
} | |
function notefile_exists() { | |
[[ -L ${notes_file} ]] ; return $? | |
} | |
function create_link() { | |
echo "Creating link" | |
ln -sf "${new_file}" "${notes_file}" | |
} | |
# Copy template to directory and create symlink | |
function copy_template() { | |
echo -e "Creating ${new_file}" | |
cp "${template}" "${new_file}" -v | |
} | |
## Run ## | |
function run() { | |
echo " == Create daily to-do file from template == " | |
setup_dirs | |
if today_file_exists && notefile_exists ; then | |
echo "File exists, exiting" | |
exit 0 | |
fi | |
# Check template exists | |
if ! template_exists ; then | |
echo "Template file $template not found" | |
exit 1 | |
fi | |
# Create file from template | |
if ! today_file_exists ; then | |
copy_template | |
fi | |
# Make link | |
if ! notefile_exists ; then | |
create_link | |
fi | |
} | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment