Skip to content

Commit d34a954

Browse files
committed
Add search
1 parent 5c94380 commit d34a954

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ snippets accessible by the token at the project on the standard out.
141141
This command takes the identifier of an existing snippet as an argument and will
142142
print out its raw content on the standard out.
143143

144+
#### `search`
145+
146+
This command searches for snippets matching the extended regular expressions
147+
passed through the options and prints their identifiers. Recognised options are:
148+
149+
+ `-t` or `--title` is the regex to match against the title of the snippet.
150+
+ `-d` or `--description` is the regex to match against the description of the
151+
snippet.
152+
+ `-f` or `--filename` is the regex to match against the filename of the
153+
snippet.
154+
+ `-v` or `--visibility` is the regex to match against the visibility of the
155+
snippet.
156+
144157
#### `details`
145158

146159
This command takes the identifier of an existing snippet and prints out the

lib/getters.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,58 @@ snippet_details() {
4040
snippet_curl "${1}" | yush_json
4141
fi
4242
fi
43+
}
44+
45+
_snippet_value() {
46+
printf %s\\n "$1" | grep -E "^/${2} " | cut -d " " -f 3-
47+
}
48+
49+
snippet_search() {
50+
_title=".*"
51+
_description=".*"
52+
_filename=".*"
53+
_visibility=".*"
54+
while [ $# -gt 0 ]; do
55+
case "$1" in
56+
-t | --title)
57+
_title=$2; shift 2;;
58+
--title=*)
59+
_title="${1#*=}"; shift 1;;
60+
61+
-d | --description)
62+
_description=$2; shift 2;;
63+
--description=*)
64+
_description="${1#*=}"; shift 1;;
65+
66+
-f | --filename)
67+
_filename=$2; shift 2;;
68+
--filename=*)
69+
_filename="${1#*=}"; shift 1;;
70+
71+
-v | --visibility)
72+
_visibility=$2; shift 2;;
73+
--visibility=*)
74+
_visibility="${1#*=}"; shift 1;;
75+
76+
-h | --help)
77+
usage "" 0;;
78+
79+
--)
80+
shift; break;;
81+
-*)
82+
usage "Unknown option: $1 !";;
83+
*)
84+
break;;
85+
esac
86+
done
87+
88+
for _s in $(snippet_list); do
89+
_json=$(snippet_details "$_s")
90+
if _snippet_value "$_json" "title" | grep -Eq "$_title" \
91+
&& _snippet_value "$_json" "description" | grep -Eq "$_description" \
92+
&& _snippet_value "$_json" "file_name" | grep -Eq "$_filename" \
93+
&& _snippet_value "$_json" "visibility" | grep -Eq "$_visibility"; then
94+
printf %d\\n "$_s"
95+
fi
96+
done
4397
}

snippet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Commands:
6969
specified)
7070
get|read Get raw content of snippet
7171
details Get JSON or parsed JSON content of snippet
72+
search Search for matching snippet
7273
add|create Create new snippet
7374
update|change Change existing snippet
7475
delete|remove Delete existing snippet
@@ -196,6 +197,8 @@ case "$cmd" in
196197
snippet_get "$@";;
197198
details)
198199
snippet_details "$@";;
200+
search)
201+
snippet_search "$@";;
199202
create|add)
200203
snippet_create "$@";;
201204
update|change)

0 commit comments

Comments
 (0)