Add --json flag to format rbt output as json.
Review Request #9245 — Created Oct. 3, 2017 and discarded
When running the
rbt
command from a script, it is difficult to parse
the output log messages in order to get necessary information from the
command that was run.This change adds a
--json
flag to allrbt
commands to change the
output to json to make command output parsing easier from a script.The
rbt diff
command hasn't been changed because diff output is
expected on stdout.
All rbt tests passed.
Description | From | Last Updated |
---|---|---|
I think this would be cleaner if we build repository_type_list in one loop (or even better, a list comprehension), and … |
david | |
We should use six.text_type() instead of str() for future Python 3 compatibility. Here and lots of other places throughout your … |
david | |
Your output in this command is a little inconsistent. Here you have file_path as the key but below you use … |
david | |
I suspect we want to change these uses of logging.info() to be print() like the other commands. |
david | |
I suspect we want to change these uses of logging.info() to be print() like the other commands. |
david | |
How does this change the output? |
david | |
By printing here, it looks like we'll end up printing multiple JSON objects out? That doesn't seem correct. Instead of … |
david | |
Can you put the { on the next line and then indent things within the dict another 4 spaces? |
david | |
} should be on a separate line from the for .... We should also add a trailing comma after the … |
david | |
It would be nice to pass in the indent parameter to json.dumps so that we get easier-to-read output. |
david | |
This is a utility method called from within other commands. If this ends up printing output it seems like we … |
david | |
I think we probably want to refactor this so that the code in rbtools/clients/ returns the data, and then rbtools/commands/list_repo_types.py … |
david | |
These changes seem unrelated. Why did you do this? |
david | |
This should have an "Args" section explaining what obj is. The summary line also needs to end in a period. |
david | |
This should use the standard "Returns:" section, plus it doesn't return a bool, it returns a dict with some fields. … |
david | |
Instead of defining this as a variable and then manipulating it throughout, because we only have two cases, how about … |
david | |
Perhaps we want to rename this method now? |
david | |
Should use the standard Args/Returns sections. |
david | |
Kind of silly to define a variable and immediately return it. Lets just do return [ ... ] |
david | |
This can be a bit less wordy/more efficient as: json_output_dict.extend({ 'squash': self.options.squash, 'merge': not self.options.squash, 'source_branch': branch_name, 'destination_branch': destination_branch, }) |
david | |
Same here re: dict.extend |
david | |
While we don't localize rbtools right now, it's possible we will in the future, and this won't be translatable. We … |
david | |
Same here with msg_input not being localizable. |
david | |
Anywhere we're using msg_input here should have a conditional with separate strings. |
david | |
We generally prefer parens for wrapping this sort of thing instead of the line continuation character. We probably also want … |
david | |
su before sy. |
david | |
We generally document these like this: Returns: dict: A dictionary with the following keys: ``success``: A boolean value for whether … |
david | |
Should probably be "List of dicts, each containing name, tool_name, and detected fields." |
david | |
Other commands include a success field. We probably want that here even though it will always be True. |
david | |
Can use json_output_dict.extend to do this all in one go. |
david | |
Can use json_output_dict.extend to do this all in one go. |
david | |
And here. |
david | |
And finally here. |
david | |
Type should be unicode, not string (string isn't a python type) |
david | |
We need to list the types of config and options (dict and object, respectively). There should also be a blank … |
david |
- Change Summary:
-
Add json output formatting to more rbt subcommands.
- Testing Done:
-
+ All rbt tests passed.
- Commit:
-
9ea80bebc687330b8bbf24baebb0f3d10ef2312c083a0e6dda71b992a2adb9fde9aa199257850e7d
Checks run (2 succeeded)
- Change Summary:
-
Add json formatted error output to some rbt commands.
- Commit:
-
083a0e6dda71b992a2adb9fde9aa199257850e7d5c155c735b6c56743c4c146262afe8f9c83db5db
Checks run (2 succeeded)
- Change Summary:
-
Add json formatted error output to more rbt commands.
- Commit:
-
5c155c735b6c56743c4c146262afe8f9c83db5db987fe5eb378687bda29964142f41ae35a558bfb0
Checks run (2 succeeded)
-
-
I think this would be cleaner if we build repository_type_list in one loop (or even better, a list comprehension), and then we can either print it out directly as json or loop through it again to print in plain-text:
repository_type_list = [ { 'name': name, 'tool_name': tool.name, 'detected': bool(tool.get_repository_info()), } for name, tool in six.iteritems(SCMCLIENTS) ] if options.json: print_json({ 'repository_types': repository_type_list, }) else: for repository_type in repository_type_list: if repository_type['detected']: dectected = '*' else: dectected = ' ' print(' %s "%s": %s' % (detected, repository_type['name'], repository_type['tool_name']))
-
We should use
six.text_type()
instead ofstr()
for future Python 3 compatibility. Here and lots of other places throughout your change. -
Your output in this command is a little inconsistent. Here you have
file_path
as the key but below you usepath_to_file
. -
-
-
-
By printing here, it looks like we'll end up printing multiple JSON objects out? That doesn't seem correct. Instead of splatting in a
print_json()
call anywhere there might be aprint()
statement we should probably collect the information about what happened and then only print at the very end. -
-
-
It would be nice to pass in the
indent
parameter tojson.dumps
so that we get easier-to-read output.
- Change Summary:
-
Change
str()
tosix.text_type()
for python 3 compatibility and formatting fixes. - Commit:
-
987fe5eb378687bda29964142f41ae35a558bfb02679e6510c7ab888edd5a8b94a1112a16eaba998
Checks run (2 succeeded)
- Change Summary:
-
Remove json formatting of warning messages in
rbt post
and changeprint()
tologging.warning()
. - Commit:
-
2679e6510c7ab888edd5a8b94a1112a16eaba9987ba2b3f63e8f002f360d3b80bc170086c902e127
Checks run (2 succeeded)
- Change Summary:
-
Clean up inconsistencies in review request and update confirm to use stderr.
- Description:
-
When running the
rbt
command from a script, it is difficult to parsethe output log messages in order to get necessary information from the command that was run. This change adds a
--json
flag to allrbt
commands to change theoutput to json to make command output parsing easier from a script. + + The
rbt diff
command hasn't been changed because diff output is+ expected on stdout. - Commit:
-
7ba2b3f63e8f002f360d3b80bc170086c902e1277fcc1a080f32de1e791fa32925c24ecd371c8ef1
Checks run (2 succeeded)
- Change Summary:
-
Update
api-get
to only return api response when passed--json
flag and fixclear-cache
. - Commit:
-
7fcc1a080f32de1e791fa32925c24ecd371c8ef1127967c524223e320ff71c11c93212bb96d7cce4
Checks run (2 succeeded)
-
-
This is a utility method called from within other commands. If this ends up printing output it seems like we might end up with multiple JSON objects printed, which won't parse correctly. We should only be calling
print_json
from things insiderbtools/commands/*.py
-
I think we probably want to refactor this so that the code in
rbtools/clients/
returns the data, and thenrbtools/commands/list_repo_types.py
can actually do the printing. Anything that's "library" code shouldn't print JSON, since we might end up printing more than one JSON object. -
-
This should have an "Args" section explaining what
obj
is. The summary line also needs to end in a period.
- Change Summary:
-
Move output printing into commands scripts and fix docstring for
print_json
based on review feedback. - Commit:
-
127967c524223e320ff71c11c93212bb96d7cce49e132a54c4b8dad8a613cc64dce0e8f6aa07e935
Checks run (2 succeeded)
-
-
This should use the standard "Returns:" section, plus it doesn't return a bool, it returns a dict with some fields.
This also needs an "Args" section explaining what
cache_path
is. -
Instead of defining this as a variable and then manipulating it throughout, because we only have two cases, how about something like this:
try: os.unlink(cache_path) return { 'success': True, 'cache_path': cache_path, } except Exception as e: logging.error(...) return { 'success': False, 'error_message': 'Could not clear cache', 'exception_message': six.text_type(e), }
-
-
-
-
This can be a bit less wordy/more efficient as:
json_output_dict.extend({ 'squash': self.options.squash, 'merge': not self.options.squash, 'source_branch': branch_name, 'destination_branch': destination_branch, })
-
-
While we don't localize rbtools right now, it's possible we will in the future, and this won't be translatable. We should define separate format strings in the conditional, even though they're substantially similar:
if was_authenticated: api_client.logout() message_fmt = 'You are now logged out of Review Board at %s' else: message_fmt = 'You are already logged out of Review Board at %s' message = message_fmt % (api_client.domain)
-
-
-
We generally prefer parens for wrapping this sort of thing instead of the line continuation character. We probably also want the format parameters on a separate line:
error_msg = ('No %s repository found or selected for %s. %s not ' 'created.' % (tool.name, server, CONFIG_FILE))
-
- Change Summary:
-
Update review with changes requested.
- Commit:
-
9e132a54c4b8dad8a613cc64dce0e8f6aa07e9357e949ebf055731a6398b5db6499f9b519993fd87
Checks run (2 succeeded)
- Change Summary:
-
Replace line continuation backslashes for brackets around strings.
- Commit:
-
7e949ebf055731a6398b5db6499f9b519993fd87617479d5773efb935bf17daee2dbe907de2cf879
Checks run (2 succeeded)
-
Just a few more nits to pick.
-
We generally document these like this:
Returns: dict: A dictionary with the following keys: ``success``: A boolean value for whether the cache was cleared. ``cache_path``: The path of the cache file that was cleared. In the case of failure, this will also include two additional keys: ``error_message``: A user-visible error message. ``exception_message``: The message from the exception that caused the error.
-
-
Other commands include a
success
field. We probably want that here even though it will always beTrue
. -
-
-
-
- Change Summary:
-
Fix documentation and code style based on review feedback.
- Commit:
-
617479d5773efb935bf17daee2dbe907de2cf879a85bd455c0911f06ac75b6711dd014f04c3a9f59
Checks run (2 succeeded)
- Change Summary:
-
Fix documentation formatting.
- Commit:
-
a85bd455c0911f06ac75b6711dd014f04c3a9f59530f5d8a8c719ccf362f6efa608982e7bfb98b0d