Skip to content

Commit 8bf53c8

Browse files
committed
fix(cli): warn user when no fields are displayed
1 parent ce84f2e commit 8bf53c8

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

gitlab/v4/cli.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -482,23 +482,35 @@ def display_dict(d: Dict[str, Any], padding: int) -> None:
482482
if obj._id_attr:
483483
attrs.pop(obj._id_attr)
484484
display_dict(attrs, padding)
485+
return
485486

486-
else:
487-
if TYPE_CHECKING:
488-
assert isinstance(obj, gitlab.base.RESTObject)
489-
if obj._id_attr:
490-
id = getattr(obj, obj._id_attr)
491-
print(f"{obj._id_attr.replace('_', '-')}: {id}")
492-
if obj._repr_attr:
493-
value = getattr(obj, obj._repr_attr, "None") or "None"
494-
value = value.replace("\r", "").replace("\n", " ")
495-
# If the attribute is a note (ProjectCommitComment) then we do
496-
# some modifications to fit everything on one line
497-
line = f"{obj._repr_attr}: {value}"
498-
# ellipsize long lines (comments)
499-
if len(line) > 79:
500-
line = f"{line[:76]}..."
501-
print(line)
487+
lines = []
488+
489+
if TYPE_CHECKING:
490+
assert isinstance(obj, gitlab.base.RESTObject)
491+
492+
if obj._id_attr:
493+
id = getattr(obj, obj._id_attr)
494+
lines.append(f"{obj._id_attr.replace('_', '-')}: {id}")
495+
if obj._repr_attr:
496+
value = getattr(obj, obj._repr_attr, "None") or "None"
497+
value = value.replace("\r", "").replace("\n", " ")
498+
# If the attribute is a note (ProjectCommitComment) then we do
499+
# some modifications to fit everything on one line
500+
line = f"{obj._repr_attr}: {value}"
501+
# ellipsize long lines (comments)
502+
if len(line) > 79:
503+
line = f"{line[:76]}..."
504+
lines.append(line)
505+
506+
if lines:
507+
print("\n".join(lines))
508+
return
509+
510+
print(
511+
f"No default fields to show for {obj!r}. "
512+
f"Please use '--verbose' or the JSON/YAML formatters."
513+
)
502514

503515
def display_list(
504516
self,

tests/functional/cli/test_cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,13 @@ def test_cli_fields_in_list(gitlab_cli, project_file, format, loader):
182182

183183
content = loader(ret.stdout.strip())
184184
assert ["default_branch" in item for item in content]
185+
186+
187+
def test_cli_display_without_fields_warns(gitlab_cli, project):
188+
cmd = ["project-ci-lint", "get", "--project-id", project.id]
189+
190+
ret = gitlab_cli(cmd)
191+
assert ret.success
192+
193+
assert "No default fields to show" in ret.stdout
194+
assert "merged_yaml" not in ret.stdout

0 commit comments

Comments
 (0)