Skip to content

Commit 23753df

Browse files
author
Gauvain Pocentek
committed
Merge branch 'master' into debian
2 parents ef44b84 + 39a4a20 commit 23753df

File tree

2 files changed

+113
-22
lines changed

2 files changed

+113
-22
lines changed

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,41 @@ The first argument is the object type on which we will act, the second one is
9595
the action:
9696

9797
`````
98-
gitlab Project list
98+
gitlab project list
9999
`````
100100

101-
The usable objects are those which inherits GitlabObject (yes, the source is
102-
the doc ATM), and the actions are list, get, create, update, delete.
101+
Get help with:
102+
103+
`````
104+
# global help
105+
gitlab --help
106+
107+
# object help
108+
gitlab project help
109+
`````
103110

104111
Some examples:
105112

106113
`````bash
107114
# list all the projects:
108-
gitlab Project list
115+
gitlab project list
109116

110117
# get a specific project (id 2):
111-
gitlab Project get --id=2
118+
gitlab project get --id=2
112119

113120
# get a list of snippets for this project:
114-
gitlab ProjectIssue list --project_id=2
121+
gitlab project-issue list --project_id=2
115122

116123
# delete a Snippet (id 3):
117-
gitlab ProjectSnippet delete --id=3 --project_id=2
124+
gitlab project-snippet delete --id=3 --project_id=2
118125

119126
# update a Snippet:
120-
gitlab ProjectSnippet update --id=4 --project_id=2 --code="My New Code"
127+
gitlab project-snippet update --id=4 --project_id=2 --code="My New Code"
121128

122129
# create a Snippet:
123-
gitlab ProjectSnippet create --project_id=2
130+
gitlab project-snippet create --project_id=2
124131
Impossible to create object (Missing attribute(s): title, file_name, code)
125132

126133
# oops, let's add the attributes:
127-
gitlab ProjectSnippet create --project_id=2 --title="the title" --file_name="the name" --code="the code"
134+
gitlab project-snippet create --project_id=2 --title="the title" --file_name="the name" --code="the code"
128135
`````

gitlab

Lines changed: 96 additions & 12 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,29 +143,36 @@ 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)
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))
83154

84155
try:
85-
cls = gitlab.__dict__[what]
156+
cls = gitlab.__dict__[whatToCls(what)]
86157
except:
87158
die("Unknown object: %s" % what)
88159

89160
if gitlab.GitlabObject not in getmro(cls):
90161
die("Unknown object: %s" % what)
91162

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+
92176
if action == "create":
93177
if not cls.canCreate:
94178
die("%s objects can't be created" % what)
@@ -114,7 +198,7 @@ elif action == "list":
114198

115199
for o in l:
116200
o.pretty_print()
117-
print
201+
print("")
118202

119203
sys.exit(0)
120204

0 commit comments

Comments
 (0)