Skip to content

Commit b596d7e

Browse files
Clean up pagination to be re-useable
1 parent dd75831 commit b596d7e

File tree

9 files changed

+137
-126
lines changed

9 files changed

+137
-126
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ jobs:
1818
run: |
1919
python -m pip install --upgrade pip
2020
pip install shiv
21-
pip install cloudsmith-cli
2221
- name: Get version
2322
id: get_version
2423
run: echo "VERSION=$(cat cloudsmith_cli/data/VERSION)" >> $GITHUB_ENV
@@ -44,10 +43,10 @@ jobs:
4443
asset_path: ./cloudsmith-${{ env.VERSION }}.pyz
4544
asset_name: cloudsmith-${{ env.VERSION }}.pyz
4645
asset_content_type: application/zip
47-
- name: Rename asses to cloudsmith.pyz
46+
- name: Rename asset to cloudsmith.pyz
4847
run: mv ./cloudsmith-${{ env.VERSION }}.pyz cloudsmith.pyz
4948
- name: Upload to Cloudsmith
50-
run: cloudsmith push raw ${{ vars.CLOUDSMITH_NAMESPACE }}/${{ vars.CLOUDSMITH_REPO }} cloudsmith.pyz --name "cloudsmith-cli" --version ${{ env.VERSION }}
49+
run: ./cloudsmith.pyz push raw ${{ vars.CLOUDSMITH_NAMESPACE }}/${{ vars.CLOUDSMITH_REPO }} cloudsmith.pyz --name "cloudsmith-cli" --version ${{ env.VERSION }}
5150
env:
5251
CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }}
5352
VERSION: ${{ env.VERSION }}

cloudsmith_cli/cli/commands/list_.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from ...core.api.packages import get_package_format_names_with_distros, list_packages
1010
from .. import command, decorators, utils, validators
1111
from ..exceptions import handle_api_exceptions
12-
from ..utils import maybe_spinner
12+
from ..utils import maybe_spinner, paginate_results
1313
from . import dependencies, entitlements
1414
from .main import main
1515
from .repos import get as get_repos
@@ -176,29 +176,15 @@ def packages(ctx, opts, owner_repo, page, page_size, query, show_all):
176176
context_msg = "Failed to get list of packages!"
177177
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
178178
with maybe_spinner(opts):
179-
if show_all:
180-
packages_ = []
181-
current_page = 1
182-
while True:
183-
page_packages, page_info = list_packages(
184-
owner=owner,
185-
repo=repo,
186-
page=current_page,
187-
page_size=page_size,
188-
query=query,
189-
)
190-
packages_.extend(page_packages)
191-
if (
192-
len(page_packages) < page_size
193-
or current_page >= page_info.page_total
194-
):
195-
break
196-
current_page += 1
197-
page_info.count = len(packages_)
198-
else:
199-
packages_, page_info = list_packages(
200-
owner=owner, repo=repo, page=page, page_size=page_size, query=query
201-
)
179+
packages_, page_info = paginate_results(
180+
list_packages,
181+
show_all,
182+
page,
183+
page_size,
184+
owner=owner,
185+
repo=repo,
186+
query=query,
187+
)
202188

203189
click.secho("OK", fg="green", err=use_stderr)
204190

cloudsmith_cli/cli/commands/policy/license.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
maybe_spinner,
1414
maybe_truncate_list,
1515
maybe_truncate_string,
16+
paginate_results,
1617
)
1718
from .command import policy
1819

1920

