diff --git a/platformio/commands/access.py b/platformio/commands/access.py deleted file mode 100644 index fa726927..00000000 --- a/platformio/commands/access.py +++ /dev/null @@ -1,154 +0,0 @@ -# Copyright (c) 2014-present PlatformIO -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# pylint: disable=unused-argument - -import json -import re - -import click -from tabulate import tabulate - -from platformio.account.helpers import validate_username -from platformio.commands.team import validate_orgname_teamname -from platformio.registry.client import RegistryClient - - -def validate_client(value): - if ":" in value: - validate_orgname_teamname(value) - else: - validate_username(value) - return value - - -@click.group("access", short_help="Manage resource access") -def cli(): - pass - - -def validate_urn(value): - value = str(value).strip() - if not re.match(r"^prn:reg:pkg:(\d+):(\w+)$", value, flags=re.I): - raise click.BadParameter("Invalid URN format.") - return value - - -@cli.command("public", short_help="Make resource public") -@click.argument( - "urn", - callback=lambda _, __, value: validate_urn(value), -) -@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") -def access_public(urn, urn_type): - client = RegistryClient() - client.update_resource(urn=urn, private=0) - return click.secho( - "The resource %s has been successfully updated." % urn, - fg="green", - ) - - -@cli.command("private", short_help="Make resource private") -@click.argument( - "urn", - callback=lambda _, __, value: validate_urn(value), -) -@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") -def access_private(urn, urn_type): - client = RegistryClient() - client.update_resource(urn=urn, private=1) - return click.secho( - "The resource %s has been successfully updated." % urn, - fg="green", - ) - - -@cli.command("grant", short_help="Grant access") -@click.argument("level", type=click.Choice(["admin", "maintainer", "guest"])) -@click.argument( - "client", - metavar="[|]", - callback=lambda _, __, value: validate_client(value), -) -@click.argument( - "urn", - callback=lambda _, __, value: validate_urn(value), -) -@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") -def access_grant(level, client, urn, urn_type): - reg_client = RegistryClient() - reg_client.grant_access_for_resource(urn=urn, client=client, level=level) - return click.secho( - "Access for resource %s has been granted for %s" % (urn, client), - fg="green", - ) - - -@cli.command("revoke", short_help="Revoke access") -@click.argument( - "client", - metavar="[ORGNAME:TEAMNAME|USERNAME]", - callback=lambda _, __, value: validate_client(value), -) -@click.argument( - "urn", - callback=lambda _, __, value: validate_urn(value), -) -@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") -def access_revoke(client, urn, urn_type): - reg_client = RegistryClient() - reg_client.revoke_access_from_resource(urn=urn, client=client) - return click.secho( - "Access for resource %s has been revoked for %s" % (urn, client), - fg="green", - ) - - -@cli.command("list", short_help="List published resources") -@click.argument("owner", required=False) -@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") -@click.option("--json-output", is_flag=True) -def access_list(owner, urn_type, json_output): - reg_client = RegistryClient() - resources = reg_client.list_resources(owner=owner) - if json_output: - return click.echo(json.dumps(resources)) - if not resources: - return click.secho("You do not have any resources.", fg="yellow") - for resource in resources: - click.echo() - click.secho(resource.get("name"), fg="cyan") - click.echo("-" * len(resource.get("name"))) - table_data = [] - table_data.append(("URN:", resource.get("urn"))) - table_data.append(("Owner:", resource.get("owner"))) - table_data.append( - ( - "Access:", - click.style("Private", fg="red") - if resource.get("private", False) - else "Public", - ) - ) - table_data.append( - ( - "Access level(s):", - ", ".join( - (level.capitalize() for level in resource.get("access_levels")) - ), - ) - ) - click.echo(tabulate(table_data, tablefmt="plain")) - return click.echo() diff --git a/platformio/registry/access/__init__.py b/platformio/registry/access/__init__.py new file mode 100644 index 00000000..b0514903 --- /dev/null +++ b/platformio/registry/access/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/platformio/registry/access/cli.py b/platformio/registry/access/cli.py new file mode 100644 index 00000000..fe585c94 --- /dev/null +++ b/platformio/registry/access/cli.py @@ -0,0 +1,36 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import click + +from platformio.registry.access.commands.grant import access_grant_cmd +from platformio.registry.access.commands.list import access_list_cmd +from platformio.registry.access.commands.private import access_private_cmd +from platformio.registry.access.commands.public import access_public_cmd +from platformio.registry.access.commands.revoke import access_revoke_cmd + + +@click.group( + "access", + commands=[ + access_grant_cmd, + access_list_cmd, + access_private_cmd, + access_public_cmd, + access_revoke_cmd, + ], + short_help="Manage resource access", +) +def cli(): + pass diff --git a/platformio/registry/access/commands/__init__.py b/platformio/registry/access/commands/__init__.py new file mode 100644 index 00000000..b0514903 --- /dev/null +++ b/platformio/registry/access/commands/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/platformio/registry/access/commands/grant.py b/platformio/registry/access/commands/grant.py new file mode 100644 index 00000000..52e218ef --- /dev/null +++ b/platformio/registry/access/commands/grant.py @@ -0,0 +1,39 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import click + +from platformio.registry.access.helpers import validate_client, validate_urn +from platformio.registry.client import RegistryClient + + +@click.command("grant", short_help="Grant access") +@click.argument("level", type=click.Choice(["admin", "maintainer", "guest"])) +@click.argument( + "client", + metavar="[|]", + callback=lambda _, __, value: validate_client(value), +) +@click.argument( + "urn", + callback=lambda _, __, value: validate_urn(value), +) +@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") +def access_grant_cmd(level, client, urn, urn_type): # pylint: disable=unused-argument + reg_client = RegistryClient() + reg_client.grant_access_for_resource(urn=urn, client=client, level=level) + return click.secho( + "Access for resource %s has been granted for %s" % (urn, client), + fg="green", + ) diff --git a/platformio/registry/access/commands/list.py b/platformio/registry/access/commands/list.py new file mode 100644 index 00000000..ce9d8a6a --- /dev/null +++ b/platformio/registry/access/commands/list.py @@ -0,0 +1,58 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json + +import click +from tabulate import tabulate + +from platformio.registry.client import RegistryClient + + +@click.command("list", short_help="List published resources") +@click.argument("owner", required=False) +@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") +@click.option("--json-output", is_flag=True) +def access_list_cmd(owner, urn_type, json_output): # pylint: disable=unused-argument + reg_client = RegistryClient() + resources = reg_client.list_resources(owner=owner) + if json_output: + return click.echo(json.dumps(resources)) + if not resources: + return click.secho("You do not have any resources.", fg="yellow") + for resource in resources: + click.echo() + click.secho(resource.get("name"), fg="cyan") + click.echo("-" * len(resource.get("name"))) + table_data = [] + table_data.append(("URN:", resource.get("urn"))) + table_data.append(("Owner:", resource.get("owner"))) + table_data.append( + ( + "Access:", + click.style("Private", fg="red") + if resource.get("private", False) + else "Public", + ) + ) + table_data.append( + ( + "Access level(s):", + ", ".join( + (level.capitalize() for level in resource.get("access_levels")) + ), + ) + ) + click.echo(tabulate(table_data, tablefmt="plain")) + return click.echo() diff --git a/platformio/registry/access/commands/private.py b/platformio/registry/access/commands/private.py new file mode 100644 index 00000000..276fb460 --- /dev/null +++ b/platformio/registry/access/commands/private.py @@ -0,0 +1,33 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import click + +from platformio.registry.access.helpers import validate_urn +from platformio.registry.client import RegistryClient + + +@click.command("private", short_help="Make resource private") +@click.argument( + "urn", + callback=lambda _, __, value: validate_urn(value), +) +@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") +def access_private_cmd(urn, urn_type): # pylint: disable=unused-argument + client = RegistryClient() + client.update_resource(urn=urn, private=1) + return click.secho( + "The resource %s has been successfully updated." % urn, + fg="green", + ) diff --git a/platformio/registry/access/commands/public.py b/platformio/registry/access/commands/public.py new file mode 100644 index 00000000..58a3c26b --- /dev/null +++ b/platformio/registry/access/commands/public.py @@ -0,0 +1,33 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import click + +from platformio.registry.access.helpers import validate_urn +from platformio.registry.client import RegistryClient + + +@click.command("public", short_help="Make resource public") +@click.argument( + "urn", + callback=lambda _, __, value: validate_urn(value), +) +@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") +def access_public_cmd(urn, urn_type): # pylint: disable=unused-argument + client = RegistryClient() + client.update_resource(urn=urn, private=0) + return click.secho( + "The resource %s has been successfully updated." % urn, + fg="green", + ) diff --git a/platformio/registry/access/commands/revoke.py b/platformio/registry/access/commands/revoke.py new file mode 100644 index 00000000..7768d229 --- /dev/null +++ b/platformio/registry/access/commands/revoke.py @@ -0,0 +1,38 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import click + +from platformio.registry.access.helpers import validate_client, validate_urn +from platformio.registry.client import RegistryClient + + +@click.command("revoke", short_help="Revoke access") +@click.argument( + "client", + metavar="[ORGNAME:TEAMNAME|USERNAME]", + callback=lambda _, __, value: validate_client(value), +) +@click.argument( + "urn", + callback=lambda _, __, value: validate_urn(value), +) +@click.option("--urn-type", type=click.Choice(["prn:reg:pkg"]), default="prn:reg:pkg") +def access_revoke_cmd(client, urn, urn_type): # pylint: disable=unused-argument + reg_client = RegistryClient() + reg_client.revoke_access_from_resource(urn=urn, client=client) + return click.secho( + "Access for resource %s has been revoked for %s" % (urn, client), + fg="green", + ) diff --git a/platformio/registry/access/helpers.py b/platformio/registry/access/helpers.py new file mode 100644 index 00000000..9a49b81e --- /dev/null +++ b/platformio/registry/access/helpers.py @@ -0,0 +1,35 @@ +# Copyright (c) 2014-present PlatformIO +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import re + +import click + +from platformio.account.helpers import validate_username +from platformio.commands.team import validate_orgname_teamname + + +def validate_urn(value): + value = str(value).strip() + if not re.match(r"^prn:reg:pkg:(\d+):(\w+)$", value, flags=re.I): + raise click.BadParameter("Invalid URN format.") + return value + + +def validate_client(value): + if ":" in value: + validate_orgname_teamname(value) + else: + validate_username(value) + return value