18
18
MISSION_TAG ,
19
19
PLUGIN_YAML ,
20
20
PLUGINS_LABEL ,
21
+ USER_DIR_TAG ,
21
22
WARNET_USER_DIR_ENV_VAR ,
22
23
)
23
24
@@ -37,9 +38,10 @@ def plugins():
37
38
38
39
39
40
@plugins .command ()
40
- def ls ():
41
+ @click .pass_context
42
+ def ls (ctx ):
41
43
"""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 ) )
43
45
if plugin_dir is None :
44
46
direct_user_to_plugin_directory_and_exit ()
45
47
@@ -52,9 +54,10 @@ def ls():
52
54
53
55
@plugins .command ()
54
56
@click .argument ("plugin" , type = str , default = "" )
55
- def toggle (plugin : str ):
57
+ @click .pass_context
58
+ def toggle (ctx , plugin : str ):
56
59
"""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 ) )
58
61
if plugin_dir is None :
59
62
direct_user_to_plugin_directory_and_exit ()
60
63
@@ -85,10 +88,10 @@ def toggle(plugin: str):
85
88
write_yaml (updated_settings , plugin_dir / Path (plugin ) / Path (PLUGIN_YAML ))
86
89
87
90
88
- def load_user_modules () -> bool :
91
+ def load_user_modules (path : Optional [ Path ] = None ) -> bool :
89
92
was_successful_load = False
90
93
91
- plugin_dir = _get_plugins_directory ( )
94
+ plugin_dir = get_plugins_directory_or ( path )
92
95
93
96
if not plugin_dir or not plugin_dir .is_dir ():
94
97
return was_successful_load
@@ -125,15 +128,23 @@ def register_command(command):
125
128
register .add_command (command )
126
129
127
130
128
- def load_plugins (fn ):
129
- load_user_modules ()
131
+ def load_plugins ():
130
132
for module in imported_modules .values ():
131
133
for name , func in inspect .getmembers (module , inspect .isfunction ):
132
134
if name == "warnet_register_plugin" :
133
135
func (register_command )
134
136
135
137
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
+
137
148
user_dir = os .getenv (WARNET_USER_DIR_ENV_VAR )
138
149
139
150
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]:
147
158
def direct_user_to_plugin_directory_and_exit ():
148
159
click .secho ("Could not determine the plugin directory location." )
149
160
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."
151
162
)
152
163
click .secho (
153
164
"Solution 2: consider setting environment variable pointing to your Warnet project directory:"
@@ -190,7 +201,7 @@ def check_if_plugin_enabled(path: Path) -> bool:
190
201
191
202
def get_plugins_with_status (plugin_dir : Optional [Path ] = None ) -> list [tuple [Path , bool ]]:
192
203
if not plugin_dir :
193
- plugin_dir = _get_plugins_directory ()
204
+ plugin_dir = get_plugins_directory_or ()
194
205
candidates = [
195
206
Path (os .path .join (plugin_dir , name ))
196
207
for name in os .listdir (plugin_dir )
0 commit comments