35
35
}
36
36
37
37
# 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:- }
39
40
40
41
# Token for accessing the API
41
42
GITLAB_TOKEN=${GITLAB_TOKEN:- }
@@ -47,8 +48,13 @@ GITLAB_PROJECT=${GITLAB_PROJECT:-}
47
48
while [ $# -gt 0 ]; do
48
49
case " $1 " in
49
50
-g | --gitlab)
50
- GITLAB_ROOT =$2 ; shift 2;;
51
+ GITLAB_HOST =$2 ; shift 2;;
51
52
--gitlab=* )
53
+ GITLAB_HOST=" ${1#* =} " ; shift 1;;
54
+
55
+ -r | --root)
56
+ GITLAB_ROOT=$2 ; shift 2;;
57
+ --root=* )
52
58
GITLAB_ROOT=" ${1#* =} " ; shift 1;;
53
59
54
60
-p | --project)
@@ -84,6 +90,8 @@ while [ $# -gt 0 ]; do
84
90
esac
85
91
done
86
92
93
+ [ -z " $GITLAB_ROOT " ] && GITLAB_ROOT=" https://${GITLAB_HOST} /api/v4"
94
+
87
95
if [ " $# " = " 0" ]; then
88
96
cmd=list
89
97
else
@@ -100,13 +108,15 @@ callcurl() {
100
108
_path=${1:- }
101
109
shift
102
110
fi
111
+ yush_debug " Calling ${GITLAB_ROOT%/ } /projects/${GITLAB_PROJECT} /snippets/${_path%/ } "
103
112
curl -sSL \
104
113
--header " PRIVATE-TOKEN: ${GITLAB_TOKEN} " \
105
114
" $@ " \
106
115
" ${GITLAB_ROOT%/ } /projects/${GITLAB_PROJECT} /snippets/${_path%/ } "
107
116
}
108
117
109
118
json_generate () {
119
+ _fields=
110
120
_title=
111
121
_description=
112
122
_filename=
@@ -115,29 +125,29 @@ json_generate() {
115
125
while [ $# -gt 0 ]; do
116
126
case " $1 " in
117
127
-t | --title)
118
- _title=$2 ; shift 2;;
128
+ _title=$2 ; _fields= " t ${_fields} " ; shift 2;;
119
129
--title=* )
120
- _title=" ${1#* =} " ; shift 1;;
130
+ _title=" ${1#* =} " ; _fields= " t ${_fields} " ; shift 1;;
121
131
122
132
-d | --description)
123
- _description=$2 ; shift 2;;
133
+ _description=$2 ; _fields= " d ${_fields} " ; shift 2;;
124
134
--description=* )
125
- _description=" ${1#* =} " ; shift 1;;
135
+ _description=" ${1#* =} " ; _fields= " d ${_fields} " ; shift 1;;
126
136
127
137
-f | --filename)
128
- _filename=$2 ; shift 2;;
138
+ _filename=$2 ; _fields= " f ${_fields} " ; shift 2;;
129
139
--filename=* )
130
- _filename=" ${1#* =} " ; shift 1;;
140
+ _filename=" ${1#* =} " ; _fields= " f ${_fields} " ; shift 1;;
131
141
132
142
-c | --content)
133
- _content=$2 ; shift 2;;
143
+ _content=$2 ; _fields= " c ${_fields} " ; shift 2;;
134
144
--content=* )
135
- _content=" ${1#* =} " ; shift 1;;
145
+ _content=" ${1#* =} " ; _fields= " c ${_fields} " ; shift 1;;
136
146
137
147
-v | --visibility)
138
- _visibility=$2 ; shift 2;;
148
+ _visibility=$2 ; _fields= " v ${_fields} " ; shift 2;;
139
149
--visibility=* )
140
- _visibility=" ${1#* =} " ; shift 1;;
150
+ _visibility=" ${1#* =} " ; _fields= " v ${_fields} " ; shift 1;;
141
151
142
152
-h | --help)
143
153
usage " " 0;;
@@ -150,12 +160,23 @@ json_generate() {
150
160
break ;;
151
161
esac
152
162
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 "
159
180
}
160
181
161
182
case " $cmd " in
@@ -189,10 +210,42 @@ case "$cmd" in
189
210
;;
190
211
create|add)
191
212
_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
196
249
rm -f " $_json "
197
250
;;
198
251
esac
0 commit comments