diff --git a/notes.sh b/notes.sh index d4737a0..2ed592d 100755 --- a/notes.sh +++ b/notes.sh @@ -197,6 +197,7 @@ pack_mime() { } new_entry() { + INP="$1" DIR=$(mktemp -d) ENTRY_FILE="$DIR/note.md" ENTRY_FILE_START="$(mktemp)" @@ -210,25 +211,38 @@ new_entry() { cp "$ENTRY_FILE" "$ENTRY_FILE_START" - if [ -t 0 ]; then + if [ -n "$INP" ] && [ ! -f "$INP" ] && [ ! -d "$INP" ]; then + die "File or directory doesn't exist: $INP" + fi + + if [ -f "$INP" ]; then + cp "$INP" "$DIR/note.md" + elif [ -d "$INP" ]; then + if [ ! -f "$INP/note.md" ]; then + die "File doesn't exist: $INP/note.md" + fi + cp -n "$INP"/* "$DIR/" || true + cat "$INP/note.md" >> "$DIR/note.md" + elif [ -t 0 ]; then "$EDITOR" "$ENTRY_FILE" else - ENTRY_FILE_STDIN="$(mktemp)" while read -r line ; do - echo "$line" >> "$ENTRY_FILE_STDIN" + echo "$line" >> "$ENTRY_FILE" done - - HEADERS=$( echo "$( - get_headers "$ENTRY_FILE_STDIN" - get_headers "$ENTRY_FILE" - )" | sort -u -k1,1) - - { - echo "$HEADERS" - echo "" - get_body "$ENTRY_FILE_STDIN" - } > "$ENTRY_FILE" fi + MERGED_ENTRY_FILE="$(mktemp)" + + HEADERS=$( echo "$( + get_headers "$ENTRY_FILE" + )" | tac | sort -u -k1,1) + + { + echo "$HEADERS" + echo "" + get_body "$ENTRY_FILE" + } > "$MERGED_ENTRY_FILE" + + mv "$MERGED_ENTRY_FILE" "$ENTRY_FILE" if ! cmp -s "$ENTRY_FILE" "$ENTRY_FILE_START" ; then UNIX_TIMESTAMP=$(date "+%s") @@ -313,7 +327,7 @@ usage() { while (( "$#" )); do case "$1" in -n|--new) - new_entry + new_entry "$2" exit 0 ;; -l|--list) diff --git a/test/test.sh b/test/test.sh index ceabc1d..be32203 100755 --- a/test/test.sh +++ b/test/test.sh @@ -26,11 +26,19 @@ testcase() { } assert() { - if "$@"; then - return 0 - else - echo "Assert: - fi + # assert [stdin] + + command="$1" + expected="$(echo -ne "${2:-}")" + result="$(eval 2>/dev/null $1 <<< ${3:-})" || true + + if [[ "$result" == "$expected" ]]; then + return + fi + result="$(sed -e :a -e '$!N;s/\n/\\n/;ta' <<< "$result")" + + echo "Expected '$command' == '$expected'. Got: '$result'" + exit 1 } new_note_from_stdin() { @@ -42,11 +50,44 @@ new_note_from_stdin() { OUTPUT="$(cat "$(pwd)/notes/cur"/*)" - test 1 = 2 + assert 'echo "$OUTPUT" | grep Content-Type:' "Content-Type: text/plain; charset=utf-8" + assert 'echo "$OUTPUT" | grep MIME-Version:' "MIME-Version: 1.0" + assert 'echo "$OUTPUT" | grep Content-Disposition:' "Content-Disposition: inline" + + assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header" + +} + +new_note_from_file() { + cat > "$TMP/input.md" <<- EOF + Subject: This is a header + + # This is a body + EOF + + "$BASE_DIR/notes.sh" -n "$TMP/input.md" + OUTPUT="$(cat "$(pwd)/notes/cur"/*)" + + assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header" +} + +new_note_from_dir() { + mkdir "$TMP/inpdir" + cat > "$TMP/inpdir/note.md" <<- EOF + Subject: This is a header + + # This is a body + EOF + + "$BASE_DIR/notes.sh" -n "$TMP/inpdir" + OUTPUT="$(cat "$(pwd)/notes/cur"/*)" + + assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header" } testcase new_note_from_stdin - +testcase new_note_from_file +testcase new_note_from_dir if [[ "$RESULT" == "0" ]]; then echo "All tests passed."