Skip to content

Commit 3b15c6d

Browse files
author
Gauvain Pocentek
committed
gitlab: be less verbose by default
Provide a --fancy option to output more data on list/get/create queries.
1 parent 41b6dba commit 3b15c6d

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

gitlab

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ def actionHelpList(cls):
7373
return (l)
7474

7575
def usage():
76-
print("usage: gitlab [--help] [--gitlab=GITLAB] what action [options]")
76+
print("usage: gitlab [--help] [--gitlab=GITLAB] [--fancy] what action [options]")
7777
print("")
7878
print("--gitlab=GITLAB: Specifies which python-gitlab.cfg configuration section should be used.")
7979
print(" If not defined, the default selection will be used.")
80-
print("")
80+
print("--fancy : More verbose output.")
8181
print("--help : Displays this message.")
8282
print("")
8383
print("Available `options` depend on which what/action couple is used.")
@@ -104,6 +104,7 @@ def usage():
104104

105105

106106
gitlab_id = None
107+
verbose = False
107108

108109
args = []
109110
d = {}
@@ -114,6 +115,9 @@ for arg in sys.argv[1:]:
114115
if arg == 'help':
115116
usage()
116117
sys.exit(0)
118+
elif arg == 'fancy':
119+
verbose = True
120+
continue
117121

118122
k, v = arg.split('=', 2)
119123
k = k.strip()
@@ -183,7 +187,10 @@ if action == "create":
183187
except Exception as e:
184188
die("Impossible to create object (%s)" % str(e))
185189

186-
o.pretty_print()
190+
if verbose:
191+
o.pretty_print()
192+
else:
193+
o.short_print()
187194

188195
sys.exit(0)
189196

@@ -197,7 +204,10 @@ elif action == "list":
197204
die("Impossible to list objects (%s)" % str(e))
198205

199206
for o in l:
200-
o.pretty_print()
207+
if verbose:
208+
o.pretty_print()
209+
else:
210+
o.short_print()
201211
print("")
202212

203213
sys.exit(0)
@@ -216,7 +226,10 @@ elif action == "get":
216226
except Exception as e:
217227
die("Impossible to get object (%s)" % str(e))
218228

219-
o.pretty_print()
229+
if verbose:
230+
o.pretty_print()
231+
else:
232+
o.short_print()
220233

221234
sys.exit(0)
222235

gitlab.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ class GitlabObject(object):
397397
requiredCreateAttrs = []
398398
optionalCreateAttrs = []
399399
idAttr = 'id'
400+
shortPrintAttr = None
400401

401402
@classmethod
402403
def list(cls, gl, **kwargs):
@@ -486,6 +487,14 @@ def __init__(self, gl, data=None, **kwargs):
486487
def __str__(self):
487488
return '%s => %s' % (type(self), str(self.__dict__))
488489

490+
def short_print(self, depth=0):
491+
id = self.__dict__[self.idAttr]
492+
print("%s%s: %s" % (" " * depth * 2, self.idAttr, id))
493+
if self.shortPrintAttr:
494+
print ("%s%s: %s" % (" " * depth * 2,
495+
self.shortPrintAttr.replace('_', '-'),
496+
self.__dict__[self.shortPrintAttr]))
497+
489498
def pretty_print(self, depth=0):
490499
id = self.__dict__[self.idAttr]
491500
print("%s%s: %s" % (" " * depth * 2, self.idAttr, id))
@@ -511,6 +520,7 @@ def json(self):
511520

512521
class User(GitlabObject):
513522
_url = '/users'
523+
shortPrintAttr = 'username'
514524
requiredCreateAttrs = ['email', 'password', 'username', 'name']
515525
optionalCreateAttrs = ['skype', 'linkedin', 'twitter', 'projects_limit',
516526
'extern_uid', 'provider', 'bio']
@@ -539,6 +549,7 @@ class Group(GitlabObject):
539549
_url = '/groups'
540550
_constructorTypes = {'projects': 'Project'}
541551
requiredCreateAttrs = ['name', 'path']
552+
shortPrintAttr = 'name'
542553

