#!/usr/bin/env bash # This file contains functions relating to file operations such as new, edit and deleting files newFile(){ vecho "newFile $*" cd "$dataDir" || return # While there is a - at the begining local title="$*" if [ -z "$title" ]; then echo -n "Enter a title: " read -r title fi local filename filename="$(escapeFilename "$title.md")" [ -e "$filename" ] && die "$filename already exists" echo -e "--- Title: $title Tags: - --- " > "$filename" echo "INSERT INTO items (filename, title, type) VALUES ( '$(safeSQL "$filename")', '$(safeSQL "$title")', 'normal' );" | sqlite3 "${sqliteFile}" editFile "$filename" } # Takes the filename as a parameter editFile(){ vecho "editFile $*" cd "$dataDir" || return local filename local oldTitle local newTitle filename="$(findFile "$*")" [ ! -e "$filename" ] && exit 1 oldTitle="$(getYamlTitle "$filename")" "$editor" "$filename" newTitle="$(getYamlTitle "$filename")" getYamlTags "$filename" | assignTags "$filename" if [ "$newTitle" != "$oldTitle" ]; then vecho "Changed title" local newfilename newfilename="$(escapeFilename "$newTitle.md")" if [ -e "$newfilename" ]; then echo -e "${YELLOW}File name $newfilename already exists${NC}" echo -e "Please fix manually" exit 1 else mv "$filename" "$newfilename" echo "UPDATE items SET (filename,title) = ('$(safeSQL "$newfilename")','$(safeSQL "$newTitle")') WHERE filename = '$(safeSQL "$filename")';" | sqlite3 "${sqliteFile}" gitChange "$newfilename" fi else gitChange "$filename" fi } deleteFile(){ cd "$dataDir" || return local filename local fileID local rsp filename="$(findFile "$1")" fileID="$(findFileId "$filename")" [ ! -e "$filename" ] && exit 1 echo -n "Are you sure? [yN] " read -r rsp if [[ "$(echo "$rsp" | tr '[:upper:]' '[:lower:]')" = "y"* ]]; then rm "$filename" # This deletes the file from the sql database and any tag links echo "DELETE FROM items WHERE id = '$(safeSQL "$fileID")'; DELETE FROM links WHERE itemID = '$(safeSQL "$fileID")';" | sqlite3 --column --header "${sqliteFile}" fi }