| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9783207 commit abade40
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,10 +80,11 @@ section. | |||
| 80 | 80 | * - ``url`` | |
| 81 | 81 | - URL for the GitLab server | |
| 82 | 82 | * - ``private_token`` | |
| 83 | - - Your user token. Login/password is not supported. | ||
| 84 | - Refer `the official documentation`__ to learn how to obtain a token. | ||
| 83 | + - Your user token. Login/password is not supported. Refer to `the official | ||
| 84 | + documentation`__ to learn how to obtain a token. | ||
| 85 | 85 | * - ``api_version`` | |
| 86 | - - API version to use (``3`` or ``4``), defaults to ``3`` | ||
| 86 | + - GitLab API version to use (``3`` or ``4``). Defaults to ``3`` for now, | ||
| 87 | + but will switch to ``4`` eventually. | ||
| 87 | 88 | * - ``http_username`` | |
| 88 | 89 | - Username for optional HTTP authentication | |
| 89 | 90 | * - ``http_password`` | |
@@ -126,19 +127,27 @@ Use the following optional arguments to change the behavior of ``gitlab``. | |||
| 126 | 127 | These options must be defined before the mandatory arguments. | |
| 127 | 128 | ||
| 128 | 129 | ``--verbose``, ``-v`` | |
| 129 | - Outputs detail about retrieved objects. | ||
| 130 | + Outputs detail about retrieved objects. Available for legacy (default) | ||
| 131 | + output only. | ||
| 130 | 132 | ||
| 131 | 133 | ``--config-file``, ``-c`` | |
| 132 | 134 | Path to a configuration file. | |
| 133 | 135 | ||
| 134 | 136 | ``--gitlab``, ``-g`` | |
| 135 | 137 | ID of a GitLab server defined in the configuration file. | |
| 136 | 138 | ||
| 139 | + ``--output``, ``-o`` | ||
| 140 | + Output format. Defaults to a custom format. Can also be ``yaml`` or ``json``. | ||
| 141 | + | ||
| 142 | + ``--fields``, ``-f`` | ||
| 143 | + Comma-separated list of fields to display (``yaml`` and ``json`` formats | ||
| 144 | + only). If not used, all the object fields are displayed. | ||
| 145 | + | ||
| 137 | 146 | Example: | |
| 138 | 147 | ||
| 139 | 148 | .. code-block:: console | |
| 140 | 149 | ||
| 141 | - $ gitlab -v -g elsewhere -c /tmp/gl.cfg project list | ||
| 150 | + $ gitlab -o yaml -f id,permissions -g elsewhere -c /tmp/gl.cfg project list | ||
| 142 | 151 | ||
| 143 | 152 | ||
| 144 | 153 | Examples | |
@@ -168,12 +177,11 @@ Get a specific project (id 2): | |||
| 168 | 177 | ||
| 169 | 178 | $ gitlab project get --id 2 | |
| 170 | 179 | ||
| 171 | - Get a specific user by id or by username: | ||
| 180 | + Get a specific user by id: | ||
| 172 | 181 | ||
| 173 | 182 | .. code-block:: console | |
| 174 | 183 | ||
| 175 | 184 | $ gitlab user get --id 3 | |
| 176 | - $ gitlab user get-by-username --query jdoe | ||
| 177 | 185 | ||
| 178 | 186 | Get a list of snippets for this project: | |
| 179 | 187 | ||
@@ -200,7 +208,6 @@ Create a snippet: | |||
| 200 | 208 | ||
| 201 | 209 | $ gitlab project-snippet create --project-id 2 | |
| 202 | 210 | Impossible to create object (Missing attribute(s): title, file-name, code) | |
| 203 | - | ||
| 204 | 211 | $ # oops, let's add the attributes: | |
| 205 | 212 | $ gitlab project-snippet create --project-id 2 --title "the title" \ | |
| 206 | 213 | --file-name "the name" --code "the code" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,7 +51,6 @@ def wrapped_f(*args, **kwargs): | |||
| 51 | 51 | custom_actions[final_name] = {} | |
| 52 | 52 | ||
| 53 | 53 | action = f.__name__ | |
| 54 | - | ||
| 55 | 54 | custom_actions[final_name][action] = (mandatory, optional, in_obj) | |
| 56 | 55 | ||
| 57 | 56 | return wrapped_f | |
@@ -79,7 +78,7 @@ def _get_base_parser(): | |||
| 79 | 78 | parser.add_argument("--version", help="Display the version.", | |
| 80 | 79 | action="store_true") | |
| 81 | 80 | parser.add_argument("-v", "--verbose", "--fancy", | |
| 82 | - help="Verbose mode", | ||
| 81 | + help="Verbose mode (legacy format only)", | ||
| 83 | 82 | action="store_true") | |
| 84 | 83 | parser.add_argument("-d", "--debug", | |
| 85 | 84 | help="Debug mode (display HTTP requests", | |
@@ -92,6 +91,15 @@ def _get_base_parser(): | |||
| 92 | 91 | "be used. If not defined, the default selection " | |
| 93 | 92 | "will be used."), | |
| 94 | 93 | required=False) | |
| 94 | + parser.add_argument("-o", "--output", | ||
| 95 | + help=("Output format (v4 only): json|legacy|yaml"), | ||
| 96 | + required=False, | ||
| 97 | + choices=['json', 'legacy', 'yaml'], | ||
| 98 | + default="legacy") | ||
| 99 | + parser.add_argument("-f", "--fields", | ||
| 100 | + help=("Fields to display in the output (comma " | ||
| 101 | + "separated). Not used with legacy output"), | ||
| 102 | + required=False) | ||
| 95 | 103 | ||
| 96 | 104 | return parser | |
| 97 | 105 | ||
@@ -117,14 +125,18 @@ def main(): | |||
| 117 | 125 | config_files = args.config_file | |
| 118 | 126 | gitlab_id = args.gitlab | |
| 119 | 127 | verbose = args.verbose | |
| 128 | + output = args.output | ||
| 129 | + fields = [] | ||
| 130 | + if args.fields: | ||
| 131 | + fields = [x.strip() for x in args.fields.split(',')] | ||
| 120 | 132 | debug = args.debug | |
| 121 | 133 | action = args.action | |
| 122 | 134 | what = args.what | |
| 123 | 135 | ||
| 124 | 136 | args = args.__dict__ | |
| 125 | 137 | # Remove CLI behavior-related args | |
| 126 | 138 | for item in ('gitlab', 'config_file', 'verbose', 'debug', 'what', 'action', | |
| 127 | - 'version'): | ||
| 139 | + 'version', 'output'): | ||
| 128 | 140 | args.pop(item) | |
| 129 | 141 | args = {k: v for k, v in args.items() if v is not None} | |
| 130 | 142 | ||
@@ -137,6 +149,6 @@ def main(): | |||
| 137 | 149 | if debug: | |
| 138 | 150 | gl.enable_debug() | |
| 139 | 151 | ||
| 140 | - cli_module.run(gl, what, action, args, verbose) | ||
| 152 | + cli_module.run(gl, what, action, args, verbose, output, fields) | ||
| 141 | 153 | ||
| 142 | 154 | sys.exit(0) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -245,30 +245,47 @@ def extend_parser(parser): | |||
| 245 | 245 | return parser | |
| 246 | 246 | ||
| 247 | 247 | ||
| 248 | + class JSONPrinter(object): | ||
| 249 | + def display(self, d, **kwargs): | ||
| 250 | + import json # noqa | ||
| 251 | + | ||
| 252 | + print(json.dumps(d)) | ||
| 253 | + | ||
| 254 | + | ||
| 255 | + class YAMLPrinter(object): | ||
| 256 | + def display(self, d, **kwargs): | ||
| 257 | + import yaml # noqa | ||
| 258 | + | ||
| 259 | + print(yaml.safe_dump(d, default_flow_style=False)) | ||
| 260 | + | ||
| 261 | + | ||
| 248 | 262 | class LegacyPrinter(object): | |
| 249 | - def display(self, obj, verbose=False, padding=0): | ||
| 250 | - def display_dict(d): | ||
| 263 | + def display(self, d, **kwargs): | ||
| 264 | + verbose = kwargs.get('verbose', False) | ||
| 265 | + padding = kwargs.get('padding', 0) | ||
| 266 | + obj = kwargs.get('obj') | ||
| 267 | + | ||
| 268 | + def display_dict(d, padding): | ||
| 251 | 269 | for k in sorted(d.keys()): | |
| 252 | 270 | v = d[k] | |
| 253 | 271 | if isinstance(v, dict): | |
| 254 | 272 | print('%s%s:' % (' ' * padding, k)) | |
| 255 | 273 | new_padding = padding + 2 | |
| 256 | - self.display(v, True, new_padding) | ||
| 274 | + self.display(v, verbose=True, padding=new_padding, obj=v) | ||
| 257 | 275 | continue | |
| 258 | 276 | print('%s%s: %s' % (' ' * padding, k, v)) | |
| 259 | 277 | ||
| 260 | 278 | if verbose: | |
| 261 | 279 | if isinstance(obj, dict): | |
| 262 | - display_dict(obj) | ||
| 280 | + display_dict(obj, padding) | ||
| 263 | 281 | return | |
| 264 | 282 | ||
| 265 | 283 | # not a dict, we assume it's a RESTObject | |
| 266 | - id = getattr(obj, obj._id_attr) | ||
| 284 | + id = getattr(obj, obj._id_attr, None) | ||
| 267 | 285 | print('%s: %s' % (obj._id_attr, id)) | |
| 268 | 286 | attrs = obj.attributes | |
| 269 | 287 | attrs.pop(obj._id_attr) | |
| 270 | - display_dict(attrs) | ||
| 271 | - print('') | ||
| 288 | + display_dict(attrs, padding) | ||
| 272 | 289 | ||
| 273 | 290 | else: | |
| 274 | 291 | id = getattr(obj, obj._id_attr) | |
@@ -278,19 +295,33 @@ def display_dict(d): | |||
| 278 | 295 | print('%s: %s' % (obj._short_print_attr, value)) | |
| 279 | 296 | ||
| 280 | 297 | ||
| 281 | - def run(gl, what, action, args, verbose): | ||
| 298 | + PRINTERS = { | ||
| 299 | + 'json': JSONPrinter, | ||
| 300 | + 'legacy': LegacyPrinter, | ||
| 301 | + 'yaml': YAMLPrinter, | ||
| 302 | + } | ||
| 303 | + | ||
| 304 | + | ||
| 305 | + def run(gl, what, action, args, verbose, output, fields): | ||
| 282 | 306 | g_cli = GitlabCLI(gl, what, action, args) | |
| 283 | 307 | ret_val = g_cli() | |
| 284 | 308 | ||
| 285 | - printer = LegacyPrinter() | ||
| 309 | + printer = PRINTERS[output]() | ||
| 310 | + | ||
| 311 | + def get_dict(obj): | ||
| 312 | + if fields: | ||
| 313 | + return {k: v for k, v in obj.attributes.items() | ||
| 314 | + if k in fields} | ||
| 315 | + return obj.attributes | ||
| 286 | 316 | ||
| 287 | 317 | if isinstance(ret_val, list): | |
| 288 | - for o in ret_val: | ||
| 289 | - if isinstance(o, gitlab.base.RESTObject): | ||
| 290 | - printer.display(o, verbose) | ||
| 318 | + for obj in ret_val: | ||
| 319 | + if isinstance(obj, gitlab.base.RESTObject): | ||
| 320 | + printer.display(get_dict(obj), verbose=verbose, obj=obj) | ||
| 291 | 321 | else: | |
| 292 | - print(o) | ||
| 322 | + print(obj) | ||
| 323 | + print('') | ||
| 293 | 324 | elif isinstance(ret_val, gitlab.base.RESTObject): | |
| 294 | - printer.display(ret_val, verbose) | ||
| 325 | + printer.display(get_dict(ret_val), verbose=verbose, obj=ret_val) | ||
| 295 | 326 | elif isinstance(ret_val, six.string_types): | |
| 296 | 327 | print(ret_val) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments