-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag_base
102 lines (93 loc) · 3.11 KB
/
tag_base
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# vim:ft=sh
# this is the root of where our tag data will be stored
export TAG_STORE_ROOT=${TAG_STORE_ROOT-~/.tags}
# and under that, where our "items tagged" will be referenced
export ITEM_STORE=${TAG_STORE_ROOT}/objs/
# as well, the tags that reference those tagged items
export TAG_STORE=${TAG_STORE_ROOT}/tags/
mkdir -p ${ITEM_STORE} ${TAG_STORE}
# my job is to make a tag into something nice and easy to store
# on the file system
function sanitize_tag(){
TAG="${1?You must provide a tag for me to sanitize}"
TAG="${TAG/ /_}"
}
readonly -f sanitize_tag
export -f sanitize_tag
# my job is to determine if a provided tag meets the criteria of being
# a tag. that is to say, it must contain only alphanumeric characters
# spaces dashes '-' or underscores '_'
function validate_tag(){
TAG="${1?You must provide a tag for me to validate}"
TAG="${TAG//[A-Za-z0-9_\- ]/}"
[ -z "${TAG}" ] || die "invalid tag ${TAG}, you can only use alphanumeric, space, -, and _" 1
}
readonly -f validate_tag
export -f validate_tag
# my job is to validate an entire array of tags are formatted correctly as
# per the definition in validate_tag
function validate_tags(){
local TAGS=("$@")
[ "${#TAGS[@]}" -ne 0 ] || exit 0 # no tags is perfectly valid
for tag in "${TAGS[@]}"
do
validate_tag "$tag"
done
}
readonly -f validate_tags
export -f validate_tags
# my job is to determine if the provided value meets the criteria to be
# a key. that is to say it, it must contain only alphanumeric characters,
# underscores '_' or dashes '-"
function validate_key(){
local KEY="${1?You must provide a tag for me to validate}"
local KEY="${KEY//[A-Za-z0-9_\-]/}"
[ -z "${KEY}" ] || die "invalid key ${KEY}, you can only use alphanumeric, -, and _" 2
}
readonly -f validate_key
export -f validate_key
function tag_this(){
local KEY="${1?You must provide me a key to associate tags with}"
shift
# After the key, the rest are the tags
local TAGS=("$@")
[ "${#TAGS[@]}" -eq 0 ] && die "You provided no tags, I can't tag something with no tags" 3
local KEY_FILE="${ITEM_STORE}${KEY}.json"
# this is not local, because we want the caller to have access
# if they're interested
ONCE_THRU=
TAG_JSON=$(
echo "{"
echo " "\""key"\"":"\""${KEY}"\"","
echo -n " "\""tags"\"":["
for tag in "${TAGS[@]}"
do
[ -n "${ONCE_THRU}" ] && echo -n ","
echo -n ""\""${tag}"\"""
ONCE_THRU=true
done
echo "]"
echo "}"
)
# store our tag JSON blob, which serves as a convenient way to go vrom
# key to tags
echo "${TAG_JSON}" > "${KEY_FILE}"
# create our tag links which make a nice way to go from tag to items
for tag in "${TAGS[@]}"
do
sanitize_tag "$tag"
mkdir -p "${TAG_STORE}${TAG}"
local TAG_FILE="${TAG_STORE}${TAG}/${KEY}.json"
# it's ok, if we're overwriting one it's because it's the same thing
ln -fs "${KEY_FILE}" "${TAG_FILE}"
done
}
readonly -f tag_this
export -f tag_this
function get_tags(){
local KEY="${1?You need to provide a key so I can look up tags for you}"
local KEY_FILE="${ITEM_STORE}${KEY}.json"
jq ".tags" "${KEY_FILE}"
}
readonly -f get_tags
export -f get_tags