Skip to content

Commit 23547eb

Browse files
authored
Fix: 增加用户提示以正确指引用户进行更新操作 (#197)
1 parent 3fed627 commit 23547eb

6 files changed

Lines changed: 208 additions & 255 deletions

File tree

.basedpyright/baseline.json

Lines changed: 1 addition & 113 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nb_cli/cli/commands/adapter.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ async def install(
159159
name, extras = name.split("[", 1)
160160
extras = extras.rstrip("]")
161161

162+
_all_adapters = await list_adapters(include_unpublished=include_unpublished)
163+
162164
try:
163165
_installed_adapters = await list_installed_adapters()
164166
is_installed = False
@@ -180,11 +182,10 @@ async def install(
180182
name,
181183
[
182184
a
183-
for a in await list_adapters(
184-
include_unpublished=include_unpublished
185-
)
185+
for a in _all_adapters
186186
if (a.project_link, a.module_name) not in _installed
187187
],
188+
echo=False,
188189
)
189190

190191
assert adapter is not None # confirmed by above logic
@@ -193,6 +194,23 @@ async def install(
193194
except NoSelectablePackageError:
194195
click.echo(_("No available adapter found to install."))
195196
return
197+
except RuntimeError:
198+
_adapter = await find_exact_package(
199+
_("Adapter name to install:"), name, _all_adapters
200+
)
201+
click.secho(
202+
_("ERROR: Adapter {name} is already installed.").format(
203+
name=_adapter.project_link
204+
),
205+
fg="red",
206+
)
207+
click.secho(
208+
_("To upgrade the adapter, run `nb adapter update {name}` instead.").format(
209+
name=_adapter.project_link
210+
),
211+
fg="red",
212+
)
213+
return
196214

197215
if include_unpublished:
198216
click.secho(

nb_cli/cli/commands/plugin.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ async def install(
159159
name, extras = name.split("[", 1)
160160
extras = extras.rstrip("]")
161161

162+
_all_plugins = await list_plugins(include_unpublished=include_unpublished)
163+
162164
try:
163165
_installed_plugins = await list_installed_plugins()
164166
is_installed = False
@@ -180,9 +182,10 @@ async def install(
180182
name,
181183
[
182184
p
183-
for p in await list_plugins(include_unpublished=include_unpublished)
185+
for p in _all_plugins
184186
if (p.project_link, p.module_name) not in _installed
185187
],
188+
echo=False,
186189
)
187190

188191
assert plugin is not None # confirmed by above logic
@@ -191,6 +194,23 @@ async def install(
191194
except NoSelectablePackageError:
192195
click.echo(_("No available plugin found to install."))
193196
return
197+
except RuntimeError:
198+
_plugin = await find_exact_package(
199+
_("Plugin name to install:"), name, _all_plugins
200+
)
201+
click.secho(
202+
_("ERROR: Plugin {name} is already installed.").format(
203+
name=_plugin.project_link
204+
),
205+
fg="red",
206+
)
207+
click.secho(
208+
_("To upgrade the plugin, run `nb plugin update {name}` instead.").format(
209+
name=_plugin.project_link
210+
),
211+
fg="red",
212+
)
213+
return
194214

195215
if include_unpublished:
196216
click.secho(

nb_cli/cli/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ def __call__(self, x: Adapter | Plugin | Driver, *, value: str) -> bool: ...
6363

6464

6565
async def find_exact_package(
66-
question: str, name: str | None, packages: list[T], *, no_extras: bool = False
66+
question: str,
67+
name: str | None,
68+
packages: list[T],
69+
*,
70+
no_extras: bool = False,
71+
echo: bool = True,
6772
) -> T:
6873
if name is None:
6974
if not packages:
@@ -99,9 +104,9 @@ async def find_exact_package(
99104
]
100105
if len(packages) == 1:
101106
return packages[0]
102-
elif len(packages) > 1:
107+
elif echo and len(packages) > 1:
103108
click.echo(format_package_results(packages))
104-
else:
109+
elif echo:
105110
click.echo(_("Package {name} not found.").format(name=name))
106111
click.echo(
107112
_("*** You may check with `--include-unpublished` option if supported.")

0 commit comments

Comments
 (0)