Skip to content

Commit 33c771d

Browse files
author
Gauvain Pocentek
committed
rework the script code organization
1 parent 02bd7cd commit 33c771d

File tree

2 files changed

+97
-97
lines changed

2 files changed

+97
-97
lines changed

gitlab

Lines changed: 91 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,85 @@ def usage():
111111
for cls in classes:
112112
print(" %s" % clsToWhat(cls))
113113

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+
114193

115194
gitlab_id = None
116195
verbose = False
@@ -186,9 +265,6 @@ try:
186265
except:
187266
die("Missing arguments. Use `gitlab -h` for help.")
188267

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-
192268
try:
193269
cls = gitlab.__dict__[whatToCls(what)]
194270
except:
@@ -204,110 +280,28 @@ if action == "help":
204280

205281
sys.exit(0)
206282

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()
212284

213285
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)
229288

230289
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)
244292
print("")
245293

246-
sys.exit(0)
247-
248294
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)
270297

271298
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)
291300

292301
elif action == "update":
293-
if not cls.canDelete:
294-
die("%s objects can't be updated" % what)
302+
o = do_update(cls, d)
295303

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))
312306

313-
sys.exit(0)
307+
sys.exit(0)

gitlab.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,12 @@ def __init__(self, gl, data=None, **kwargs):
490490
def __str__(self):
491491
return '%s => %s' % (type(self), str(self.__dict__))
492492

493+
def display(self, pretty):
494+
if pretty:
495+
self.pretty_print()
496+
else:
497+
self.short_print()
498+
493499
def short_print(self, depth=0):
494500
id = self.__dict__[self.idAttr]
495501
print("%s%s: %s" % (" " * depth * 2, self.idAttr, id))

0 commit comments

Comments
 (0)