66
77from nb_cli import _
88from nb_cli .config import GLOBAL_CONFIG
9+ from nb_cli .exceptions import NoSelectablePackageError
910from nb_cli .cli .utils import find_exact_package , format_package_results
1011from nb_cli .cli import (
1112 CLI_DEFAULT_STYLE ,
2122 call_pip_update ,
2223 call_pip_install ,
2324 call_pip_uninstall ,
25+ list_installed_adapters ,
2426)
2527
2628
@@ -80,6 +82,13 @@ async def store():
8082@adapter .command (
8183 name = "list" , help = _ ("List nonebot adapters published on nonebot homepage." )
8284)
85+ @click .option (
86+ "--installed" ,
87+ is_flag = True ,
88+ default = False ,
89+ flag_value = True ,
90+ help = _ ("Whether to list installed adapters only in current project." ),
91+ )
8392@click .option (
8493 "--include-unpublished" ,
8594 is_flag = True ,
@@ -88,8 +97,12 @@ async def store():
8897 help = _ ("Whether to include unpublished adapters." ),
8998)
9099@run_async
91- async def list_ (include_unpublished : bool = False ):
92- adapters = await list_adapters (include_unpublished = include_unpublished )
100+ async def list_ (installed : bool = False , include_unpublished : bool = False ):
101+ adapters = (
102+ await list_installed_adapters ()
103+ if installed
104+ else await list_adapters (include_unpublished = include_unpublished )
105+ )
93106 if include_unpublished :
94107 click .secho (_ ("WARNING: Unpublished adapters may be included." ), fg = "yellow" )
95108 click .echo (format_package_results (adapters ))
@@ -143,13 +156,23 @@ async def install(
143156 include_unpublished : bool = False ,
144157):
145158 try :
159+ _installed = {
160+ (a .project_link , a .module_name ) for a in await list_installed_adapters ()
161+ }
146162 adapter = await find_exact_package (
147163 _ ("Adapter name to install:" ),
148164 name ,
149- await list_adapters (include_unpublished = include_unpublished ),
165+ [
166+ a
167+ for a in await list_adapters (include_unpublished = include_unpublished )
168+ if (a .project_link , a .module_name ) not in _installed
169+ ],
150170 )
151171 except CancelledError :
152172 return
173+ except NoSelectablePackageError :
174+ click .echo (_ ("No available adapter found to install." ))
175+ return
153176
154177 if include_unpublished :
155178 click .secho (
@@ -216,10 +239,13 @@ async def update(
216239 adapter = await find_exact_package (
217240 _ ("Adapter name to update:" ),
218241 name ,
219- await list_adapters ( include_unpublished = include_unpublished ),
242+ await list_installed_adapters ( ),
220243 )
221244 except CancelledError :
222245 return
246+ except NoSelectablePackageError :
247+ click .echo (_ ("No installed adapter found to update." ))
248+ return
223249
224250 if include_unpublished :
225251 click .secho (
@@ -263,12 +289,13 @@ async def uninstall(name: str | None, pip_args: list[str] | None):
263289 adapter = await find_exact_package (
264290 _ ("Adapter name to uninstall:" ),
265291 name ,
266- await list_adapters (
267- include_unpublished = True # unpublished modules are always removable
268- ),
292+ await list_installed_adapters (),
269293 )
270294 except CancelledError :
271295 return
296+ except NoSelectablePackageError :
297+ click .echo (_ ("No installed adapter found to uninstall." ))
298+ return
272299
273300 try :
274301 can_uninstall = GLOBAL_CONFIG .remove_adapter (adapter )
0 commit comments