1818 MISSION_TAG ,
1919 PLUGIN_YAML ,
2020 PLUGINS_LABEL ,
21+ USER_DIR_TAG ,
2122 WARNET_USER_DIR_ENV_VAR ,
2223)
2324
@@ -37,9 +38,10 @@ def plugins():
3738
3839
3940@plugins .command ()
40- def ls ():
41+ @click .pass_context
42+ def ls (ctx ):
4143 """List all available plugins and whether they are activated"""
42- plugin_dir = _get_plugins_directory ( )
44+ plugin_dir = get_plugins_directory_or ( ctx . obj . get ( USER_DIR_TAG ) )
4345 if plugin_dir is None :
4446 direct_user_to_plugin_directory_and_exit ()
4547
@@ -52,9 +54,10 @@ def ls():
5254
5355@plugins .command ()
5456@click .argument ("plugin" , type = str , default = "" )
55- def toggle (plugin : str ):
57+ @click .pass_context
58+ def toggle (ctx , plugin : str ):
5659 """Toggle a plugin on or off"""
57- plugin_dir = _get_plugins_directory ( )
60+ plugin_dir = get_plugins_directory_or ( ctx . obj . get ( USER_DIR_TAG ) )
5861 if plugin_dir is None :
5962 direct_user_to_plugin_directory_and_exit ()
6063
@@ -85,10 +88,10 @@ def toggle(plugin: str):
8588 write_yaml (updated_settings , plugin_dir / Path (plugin ) / Path (PLUGIN_YAML ))
8689
8790
88- def load_user_modules () -> bool :
91+ def load_user_modules (path : Optional [ Path ] = None ) -> bool :
8992 was_successful_load = False
9093
91- plugin_dir = _get_plugins_directory ( )
94+ plugin_dir = get_plugins_directory_or ( path )
9295
9396 if not plugin_dir or not plugin_dir .is_dir ():
9497 return was_successful_load
@@ -125,15 +128,23 @@ def register_command(command):
125128 register .add_command (command )
126129
127130
128- def load_plugins (fn ):
129- load_user_modules ()
131+ def load_plugins ():
130132 for module in imported_modules .values ():
131133 for name , func in inspect .getmembers (module , inspect .isfunction ):
132134 if name == "warnet_register_plugin" :
133135 func (register_command )
134136
135137
136- def _get_plugins_directory () -> Optional [Path ]:
138+ def get_plugins_directory_or (path : Optional [Path ] = None ) -> Optional [Path ]:
139+ """Get the plugins directory
140+ user-provided path > environment variable > relative path
141+ """
142+ if path :
143+ if path .is_dir ():
144+ return path / PLUGINS_LABEL
145+ else :
146+ click .secho (f"Not a directory: { path } " , fg = "red" )
147+
137148 user_dir = os .getenv (WARNET_USER_DIR_ENV_VAR )
138149
139150 plugin_dir = Path (user_dir ) / PLUGINS_LABEL if user_dir else Path .cwd () / PLUGINS_LABEL
@@ -147,7 +158,7 @@ def _get_plugins_directory() -> Optional[Path]:
147158def direct_user_to_plugin_directory_and_exit ():
148159 click .secho ("Could not determine the plugin directory location." )
149160 click .secho (
150- "Solution 1: try runing this command again, but this time from your initialized warnet directory."
161+ "Solution 1: try runing this command again, but this time from your initialized Warnet directory."
151162 )
152163 click .secho (
153164 "Solution 2: consider setting environment variable pointing to your Warnet project directory:"
@@ -190,7 +201,7 @@ def check_if_plugin_enabled(path: Path) -> bool:
190201
191202def get_plugins_with_status (plugin_dir : Optional [Path ] = None ) -> list [tuple [Path , bool ]]:
192203 if not plugin_dir :
193- plugin_dir = _get_plugins_directory ()
204+ plugin_dir = get_plugins_directory_or ()
194205 candidates = [
195206 Path (os .path .join (plugin_dir , name ))
196207 for name in os .listdir (plugin_dir )
0 commit comments