-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve
executable file
·44 lines (35 loc) · 987 Bytes
/
resolve
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env bash
# This script resolves a link from one note to another.
# Finding the target of a link is no more than finding the match of an exact
# search by file name. If no matches are found, the script fails
# The script takes a search query, a location to search within
# and optional flag
set -eu
flag_pass_is_present=0;
if [[ "$1" == "-p" ]] || [[ "$1" == "--pass" ]];
then
flag_pass_is_present=1;
shift;
fi
if (($# != 2)); then
echo "Usage: $0 [-p|--pass] <search query> <notes directory>"
echo "Example: $0 'Foundations of Mathematics' ~/notes"
exit 1
fi
search_query="$1"
notes_path="$(realpath "$2")"
FIND_COMMAND=(
fd
--search-path "$notes_path"
--type file
--and "$search_query.md"
)
search_result="$("${FIND_COMMAND[@]}" | head -1)"
if [[ -n "$search_result" ]]; then
printf '%s' "$search_result"
elif [[ $flag_pass_is_present == 1 ]]; then
printf '%s/%s.md' "$notes_path" "$search_query"
else
echo "No results found" >&2
exit 1
fi