Skip to content

Commit 39a4a20

Browse files
author
Gauvain Pocentek
committed
gitlab: autogenerate some doc
1 parent 9ca47aa commit 39a4a20

File tree

2 files changed

+103
-18
lines changed

2 files changed

+103
-18
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ the action:
9898
gitlab project list
9999
`````
100100

101-
The usable objects are those which inherits GitlabObject (yes, the source is
102-
the doc ATM), with a bit of string transformation (Project => project,
103-
ProjectIssue => project-issue, ...).
101+
Get help with:
104102

105-
The actions are list, get, create, update, delete.
103+
`````
104+
# global help
105+
gitlab --help
106+
107+
# object help
108+
gitlab project help
109+
`````
106110

107111
Some examples:
108112

gitlab

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,105 @@
1818

1919
import os
2020
import sys
21+
import re
2122

2223
try:
2324
from ConfigParser import ConfigParser
2425
except:
2526
from configparser import ConfigParser
2627

27-
from inspect import getmro
28+
from inspect import getmro, getmembers, isclass
2829

2930
import gitlab
3031

32+
camel_re = re.compile('(.)([A-Z])')
33+
3134
def die(msg):
3235
sys.stderr.write(msg + "\n")
3336
sys.exit(1)
3437

38+
def whatToCls(what):
39+
return "".join([s.capitalize() for s in what.split("-")])
40+
41+
def clsToWhat(cls):
42+
return camel_re.sub(r'\1-\2', cls.__name__).lower()
43+
44+
def actionHelpList(cls):
45+
l = []
46+
for action in 'list', 'get', 'create', 'update', 'delete':
47+
attr = 'can' + action.capitalize()
48+
try:
49+
y = cls.__dict__[attr]
50+
except:
51+
y = gitlab.GitlabObject.__dict__[attr]
52+
if not y:
53+
continue
54+
55+
detail = ''
56+
if action == 'list':
57+
detail = " ".join(["--%s=ARG" % x for x in cls.requiredListAttrs])
58+
elif action in ['get', 'delete']:
59+
detail = "--id=ARG "
60+
detail += " ".join(["--%s=ARG" % x for x in cls.requiredGetAttrs])
61+
elif action == 'create':
62+
detail = " ".join(["--%s=ARG" % x for x in cls.requiredCreateAttrs])
63+
if detail:
64+
detail += " "
65+
detail += " ".join(["[--%s=ARG]" % x for x in cls.optionalCreateAttrs])
66+
elif action == 'update':
67+
detail = " ".join(["[--%s=ARG]" % x for x in cls.requiredCreateAttrs])
68+
if detail:
69+
detail += " "
70+
detail += " ".join(["[--%s=ARG]" % x for x in cls.optionalCreateAttrs])
71+
l.append("%s %s" % (action, detail))
72+
73+
return (l)
74+
75+
def usage():
76+
print("usage: gitlab [--help] [--gitlab=GITLAB] what action [options]")
77+
print("")
78+
print("--gitlab=GITLAB: Specifies which python-gitlab.cfg configuration section should be used.")
79+
print(" If not defined, the default selection will be used.")
80+
print("")
81+
print("--help : Displays this message.")
82+
print("")
83+
print("Available `options` depend on which what/action couple is used.")
84+
print("If `action` is \"help\", available actions and options will be listed for `what`.")
85+
print("")
86+
print("Available `what` values are:")
87+
88+
classes = []
89+
for name, o in getmembers(gitlab):
90+
if not isclass(o):
91+
continue
92+
if gitlab.GitlabObject in getmro(o) and o != gitlab.GitlabObject:
93+
classes.append(o)
94+
95+
def s(a, b):
96+
if a.__name__ < b.__name__:
97+
return -1
98+
elif a.__name__ > b.__name__:
99+
return 1
100+
101+
classes.sort(cmp=s)
102+
for cls in classes:
103+
print(" %s" % clsToWhat(cls))
104+
105+
35106
gitlab_id = None
36107

37108
args = []
38109
d = {}
39110
for arg in sys.argv[1:]:
40111
if arg.startswith('--'):
112+
arg = arg[2:]
113+
114+
if arg == 'help':
115+
usage()
116+
sys.exit(0)
117+
41118
k, v = arg.split('=', 2)
42-
k = k[2:].strip()
119+
k = k.strip()
43120
v = v.strip()
44121

45122
if k == 'gitlab':
@@ -66,23 +143,14 @@ try:
66143
except:
67144
die("Impossible to get gitlab informations from configuration (%s)" % gitlab_id)
68145

69-
try:
70-
gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token)
71-
gl.auth()
72-
except:
73-
die("Could not connect to GitLab (%s)" % gitlab_url)
74-
75146
try:
76147
what = args.pop(0)
77148
action = args.pop(0)
78149
except:
79150
die("Missing arguments")
80151

81-
if action not in ['get', 'list', 'update', 'create', 'delete']:
82-
die("Unknown action: %s" % action)
83-
84-
def whatToCls(what):
85-
return "".join([s.capitalize() for s in what.split("-")])
152+
if action not in ['get', 'list', 'update', 'create', 'delete', 'help']:
153+
die("Unknown action: %s. Use \"gitlab %s help\" to get details." % (action, what))
86154

87155
try:
88156
cls = gitlab.__dict__[whatToCls(what)]
@@ -92,6 +160,19 @@ except:
92160
if gitlab.GitlabObject not in getmro(cls):
93161
die("Unknown object: %s" % what)
94162

163+
if action == "help":
164+
print("%s options:" % what)
165+
for item in actionHelpList(cls):
166+
print(" %s %s" % (what, item))
167+
168+
sys.exit(0)
169+
170+
try:
171+
gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token)
172+
gl.auth()
173+
except:
174+
die("Could not connect to GitLab (%s)" % gitlab_url)
175+
95176
if action == "create":
96177
if not cls.canCreate:
97178
die("%s objects can't be created" % what)
@@ -117,7 +198,7 @@ elif action == "list":
117198

118199
for o in l:
119200
o.pretty_print()
120-
print
201+
print("")
121202

122203
sys.exit(0)
123204

0 commit comments

Comments
 (0)