-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathupstream.py
More file actions
456 lines (358 loc) · 14.9 KB
/
upstream.py
File metadata and controls
456 lines (358 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"""CLI/Commands - create, retrieve, update or delete repository upstreams."""
import json
import click
from ...core.api import upstreams as api
from .. import command, decorators, utils, validators
from ..exceptions import handle_api_exceptions
from ..utils import (
fmt_bool,
fmt_datetime,
maybe_spinner,
maybe_truncate_list,
maybe_truncate_string,
)
from .main import main
UPSTREAM_FORMATS = [
"dart",
"deb",
"docker",
"helm",
"maven",
"nuget",
"npm",
"python",
"rpm",
"ruby",
"cran",
]
def print_upstreams(upstreams, upstream_fmt):
"""Print upstreams as a table or output in another format."""
def build_row(u):
row = [
click.style(u["name"], fg="cyan"),
click.style(maybe_truncate_string(u["upstream_url"]), fg="cyan"),
click.style(str(u["auth_mode"]), fg="yellow"),
click.style(
maybe_truncate_string(str(u["auth_secret"] or "")),
fg="yellow",
),
click.style(str(u["auth_username"] or ""), fg="yellow"),
click.style(fmt_datetime(u["created_at"]), fg="blue"),
click.style(str(u["extra_header_1"] or ""), fg="yellow"),
click.style(str(u["extra_header_2"] or ""), fg="yellow"),
click.style(str(u["extra_value_1"] or ""), fg="yellow"),
click.style(str(u["extra_value_2"] or ""), fg="yellow"),
click.style(fmt_bool(u["is_active"]), fg="green"),
click.style(u["mode"], fg="green"),
click.style(str(u["priority"]), fg="green"),
click.style(u["slug_perm"], fg="green"),
click.style(fmt_datetime(u["updated_at"]), fg="blue"),
click.style(fmt_bool(u["verify_ssl"]), fg="green"),
]
if upstream_fmt == "deb":
# `Component`, `Distribution Versions` and `Upstream Distribution` are deb-only
row.append(click.style(str(u.get("component", None)), fg="yellow"))
row.append(
click.style(
str(maybe_truncate_list(u.get("distro_versions", []))),
fg="yellow",
)
)
row.append(
click.style(str(u.get("upstream_distribution", None)), fg="yellow")
)
if upstream_fmt == "rpm":
# `Distribution Version` is rpm-only
row.append(click.style(str(u.get("distro_version", "")), fg="yellow"))
return row
headers = [
"Name",
"Upstream Url",
"Auth mode",
"Auth Secret",
"Auth Username",
"Created At",
"Extra Header 1",
"Extra Header 2",
"Extra Value 1",
"Extra Value 2",
"Active",
"Mode",
"Priority",
"Slug Perm",
"Updated At",
"Verify SSL",
]
if upstream_fmt == "deb":
headers.append("Component")
headers.append("Distribution Versions")
headers.append("Upstream Distribution")
if upstream_fmt == "rpm":
headers.append("Distribution Version")
rows = [build_row(x) for x in upstreams]
click.echo()
utils.pretty_print_table(headers, rows)
click.echo()
num_results = len(rows)
list_suffix = "upstream%s" % ("" if num_results == 1 else "s")
utils.pretty_print_list_info(num_results=num_results, suffix=list_suffix)
@main.group(cls=command.AliasGroup, name="upstream", aliases=[])
@decorators.common_cli_config_options
@decorators.common_cli_output_options
@decorators.common_api_auth_options
@decorators.initialise_api
@click.pass_context
def upstream(*args, **kwargs):
"""
Manage upstreams for a repository.
See the help for subcommands for more information on each.
"""
def build_upstream_group_func(upstream_fmt):
@decorators.common_cli_config_options
@decorators.common_cli_output_options
@decorators.common_api_auth_options
@decorators.initialise_api
@click.pass_context
def func(ctx, opts):
pass
func.__doc__ = (
"""
Manage %s upstreams for a repository.
See the help for subcommands for more information on each.
"""
% upstream_fmt
)
return func
def build_upstream_list_command(upstream_fmt):
@decorators.common_cli_config_options
@decorators.common_cli_list_options
@decorators.common_cli_output_options
@decorators.common_api_auth_options
@decorators.initialise_api
@click.argument(
"owner_repo", metavar="OWNER/REPO", callback=validators.validate_owner_repo
)
@click.pass_context
def func(ctx, opts, owner_repo, page, page_size):
owner, repo = owner_repo
# Use stderr for messages if the output is something else (e.g. # JSON)
use_stderr = opts.output != "pretty"
click.echo("Getting upstreams... ", nl=False, err=use_stderr)
context_msg = "Failed to get upstreams!"
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
with maybe_spinner(opts):
upstreams, page_info = api.list_upstreams(
owner=owner,
repo=repo,
upstream_format=upstream_fmt,
page=page,
page_size=page_size,
)
click.secho("OK", fg="green", err=use_stderr)
if utils.maybe_print_as_json(opts, upstreams, page_info):
return
print_upstreams(upstreams, upstream_fmt)
func.__doc__ = f"""
List {upstream_fmt} upstreams for a repository.
This requires appropriate permissions for the owner (a member of the organization, repository privileges and a valid API key).
- OWNER/REPO: Specify the OWNER namespace (organization) and REPO (repository) to target a specific Cloudsmith repository.
Example: 'your-org/your-repo'
Full CLI example:
$ cloudsmith upstream {upstream_fmt} ls your-org/your-repo
"""
return func
def build_upstream_create_command(upstream_fmt):
@decorators.common_cli_config_options
@decorators.common_cli_output_options
@decorators.common_api_auth_options
@decorators.initialise_api
@click.argument(
"owner_repo", metavar="OWNER/REPO", callback=validators.validate_owner_repo
)
@click.argument("upstream_config_file", type=click.File("rb"), required=True)
@click.pass_context
def func(ctx, opts, owner_repo, upstream_config_file):
# Use stderr for messages if the output is something else (e.g. JSON)
use_stderr = opts.output != "pretty"
owner, repo = owner_repo
upstream_config = json.load(upstream_config_file)
upstream_name = upstream_config.get("name", None)
if upstream_name is None:
raise click.BadParameter(
"Name is a required field for creating an upstream.", param="name"
)
click.secho(
'Creating "%(name)s" upstream for the %(owner)s/%(repo)s repository...'
% {
"name": click.style(upstream_name, bold=True),
"owner": click.style(owner, bold=True),
"repo": click.style(repo, bold=True),
},
nl=False,
err=use_stderr,
)
context_msg = "Failed to create the upstream!"
with handle_api_exceptions(ctx, opts, context_msg=context_msg):
with maybe_spinner(opts):
upstream_resp_data = api.create_upstream(
owner, repo, upstream_fmt, upstream_config
)
click.secho("OK", fg="green", err=use_stderr)
if utils.maybe_print_as_json(opts, upstream_resp_data):
return
print_upstreams([upstream_resp_data], upstream_fmt)
func.__doc__ = f"""
Create a {upstream_fmt} upstream for a repository.
This requires appropriate permissions for the owner (a member of the organization, repository privileges and a valid API key).
- OWNER/REPO: Specify the OWNER namespace (organization) and REPO (repository) to target a specific Cloudsmith repository.
Example: 'your-org/your-repo'
- UPSTREAM_CONFIG_FILE: Config json file specifying the settings for the upstream to be updated.
For a full list of supported config properties, please refer to the "body params" section of the api reference for the relevant endpoint at:
https://help.cloudsmith.io/reference/repos_upstream_{upstream_fmt}_create
\b
Example:
{{
"name": "Some Upstream",
"upstream_url": "https://someupstream.net",
"mode": "Proxy Only",
"auth_mode": "None",
"priority": 5,
...
}}
Full CLI example:
$ cloudsmith upstream {upstream_fmt} create your-org/your-repo ./path/to/upstream-config.json
"""
return func
def build_upstream_update_command(upstream_fmt):
@decorators.common_cli_config_options
@decorators.common_cli_output_options
@decorators.common_api_auth_options
@decorators.initialise_api
@click.argument(
"owner_repo_slug_perm",
metavar="OWNER/REPO/SLUG_PERM",
callback=validators.validate_owner_repo_slug_perm,
)
@click.argument("upstream_config_file", type=click.File("rb"), required=True)
@click.pass_context
def func(ctx, opts, owner_repo_slug_perm, upstream_config_file):
# Use stderr for message if the output is something else (e.g. JSON)
use_stderr = opts.output != "pretty"
owner, repo, slug_perm = owner_repo_slug_perm
upstream_config = json.load(upstream_config_file)
click.secho(
"Updating the %(slug_perm)s upstream from the %(owner)s/%(repo)s repository ... "
% {
"owner": click.style(owner, bold=True),
"repo": click.style(repo, bold=True),
"slug_perm": click.style(slug_perm, bold=True),
},
nl=False,
err=use_stderr,
)
context_msg = "Failed to update the upstream!"
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
with maybe_spinner(opts):
upstream_resp_data = api.update_upstream(
owner, repo, slug_perm, upstream_fmt, upstream_config
)
click.secho("OK", fg="green", err=use_stderr)
if utils.maybe_print_as_json(opts, upstream_resp_data):
return
print_upstreams([upstream_resp_data], upstream_fmt)
func.__doc__ = f"""
Update a {upstream_fmt} upstream for a repository.
This requires appropriate permissions for the owner (a member of the organization, repository privileges and a valid API key).
- OWNER/REPO/SLUG_PERM: Specify the OWNER namespace (organization), REPO (repository) and SLUG_PERM (upstream) to target a specific upstream belonging to a repo.
Example: 'your-org/your-repo/abcdefg'
- UPSTREAM_CONFIG_FILE: Config json file specifying the settings for the upstream to be updated.
For a full list of supported config properties, please refer to the "body params" section of the api reference for the relevant endpoint at:
https://help.cloudsmith.io/reference/repos_upstream_{upstream_fmt}_partial_update
\b
Example:
{{
"name": "Some Upstream",
"upstream_url": "https://someupstream.net",
"mode": "Proxy Only",
"auth_mode": "None",
"priority": 5,
...
}}
Full CLI example:
$ cloudsmith upstream {upstream_fmt} update your-org/your-repo/abcdefg ./path/to/upstream-config.json
"""
return func
def build_upstream_delete_command(upstream_fmt):
@decorators.common_cli_config_options
@decorators.common_cli_output_options
@decorators.common_api_auth_options
@decorators.initialise_api
@click.argument(
"owner_repo_slug_perm",
metavar="OWNER/REPO/SLUG_PERM",
callback=validators.validate_owner_repo_slug_perm,
)
@click.option(
"-y",
"--yes",
default=False,
is_flag=True,
help="Assume yes as default answer to questions (this is dangerous!)",
)
@click.pass_context
def func(ctx, opts, owner_repo_slug_perm, yes):
# Use stderr for message if the output is something else (e.g. JSON)
use_stderr = opts.output != "pretty"
owner, repo, slug_perm = owner_repo_slug_perm
delete_args = {
"owner": click.style(owner, bold=True),
"repo": click.style(repo, bold=True),
"slug_perm": click.style(slug_perm, bold=True),
}
prompt = (
"delete the %(slug_perm)s upstream from the %(owner)s/%(repo)s repository"
% delete_args
)
if not utils.confirm_operation(prompt, assume_yes=yes):
return
click.secho(
"Deleting the %(slug_perm)s upstream from the %(owner)s/%(repo)s repository ... "
% delete_args,
nl=False,
err=use_stderr,
)
context_msg = "Failed to delete the upstream!"
with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
with maybe_spinner(opts):
api.delete_upstream(owner, repo, upstream_fmt, slug_perm)
click.secho("OK", fg="green", err=use_stderr)
func.__doc__ = f"""
Delete a {upstream_fmt} upstream for a repository.
This requires appropriate permissions for the owner (a member of the organization, repository privileges and a valid API key).
- OWNER/REPO/SLUG_PERM: Specify the OWNER namespace (organization), REPO (repository) and SLUG_PERM (upstream) to target a specific upstream belonging to a repo.
Example: 'your-org/your-repo/abcdefg'
Full CLI example:
$ cloudsmith upstream {upstream_fmt} delete your-org/your-repo/abcdefg
"""
return func
for upstream_format in UPSTREAM_FORMATS:
# Build a click group for the upstream name e.g. dart, npm, ruby.
# Add it to the "upstream" parent group.
# This gives us e.g. `cloudsmith upstream dart` in the cli.
upstream_group = upstream.group(
cls=command.AliasGroup, name=upstream_format, aliases=[]
)(build_upstream_group_func(upstream_format))
# Add create/list/update/delete commands to the child group we created above.
# This gives us e.g. `cloudsmith upstream dart ls`.
upstream_group.command(name="list", aliases=["ls"])(
build_upstream_list_command(upstream_format)
)
upstream_group.command(name="create", aliases=["new"])(
build_upstream_create_command(upstream_format)
)
upstream_group.command(name="delete", aliases=["rm"])(
build_upstream_delete_command(upstream_format)
)
upstream_group.command(name="update")(
build_upstream_update_command(upstream_format)
)