diff options
author | Nejc Habjan <nejc.habjan@siemens.com> | 2023-04-07 20:01:09 +0200 |
---|---|---|
committer | Nejc Habjan <hab.nejc@gmail.com> | 2023-04-11 13:53:12 +0200 |
commit | 8bf53c8b31704bdb31ffc5cf107cc5fba5dad457 (patch) | |
tree | db987cb340e0544d655a5701822dae6442b22ca3 | |
parent | ce84f2e64a640e0d025a7ba3a436f347ad25e88e (diff) | |
download | gitlab-8bf53c8b31704bdb31ffc5cf107cc5fba5dad457.tar.gz |
fix(cli): warn user when no fields are displayed
-rw-r--r-- | gitlab/v4/cli.py | 44 | ||||
-rw-r--r-- | tests/functional/cli/test_cli.py | 10 |
2 files changed, 38 insertions, 16 deletions
diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py index 0c3a655..dbf0eb0 100644 --- a/gitlab/v4/cli.py +++ b/gitlab/v4/cli.py @@ -482,23 +482,35 @@ class LegacyPrinter: if obj._id_attr: attrs.pop(obj._id_attr) display_dict(attrs, padding) + return - else: - if TYPE_CHECKING: - assert isinstance(obj, gitlab.base.RESTObject) - if obj._id_attr: - id = getattr(obj, obj._id_attr) - print(f"{obj._id_attr.replace('_', '-')}: {id}") - if obj._repr_attr: - value = getattr(obj, obj._repr_attr, "None") or "None" - value = value.replace("\r", "").replace("\n", " ") - # If the attribute is a note (ProjectCommitComment) then we do - # some modifications to fit everything on one line - line = f"{obj._repr_attr}: {value}" - # ellipsize long lines (comments) - if len(line) > 79: - line = f"{line[:76]}..." - print(line) + lines = [] + + if TYPE_CHECKING: + assert isinstance(obj, gitlab.base.RESTObject) + + if obj._id_attr: + id = getattr(obj, obj._id_attr) + lines.append(f"{obj._id_attr.replace('_', '-')}: {id}") + if obj._repr_attr: + value = getattr(obj, obj._repr_attr, "None") or "None" + value = value.replace("\r", "").replace("\n", " ") + # If the attribute is a note (ProjectCommitComment) then we do + # some modifications to fit everything on one line + line = f"{obj._repr_attr}: {value}" + # ellipsize long lines (comments) + if len(line) > 79: + line = f"{line[:76]}..." + lines.append(line) + + if lines: + print("\n".join(lines)) + return + + print( + f"No default fields to show for {obj!r}. " + f"Please use '--verbose' or the JSON/YAML formatters." + ) def display_list( self, diff --git a/tests/functional/cli/test_cli.py b/tests/functional/cli/test_cli.py index 411d378..71f2377 100644 --- a/tests/functional/cli/test_cli.py +++ b/tests/functional/cli/test_cli.py @@ -182,3 +182,13 @@ def test_cli_fields_in_list(gitlab_cli, project_file, format, loader): content = loader(ret.stdout.strip()) assert ["default_branch" in item for item in content] + + +def test_cli_display_without_fields_warns(gitlab_cli, project): + cmd = ["project-ci-lint", "get", "--project-id", project.id] + + ret = gitlab_cli(cmd) + assert ret.success + + assert "No default fields to show" in ret.stdout + assert "merged_yaml" not in ret.stdout |