Skip to content

Commit a27a4bb

Browse files
committed
Add update operation
1 parent 80f909f commit a27a4bb

File tree

1 file changed

+75
-22
lines changed

1 file changed

+75
-22
lines changed

snippet.sh

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ USAGE
3535
}
3636

3737
# Root of the gitlab API endpoint
38-
GITLAB_ROOT=${GITLAB_ROOT:-"https://gitlab.com/api/v4"}
38+
GITLAB_HOST=${GITLAB_HOST:-gitlab.com}
39+
GITLAB_ROOT=${GITLAB_ROOT:-}
3940

4041
# Token for accessing the API
4142
GITLAB_TOKEN=${GITLAB_TOKEN:-}
@@ -47,8 +48,13 @@ GITLAB_PROJECT=${GITLAB_PROJECT:-}
4748
while [ $# -gt 0 ]; do
4849
case "$1" in
4950
-g | --gitlab)
50-
GITLAB_ROOT=$2; shift 2;;
51+
GITLAB_HOST=$2; shift 2;;
5152
--gitlab=*)
53+
GITLAB_HOST="${1#*=}"; shift 1;;
54+
55+
-r | --root)
56+
GITLAB_ROOT=$2; shift 2;;
57+
--root=*)
5258
GITLAB_ROOT="${1#*=}"; shift 1;;
5359

5460
-p | --project)
@@ -84,6 +90,8 @@ while [ $# -gt 0 ]; do
8490
esac
8591
done
8692

93+
[ -z "$GITLAB_ROOT" ] && GITLAB_ROOT="https://${GITLAB_HOST}/api/v4"
94+
8795
if [ "$#" = "0" ]; then
8896
cmd=list
8997
else
@@ -100,13 +108,15 @@ callcurl() {
100108
_path=${1:-}
101109
shift
102110
fi
111+
yush_debug "Calling ${GITLAB_ROOT%/}/projects/${GITLAB_PROJECT}/snippets/${_path%/}"
103112
curl -sSL \
104113
--header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" \
105114
"$@" \
106115
"${GITLAB_ROOT%/}/projects/${GITLAB_PROJECT}/snippets/${_path%/}"
107116
}
108117

109118
json_generate() {
119+
_fields=
110120
_title=
111121
_description=
112122
_filename=
@@ -115,29 +125,29 @@ json_generate() {
115125
while [ $# -gt 0 ]; do
116126
case "$1" in
117127
-t | --title)
118-
_title=$2; shift 2;;
128+
_title=$2; _fields="t${_fields}"; shift 2;;
119129
--title=*)
120-
_title="${1#*=}"; shift 1;;
130+
_title="${1#*=}"; _fields="t${_fields}"; shift 1;;
121131

122132
-d | --description)
123-
_description=$2; shift 2;;
133+
_description=$2; _fields="d${_fields}"; shift 2;;
124134
--description=*)
125-
_description="${1#*=}"; shift 1;;
135+
_description="${1#*=}"; _fields="d${_fields}"; shift 1;;
126136

127137
-f | --filename)
128-
_filename=$2; shift 2;;
138+
_filename=$2; _fields="f${_fields}"; shift 2;;
129139
--filename=*)
130-
_filename="${1#*=}"; shift 1;;
140+
_filename="${1#*=}"; _fields="f${_fields}"; shift 1;;
131141

132142
-c | --content)
133-
_content=$2; shift 2;;
143+
_content=$2; _fields="c${_fields}"; shift 2;;
134144
--content=*)
135-
_content="${1#*=}"; shift 1;;
145+
_content="${1#*=}"; _fields="c${_fields}"; shift 1;;
136146

137147
-v | --visibility)
138-
_visibility=$2; shift 2;;
148+
_visibility=$2; _fields="v${_fields}"; shift 2;;
139149
--visibility=*)
140-
_visibility="${1#*=}"; shift 1;;
150+
_visibility="${1#*=}"; _fields="v${_fields}"; shift 1;;
141151

