@@ -111,6 +111,85 @@ def usage():
111
111
for cls in classes :
112
112
print (" %s" % clsToWhat (cls ))
113
113
114
+ def do_auth ():
115
+ try :
116
+ gl = gitlab .Gitlab (gitlab_url , private_token = gitlab_token )
117
+ gl .auth ()
118
+ except :
119
+ die ("Could not connect to GitLab (%s)" % gitlab_url )
120
+
121
+ return gl
122
+
123
+ def get_id ():
124
+ try :
125
+ id = d .pop ('id' )
126
+ except :
127
+ die ("Missing --id argument" )
128
+
129
+ return id
130
+
131
+ def do_create (cls , d ):
132
+ if not cls .canCreate :
133
+ die ("%s objects can't be created" % what )
134
+
135
+ try :
136
+ o = cls (gl , d )
137
+ o .save ()
138
+ except Exception as e :
139
+ die ("Impossible to create object (%s)" % str (e ))
140
+
141
+ return o
142
+
143
+ def do_list (cls , d ):
144
+ if not cls .canList :
145
+ die ("%s objects can't be listed" % what )
146
+
147
+ try :
148
+ l = cls .list (gl , ** d )
149
+ except Exception as e :
150
+ die ("Impossible to list objects (%s)" % str (e ))
151
+
152
+ return l
153
+
154
+ def do_get (cls , d ):
155
+ if not cls .canGet :
156
+ die ("%s objects can't be retrieved" % what )
157
+
158
+ id = None
159
+ if cls not in [gitlab .CurrentUser ]:
160
+ id = get_id ()
161
+
162
+ try :
163
+ o = cls (gl , id , ** d )
164
+ except Exception as e :
165
+ die ("Impossible to get object (%s)" % str (e ))
166
+
167
+ return o
168
+
169
+ def do_delete (cls , d ):
170
+ if not cls .canDelete :
171
+ die ("%s objects can't be deleted" % what )
172
+
173
+ o = do_get (cls , d )
174
+ try :
175
+ o .delete ()
176
+ except Exception as e :
177
+ die ("Impossible to destroy object (%s)" % str (e ))
178
+
179
+ def do_update (cls , d ):
180
+ if not cls .canUpdate :
181
+ die ("%s objects can't be updated" % what )
182
+
183
+ o = do_get (cls , d )
184
+ try :
185
+ for k , v in d .items ():
186
+ o .__dict__ [k ] = v
187
+ o .save ()
188
+ except Exception as e :
189
+ die ("Impossible to update object (%s)" % str (e ))
190
+
191
+ return o
192
+
114
193
115
194
gitlab_id = None
116
195
verbose = False
186
265
except :
187
266
die ("Missing arguments. Use `gitlab -h` for help." )
188
267
189
- if action not in ['get' , 'list' , 'update' , 'create' , 'delete' , 'help' ]:
190
- die ("Unknown action: %s. Use \" gitlab %s help\" to get details." % (action , what ))
191
-
192
268
try :
193
269
cls = gitlab .__dict__ [whatToCls (what )]
194
270
except :
@@ -204,110 +280,28 @@ if action == "help":
204
280
205
281
sys .exit (0 )
206
282
207
- try :
208
- gl = gitlab .Gitlab (gitlab_url , private_token = gitlab_token )
209
- gl .auth ()
210
- except :
211
- die ("Could not connect to GitLab (%s)" % gitlab_url )
283
+ gl = do_auth ()
212
284
213
285
if action == "create" :
214
- if not cls .canCreate :
215
- die ("%s objects can't be created" % what )
216
-
217
- try :
218
- o = cls (gl , d )
219
- o .save ()
220
- except Exception as e :
221
- die ("Impossible to create object (%s)" % str (e ))
222
-
223
- if verbose :
224
- o .pretty_print ()
225
- else :
226
- o .short_print ()
227
-
228
- sys .exit (0 )
286
+ o = do_create (cls , d )
287
+ o .display (verbose )
229
288
230
289
elif action == "list" :
231
- if not cls .canList :
232
- die ("%s objects can't be listed" % what )
233
-
234
- try :
235
- l = cls .list (gl , ** d )
236
- except Exception as e :
237
- die ("Impossible to list objects (%s)" % str (e ))
238
-
239
- for o in l :
240
- if verbose :
241
- o .pretty_print ()
242
- else :
243
- o .short_print ()
290
+ for o in do_list (cls , d ):
291
+ o .display (verbose )
244
292
print ("" )
245
293
246
- sys .exit (0 )
247
-
248
294
elif action == "get" :
249
- if not cls .canGet :
250
- die ("%s objects can't be retrieved" % what )
251
-
252
- id = None
253
- if cls not in [gitlab .CurrentUser ]:
254
- try :
255
- id = d .pop ('id' )
256
- except :
257
- die ("Missing --id argument" )
258
-
259
- try :
260
- o = cls (gl , id , ** d )
261
- except Exception as e :
262
- die ("Impossible to get object (%s)" % str (e ))
263
-
264
- if verbose :
265
- o .pretty_print ()
266
- else :
267
- o .short_print ()
268
-
269
- sys .exit (0 )
295
+ o = do_get (cls , d )
296
+ o .display (verbose )
270
297
271
298
elif action == "delete" :
272
- if not cls .canDelete :
273
- die ("%s objects can't be deleted" % what )
274
-
275
- try :
276
- id = d .pop ('id' )
277
- except :
278
- die ("Missing --id argument" )
279
-
280
- try :
281
- o = cls (gl , id , ** d )
282
- except Exception as e :
283
- die ("Impossible to get object (%s)" % id , str (e ))
284
-
285
- try :
286
- o .delete ()
287
- except Exception as e :
288
- die ("Impossible to destroy object (%s)" % str (e ))
289
-
290
- sys .exit (0 )
299
+ o = do_delete (cls , d )
291
300
292
301
elif action == "update" :
293
- if not cls .canDelete :
294
- die ("%s objects can't be updated" % what )
302
+ o = do_update (cls , d )
295
303
296
- try :
297
- id = d .pop ('id' )
298
- except :
299
- die ("Missing --id argument" )
300
-
301
- try :
302
- o = cls (gl , id , ** d )
303
- except Exception as e :
304
- die ("Impossible to get object (%s)" % str (e ))
305
-
306
- try :
307
- for k , v in d .items ():
308
- o .__dict__ [k ] = v
309
- o .save ()
310
- except Exception as e :
311
- die ("Impossible to update object (%s)" % str (e ))
304
+ else :
305
+ die ("Unknown action: %s. Use \" gitlab %s help\" to get details." % (action , what ))
312
306
313
- sys .exit (0 )
307
+ sys .exit (0 )
0 commit comments