Implement pio pkg search command // Issue #3373

This commit is contained in:
Ivan Kravets
2022-03-30 17:32:05 +03:00
parent bd202f55ce
commit a6e12532f8
5 changed files with 139 additions and 1 deletions

View File

@ -19,6 +19,7 @@ PlatformIO Core 5
* `pio pkg install <https://docs.platformio.org/en/latest/core/userguide/pkg/cmd_install.html>`_ - install the project dependencies or custom packages
* `pio pkg list <https://docs.platformio.org/en/latest/core/userguide/pkg/cmd_list.html>`__ - list installed packages
* `pio pkg outdated <https://docs.platformio.org/en/latest/core/userguide/pkg/cmd_outdated.html>`__ - check for project outdated packages
* `pio pkg search <https://docs.platformio.org/en/latest/core/userguide/pkg/cmd_search.html>`__ - search for packages
* `pio pkg show <https://docs.platformio.org/en/latest/core/userguide/pkg/cmd_show.html>`__ - show package information
* `pio pkg uninstall <https://docs.platformio.org/en/latest/core/userguide/pkg/cmd_uninstall.html>`_ - uninstall the project dependencies or custom packages
* `pio pkg update <https://docs.platformio.org/en/latest/core/userguide/pkg/cmd_update.html>`__ - update the project dependencies or custom packages

2
docs

Submodule docs updated: d8a27471c7...e672ed7a4a

View File

@ -20,6 +20,7 @@ from platformio.package.commands.list import package_list_cmd
from platformio.package.commands.outdated import package_outdated_cmd
from platformio.package.commands.pack import package_pack_cmd
from platformio.package.commands.publish import package_publish_cmd
from platformio.package.commands.search import package_search_cmd
from platformio.package.commands.show import package_show_cmd
from platformio.package.commands.uninstall import package_uninstall_cmd
from platformio.package.commands.unpublish import package_unpublish_cmd
@ -35,6 +36,7 @@ from platformio.package.commands.update import package_update_cmd
package_outdated_cmd,
package_pack_cmd,
package_publish_cmd,
package_search_cmd,
package_show_cmd,
package_uninstall_cmd,
package_unpublish_cmd,

View File

@ -0,0 +1,77 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# 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 math
import click
from platformio import util
from platformio.clients.registry import RegistryClient
@click.command("search", short_help="Search for packages")
@click.argument("query")
@click.option("-p", "--page", type=click.IntRange(min=1))
@click.option(
"-s",
"--sort",
type=click.Choice(["relevance", "popularity", "trending", "added", "updated"]),
)
def package_search_cmd(query, page, sort):
client = RegistryClient()
result = client.list_packages(query, page=page, sort=sort)
if not result["total"]:
click.secho("Nothing has been found by your request", fg="yellow")
click.echo(
"Try a less-specific search or use truncation (or wildcard) operator *"
)
return
print_search_result(result)
def print_search_result(result):
click.echo(
"Found %d packages (page %d of %d)"
% (
result["total"],
result["page"],
math.ceil(result["total"] / result["limit"]),
)
)
for item in result["items"]:
click.echo()
print_search_item(item)
def print_search_item(item):
click.echo(
"%s/%s"
% (
click.style(item["owner"]["username"], fg="cyan"),
click.style(item["name"], fg="cyan", bold=True),
)
)
click.echo(
"%s%s • Published on %s"
% (
item["type"].capitalize()
if item["tier"] == "community"
else click.style(
("%s %s" % (item["tier"], item["type"])).title(), bold=True
),
item["version"]["name"],
util.parse_datetime(item["version"]["released_at"]).strftime("%c"),
)
)
click.echo(item["description"])

View File

@ -0,0 +1,58 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# 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.
from platformio.package.commands.search import package_search_cmd
def test_empty_query(clirunner, validate_cliresult):
result = clirunner.invoke(
package_search_cmd,
[""],
)
validate_cliresult(result)
assert all(t in result.output for t in ("Found", "Official", "page 1 of"))
def test_pagination(clirunner, validate_cliresult):
result = clirunner.invoke(
package_search_cmd,
["type:tool"],
)
validate_cliresult(result)
assert all(t in result.output for t in ("Verified Tool", "page 1 of"))
result = clirunner.invoke(
package_search_cmd,
["type:tool", "-p", "10"],
)
validate_cliresult(result)
assert all(t in result.output for t in ("Tool", "page 10 of"))
def test_sorting(clirunner, validate_cliresult):
result = clirunner.invoke(
package_search_cmd,
["OneWire", "-s", "popularity"],
)
validate_cliresult(result)
assert "paulstoffregen/OneWire" in result.output
def test_not_found(clirunner, validate_cliresult):
result = clirunner.invoke(
package_search_cmd,
["name:unknown-package"],
)
validate_cliresult(result)
assert "Nothing has been found" in result.output