142152
-h | --help)
143153
usage "" 0;;
@@ -150,12 +160,23 @@ json_generate() {
150160
break;;
151161
esac
152162
done
153-
_json=$(mktemp)
154-
printf \
155-
'{"title":"%s","description":"%s","file_name":"%s","content":"%s","visibility":"%s"}' \
156-
"$_title" "$_description" "$_filename" "$_content" "$_visibility" \
157-
> "$_json"
158-
printf %s\\n "$_json"
163+
_fpath=$(mktemp)
164+
_jsonpath=$(mktemp)
165+
# Print fields that were set in turns to the temporary _fpath
166+
printf '{\n' > $_fpath
167+
printf %s\\n "$_fields" | grep -q t && printf '"title":"%s",\n' "$_title" >> $_fpath
168+
printf %s\\n "$_fields" | grep -q d && printf '"description":"%s",\n' "$_description" >> $_fpath
169+
printf %s\\n "$_fields" | grep -q f && printf '"file_name":"%s",\n' "$_filename" >> $_fpath
170+
printf %s\\n "$_fields" | grep -q c && printf '"content":"%s",\n' "$_content" >> $_fpath
171+
printf %s\\n "$_fields" | grep -q v && printf '"visibility":"%s",\n' "$_visibility" >> $_fpath
172+
# Remove last , from last line of _fpath to create beginning of real JSON
173+
# file at _jsonpath
174+
head -n -1 $_fpath > $_jsonpath
175+
tail -n 1 $_fpath | sed -E 's/,$//' >> $_jsonpath
176+
# Close the JSON file and remove the temporary _fpath. We are done!
177+
printf '}' >> $_jsonpath
178+
rm -f "$_fpath"
179+
printf %s\\n "$_jsonpath"
159180
}
160181

161182
case "$cmd" in
@@ -189,10 +210,42 @@ case "$cmd" in
189210
;;
190211
create|add)
191212
_json=$(json_generate "$@")
192-
callcurl "" \
193-
--header "Content-Type: application/json" \
194-
--request POST \
195-
-d @"$_json"
213+
res=$(callcurl "" \
214+
--header "Content-Type: application/json" \
215+
--request POST \
216+
-d @"$_json")
217+
if printf %s\\n "$res" | grep -qE '"error"\s*:\s*"'; then
218+
if printf %s\\n "$res" | yush_json | grep -q '/error_description'; then
219+
yush_error "$(printf %s\\n "$res" | yush_json | grep '/error_description' | cut -d " " -f 3-)"
220+
else
221+
yush_error "$(printf %s\\n "$res" | yush_json | grep '/error' | cut -d " " -f 3-)"
222+
fi
223+
exit 1
224+
else
225+
printf %s\\n "$res" | yush_json | grep -E "^/id " | awk '{print $3}'
226+
fi
227+
rm -f "$_json"
228+
;;
229+
update|change)
230+
_json=$(json_generate "$@")
231+
if [ "$#" = "0" ]; then
232+
yush_warn "You have to specify a snippet ID"
233+
else
234+
res=$(callcurl "$(eval echo "\$$#")" \
235+
--header "Content-Type: application/json" \
236+
--request PUT \
237+
-d @"$_json")
238+
if printf %s\\n "$res" | grep -qE '"error"\s*:\s*"'; then
239+
if printf %s\\n "$res" | yush_json | grep -q '/error_description'; then
240+
yush_error "$(printf %s\\n "$res" | yush_json | grep '/error_description' | cut -d " " -f 3-)"
241+
else
242+
yush_error "$(printf %s\\n "$res" | yush_json | grep '/error' | cut -d " " -f 3-)"
243+
fi
244+
exit 1
245+
else
246+
printf %s\\n "$res" | yush_json | grep -E "^/id " | awk '{print $3}'
247+
fi
248+
fi
196249
rm -f "$_json"
197250
;;
198251
esac

0 commit comments

Comments
 (0)