543554
def transfer_project(self, id):
544555
url = '/groups/%d/projects/%d?private_token=%s' % \
@@ -551,6 +562,7 @@ def transfer_project(self, id):
551562
class Hook(GitlabObject):
552563
_url = '/hooks'
553564
requiredCreateAttrs = ['url']
565+
shortPrintAttr = 'url'
554566

555567

556568
class Issue(GitlabObject):
@@ -561,6 +573,7 @@ class Issue(GitlabObject):
561573
canDelete = False
562574
canUpdate = False
563575
canCreate = False
576+
shortPrintAttr = 'title'
564577

565578

566579
class ProjectBranch(GitlabObject):
@@ -599,6 +612,7 @@ class ProjectCommit(GitlabObject):
599612
canUpdate = False
600613
canCreate = False
601614
requiredListAttrs = ['project_id']
615+
shortPrintAttr = 'title'
602616

603617

604618
class ProjectKey(GitlabObject):
@@ -614,6 +628,7 @@ class ProjectHook(GitlabObject):
614628
requiredListAttrs = ['project_id']
615629
requiredGetAttrs = ['project_id']
616630
requiredCreateAttrs = ['project_id', 'url']
631+
shortPrintAttr = 'url'
617632

618633

619634
class ProjectIssueNote(GitlabObject):
@@ -636,6 +651,7 @@ class ProjectIssue(GitlabObject):
636651
requiredCreateAttrs = ['project_id', 'title']
637652
optionalCreateAttrs = ['description', 'assignee_id', 'milestone_id',
638653
'labels']
654+
shortPrintAttr = 'title'
639655

640656
def Note(self, id=None, **kwargs):
641657
return self._getListOrObject(ProjectIssueNote, id,
@@ -650,6 +666,7 @@ class ProjectMember(GitlabObject):
650666
requiredListAttrs = ['project_id']
651667
requiredGetAttrs = ['project_id']
652668
requiredCreateAttrs = ['project_id', 'user_id', 'access_level']
669+
shortPrintAttr = 'username'
653670

654671

655672
class ProjectNote(GitlabObject):
@@ -664,11 +681,13 @@ class ProjectNote(GitlabObject):
664681

665682
class ProjectTag(GitlabObject):
666683
_url = '/projects/%(project_id)s/repository/tags'
684+
idAttr = 'name'
667685
canGet = False
668686
canDelete = False
669687
canUpdate = False
670688
canCreate = False
671689
requiredListAttrs = ['project_id']
690+
shortPrintAttr = 'name'
672691

673692

674693
class ProjectMergeRequestNote(GitlabObject):
@@ -704,6 +723,7 @@ class ProjectMilestone(GitlabObject):
704723
requiredGetAttrs = ['project_id']
705724
requiredCreateAttrs = ['project_id', 'title']
706725
optionalCreateAttrs = ['description', 'due_date']
726+
shortPrintAttr = 'title'
707727

708728

709729
class ProjectSnippetNote(GitlabObject):
@@ -723,6 +743,7 @@ class ProjectSnippet(GitlabObject):
723743
requiredGetAttrs = ['project_id']
724744
requiredCreateAttrs = ['project_id', 'title', 'file_name', 'code']
725745
optionalCreateAttrs = ['lifetime']
746+
shortPrintAttr = 'title'
726747

727748
def Content(self):
728749
url = "/projects/%(project_id)s/snippets/%(snippet_id)s/raw" % \
@@ -750,6 +771,7 @@ class Project(GitlabObject):
750771
optionalCreateAttrs = ['default_branch', 'issues_enabled', 'wall_enabled',
751772
'merge_requests_enabled', 'wiki_enabled',
752773
'namespace_id']
774+
shortPrintAttr = 'path'
753775

754776
def Branch(self, id=None, **kwargs):
755777
return self._getListOrObject(ProjectBranch, id,

0 commit comments

Comments
 (0)