2021
def print_license_policies(policies):
21-
"""Print license policies as a table or output in another format."""
22-
2322
headers = [
2423
"Name",
2524
"Description",
@@ -57,10 +56,6 @@ def print_license_policies(policies):
5756
utils.pretty_print_table(headers, rows)
5857
click.echo()
5958

60-
num_results = len(rows)
61-
list_suffix = "license polic%s" % ("y" if num_results == 1 else "ies")
62-
utils.pretty_print_list_info(num_results=num_results, suffix=list_suffix)
63-
6459

6560
@policy.group(cls=command.AliasGroup, name="license", aliases=[])
6661
@decorators.common_cli_config_options
@@ -85,8 +80,14 @@ def licence(*args, **kwargs):
8580
@click.argument(
8681
"owner", metavar="OWNER", callback=validators.validate_owner, required=True
8782
)
83+
@click.option(
84+
"--show-all",
85+
"-a",
86+
is_flag=True,
87+
help="Show all results, not just the current page",
88+
)
8889
@click.pass_context
89-
def ls(ctx, opts, owner, page, page_size):
90+
def ls(ctx, opts, owner, page, page_size, show_all):
9091
"""
9192
List license policies.
9293
@@ -111,8 +112,8 @@ def ls(ctx, opts, owner, page, page_size):
111112
context_msg = "Failed to get license policies!"
112113
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
113114
with maybe_spinner(opts):
114-
policies, page_info = api.list_license_policies(
115-
owner=owner, page=page, page_size=page_size
115+
policies, page_info = paginate_results(
116+
api.list_license_policies, show_all, page, page_size, owner=owner
116117
)
117118

118119
click.secho("OK", fg="green", err=use_stderr)
@@ -122,6 +123,17 @@ def ls(ctx, opts, owner, page, page_size):
122123

123124
print_license_policies(policies)
124125

126+
click.echo()
127+
128+
num_results = len(policies)
129+
list_suffix = "license polic%s" % ("y" if num_results == 1 else "ies")
130+
utils.pretty_print_list_info(
131+
num_results=num_results,
132+
page_info=None if show_all else page_info,
133+
suffix=list_suffix,
134+
show_all=show_all,
135+
)
136+
125137

126138
@licence.command(aliases=["new"])
127139
@decorators.common_cli_config_options

cloudsmith_cli/cli/commands/policy/vulnerability.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
from ....core.api import orgs as api
77
from ... import command, decorators, utils, validators
88
from ...exceptions import handle_api_exceptions
9-
from ...utils import fmt_bool, fmt_datetime, maybe_spinner, maybe_truncate_string
9+
from ...utils import (
10+
fmt_bool,
11+
fmt_datetime,
12+
maybe_spinner,
13+
maybe_truncate_string,
14+
paginate_results,
15+
)
1016
from .command import policy
1117

1218

@@ -45,10 +51,6 @@ def print_vulnerability_policies(policies):
4551
utils.pretty_print_table(headers, rows, title="Vulnerability Policies")
4652
click.echo()
4753

48-
num_results = len(rows)
49-
list_suffix = "vulnerability polic%s" % ("y" if num_results == 1 else "ies")
50-
utils.pretty_print_list_info(num_results=num_results, suffix=list_suffix)
51-
5254

5355
@policy.group(cls=command.AliasGroup, name="vulnerability", aliases=[])
5456
@decorators.common_cli_config_options
@@ -74,7 +76,7 @@ def vulnerability(*args, **kwargs):
7476
"owner", metavar="OWNER", callback=validators.validate_owner, required=True
7577
)
7678
@click.pass_context
77-
def ls(ctx, opts, owner, page, page_size):
79+
def ls(ctx, opts, owner, page, page_size, show_all):
7880
"""
7981
List vulnerability policies.
8082
@@ -99,8 +101,8 @@ def ls(ctx, opts, owner, page, page_size):
99101
context_msg = "Failed to get package vulnerability policies!"
100102
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
101103
with maybe_spinner(opts):
102-
policies, page_info = api.list_vulnerability_policies(
103-
owner=owner, page=page, page_size=page_size
104+
policies, page_info = paginate_results(
105+
api.list_vulnerability_policies, show_all, page, page_size, owner=owner
104106
)
105107

106108
click.secho("OK", fg="green", err=use_stderr)
@@ -110,6 +112,17 @@ def ls(ctx, opts, owner, page, page_size):
110112

111113
print_vulnerability_policies(policies)
112114

115+
click.echo()
116+
117+
num_results = len(policies)
118+
list_suffix = "vulnerability polic%s" % ("y" if num_results == 1 else "ies")
119+
utils.pretty_print_list_info(
120+
num_results=num_results,
121+
page_info=None if show_all else page_info,
122+
suffix=list_suffix,
123+
show_all=show_all,
124+
)
125+
113126

114127
@vulnerability.command(aliases=["new"])
115128
@decorators.common_cli_config_options

cloudsmith_cli/cli/commands/repos.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ...core.api import repos as api
99
from .. import command, decorators, utils, validators
1010
from ..exceptions import handle_api_exceptions
11-
from ..utils import maybe_spinner
11+
from ..utils import maybe_spinner, paginate_results
1212
from .main import main
1313

1414

@@ -128,25 +128,9 @@ def get(ctx, opts, owner_repo, page, page_size, show_all):
128128
context_msg = "Failed to get list of repositories!"
129129
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
130130
with maybe_spinner(opts):
131-
if show_all:
132-
repos_ = []
133-
current_page = 1
134-
while True:
135-
page_repos, page_info = api.list_repos(
136-
owner=owner, repo=repo, page=current_page, page_size=page_size
137-
)
138-
repos_.extend(page_repos)
139-
if (
140-
len(page_repos) < page_size
141-
or current_page >= page_info.page_total
142-
):
143-
break
144-
current_page += 1
145-
page_info.count = len(repos_)
146-
else:
147-
repos_, page_info = api.list_repos(
148-
owner=owner, repo=repo, page=page, page_size=page_size
149-
)
131+
repos_, page_info = paginate_results(
132+
api.list_repos, show_all, page, page_size, owner=owner, repo=repo
133+
)
150134

151135
click.secho("OK", fg="green", err=use_stderr)
152136

@@ -222,7 +206,10 @@ def create(ctx, opts, owner, repo_config_file):
222206

223207
click.secho("OK", fg="green", err=use_stderr)
224208

225-
print_repositories(opts=opts, data=[repository], show_list_info=False)
209+
if utils.maybe_print_as_json(opts, [repository]):
210+
return
211+
212+
print_repositories(opts=opts, data=[repository], show_list_info=True)
226213

227214

228215
@repositories.command()
@@ -282,7 +269,10 @@ def update(ctx, opts, owner_repo, repo_config_file):
282269

283270
click.secho("OK", fg="green", err=use_stderr)
284271

285-
print_repositories(opts=opts, data=[repository], show_list_info=False)
272+
if utils.maybe_print_as_json(opts, [repository]):
273+
return
274+
275+
print_repositories(opts=opts, data=[repository], show_list_info=True)
286276

287277

288278
@repositories.command(aliases=["rm"])

cloudsmith_cli/cli/commands/upstream.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
maybe_spinner,
1414
maybe_truncate_list,
1515
maybe_truncate_string,
16+
paginate_results,
1617
)
1718
from .main import main
1819

@@ -174,33 +175,15 @@ def func(ctx, opts, owner_repo, page, page_size, show_all):
174175
context_msg = "Failed to get upstreams!"
175176
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
176177
with maybe_spinner(opts):
177-
if show_all:
178-
upstreams = []
179-
current_page = 1
180-
while True:
181-
page_upstreams, page_info = api.list_upstreams(
182-
owner=owner,
183-
repo=repo,
184-
upstream_format=upstream_fmt,
185-
page=current_page,
186-
page_size=page_size,
187-
)
188-
upstreams.extend(page_upstreams)
189-
if (
190-
len(page_upstreams) < page_size
191-
or current_page >= page_info.page_total
192-
):
193-
break
194-
current_page += 1
195-
page_info.count = len(upstreams)
196-
else:
197-
upstreams, page_info = api.list_upstreams(
198-
owner=owner,
199-
repo=repo,
200-
upstream_format=upstream_fmt,
201-
page=page,
202-
page_size=page_size,
203-
)
178+
upstreams, page_info = paginate_results(
179+
api.list_upstreams,
180+
show_all,
181+
page,
182+
page_size,
183+
owner=owner,
184+
repo=repo,
185+
upstream_format=upstream_fmt,
186+
)
204187

205188
click.secho("OK", fg="green", err=use_stderr)
206189

cloudsmith_cli/cli/decorators.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import click
66

77
from ..core.api.init import initialise_api as _initialise_api
8-
from . import config, utils, validators
8+
from . import config, utils
99

1010

1111
def report_retry(seconds, context=None):
@@ -142,39 +142,25 @@ def wrapper(ctx, *args, **kwargs):
142142

143143

144144
def common_cli_list_options(f):
145-
"""Add common list options to commands."""
146-
147-
@click.option(
148-
"-p",
145+
"""Common CLI options for list commands."""
146+
f = click.option(
149147
"--page",
150148
default=1,
151-
type=int,
152-
help="The page to view for lists, where 1 is the first page",
153-
callback=validators.validate_page,
154-
)
155-
@click.option(
156-
"-l",
149+
help="The page of results to show.",
150+
type=click.INT,
151+
)(f)
152+
f = click.option(
157153
"--page-size",
158-
default=30,
159-
type=int,
160-
help="The amount of items to view per page for lists.",
161-
callback=validators.validate_page_size,
162-
)
163-
@click.option(
154+
default=None,
155+
help="The number of results to return per page.",
156+
type=click.INT,
157+
)(f)
158+
f = click.option(
164159
"--show-all",
165160
is_flag=True,
166-
default=False,
167-
help="Show all results by automatically paginating",
168-
)
169-
@click.pass_context
170-
@functools.wraps(f)
171-
def wrapper(ctx, *args, **kwargs):
172-
# pylint: disable=missing-docstring
173-
opts = config.get_or_create_options(ctx)
174-
kwargs["opts"] = opts
175-
return ctx.invoke(f, *args, **kwargs)
176-
177-
return wrapper
161+
help="Show all results (unpaginated)",
162+
)(f)
163+
return f
178164

179165

180166
def common_api_auth_options(f):

cloudsmith_cli/cli/tests/commands/test_repos.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ def test_repos_commands(runner, organization, tmp_path):
134134
update, [owner_slash_repo, str(repo_config_file_path)], catch_exceptions=False
135135
)
136136
assert result.exit_code == 0
137+
assert (
138+
"Updating "
139+
+ repository_slug
140+
+ " repository in the "
141+
+ organization
142+
+ " namespace ...OK"
143+
in result.output
144+
)
137145
assert "Results: 1 repository visible" in result.output
138146
assert_output_is_equal_to_repo_config(
139147
result.output, organization, repo_config_file_path

0 commit comments

Comments
 (0)