@@ -51,12 +51,8 @@ def util():
51
51
def ls ():
52
52
"""List all available plugins and whether they are activated"""
53
53
plugin_dir = _get_plugin_directory ()
54
-
55
- if not plugin_dir :
56
- click .secho ("Could not determine the plugin directory location." )
57
- click .secho ("Consider setting environment variable containing your project directory:" )
58
- click .secho (f"export { WARNET_USER_DIR_ENV_VAR } =/home/user/path/to/project/" , fg = "yellow" )
59
- sys .exit (1 )
54
+ if plugin_dir is None :
55
+ direct_user_to_plugin_directory_and_exit ()
60
56
61
57
for plugin , status in get_plugins_with_status (plugin_dir ):
62
58
if status :
@@ -70,6 +66,8 @@ def ls():
70
66
def toggle (plugin : str ):
71
67
"""Toggle a plugin on or off"""
72
68
plugin_dir = _get_plugin_directory ()
69
+ if plugin_dir is None :
70
+ direct_user_to_plugin_directory_and_exit ()
73
71
74
72
if plugin == "" :
75
73
plugin_list = get_plugins_with_status (plugin_dir )
@@ -99,21 +97,27 @@ def toggle(plugin: str):
99
97
100
98
101
99
@plugin .command ()
102
- @click .argument ("plugin " , type = str , default = "" )
103
- @click .argument ("function " , type = str , default = "" )
100
+ @click .argument ("plugin_name " , type = str , default = "" )
101
+ @click .argument ("function_name " , type = str , default = "" )
104
102
@click .option ("--args" , default = "" , type = str , help = "Apply positional arguments to the function" )
105
103
@click .option ("--json-dict" , default = "" , type = str , help = "Use json dict to populate parameters" )
106
- def run (plugin : str , function : str , args : tuple [str , ...], json_dict : str ):
104
+ def run (plugin_name : str , function_name : str , args : tuple [str , ...], json_dict : str ):
107
105
"""Run a command available in a plugin"""
108
106
plugin_dir = _get_plugin_directory ()
107
+ if plugin_dir is None :
108
+ direct_user_to_plugin_directory_and_exit ()
109
+
110
+ if not plugin_dir :
111
+ click .secho ("\n Consider setting environment variable containing your project directory:" )
112
+ sys .exit (0 )
109
113
plugins = get_plugins_with_status (plugin_dir )
110
114
for plugin_path , status in plugins :
111
- if plugin_path .stem == plugin and not status :
115
+ if plugin_path .stem == plugin_name and not status :
112
116
click .secho (f"The plugin '{ plugin_path .stem } ' is not enabled" , fg = "yellow" )
113
117
click .secho ("Please toggle it on to run commands." )
114
118
sys .exit (0 )
115
119
116
- if plugin == "" :
120
+ if plugin_name == "" :
117
121
plugin_names = [
118
122
plugin_name .stem for plugin_name , status in get_plugins_with_status () if status
119
123
]
@@ -122,18 +126,18 @@ def run(plugin: str, function: str, args: tuple[str, ...], json_dict: str):
122
126
plugin_answer = inquirer .prompt (q , theme = GreenPassion ())
123
127
if not plugin_answer :
124
128
sys .exit (0 )
125
- plugin = plugin_answer .get ("plugin" )
129
+ plugin_name = plugin_answer .get ("plugin" )
126
130
127
- if function == "" :
128
- module = imported_modules .get (f"plugins.{ plugin } " )
131
+ if function_name == "" :
132
+ module = imported_modules .get (f"plugins.{ plugin_name } " )
129
133
funcs = [name for name , _func in inspect .getmembers (module , inspect .isfunction )]
130
134
q = [inquirer .List (name = "func" , message = "Please choose a function" , choices = funcs )]
131
135
function_answer = inquirer .prompt (q , theme = GreenPassion ())
132
136
if not function_answer :
133
137
sys .exit (0 )
134
- function = function_answer .get ("func" )
138
+ function_name = function_answer .get ("func" )
135
139
136
- func = get_func (function_name = function , plugin_name = plugin )
140
+ func = get_func (function_name = function_name , plugin_name = plugin_name )
137
141
hints = get_type_hints (func )
138
142
if not func :
139
143
sys .exit (0 )
@@ -178,20 +182,20 @@ def run(plugin: str, function: str, args: tuple[str, ...], json_dict: str):
178
182
params [name ] = cast_to_hint (user_input , hint )
179
183
if not params :
180
184
click .secho (
181
- f"\n warnet plugin run { plugin } { function } \n " ,
185
+ f"\n warnet plugin run { plugin_name } { function_name } \n " ,
182
186
fg = "green" ,
183
187
)
184
188
else :
185
189
click .secho (
186
- f"\n warnet plugin run { plugin } { function } --json-dict '{ json .dumps (params )} '\n " ,
190
+ f"\n warnet plugin run { plugin_name } { function_name } --json-dict '{ json .dumps (params )} '\n " ,
187
191
fg = "green" ,
188
192
)
189
193
else :
190
194
params = json .loads (json_dict )
191
195
192
196
try :
193
197
return_value = func (** params )
194
- if return_value :
198
+ if return_value is not None :
195
199
click .secho (return_value )
196
200
except Exception as e :
197
201
click .secho (f"Exception: { e } " , fg = "yellow" )
@@ -306,7 +310,7 @@ def create_hooks(directory: Path):
306
310
)
307
311
)
308
312
309
- click .secho ("\n Consider setting environment variable containing your project directory:" )
313
+ click .secho ("\n Consider setting an environment variable containing your project directory:" )
310
314
click .secho (f"export { WARNET_USER_DIR_ENV_VAR } ={ directory .parent } \n " , fg = "yellow" )
311
315
312
316
@@ -400,6 +404,18 @@ def _get_plugin_directory() -> Optional[Path]:
400
404
return None
401
405
402
406
407
+ def direct_user_to_plugin_directory_and_exit ():
408
+ click .secho ("Could not determine the plugin directory location." )
409
+ click .secho (
410
+ "Solution 1: try runing this command again, but this time from your initialized warnet directory."
411
+ )
412
+ click .secho (
413
+ "Solution 2: consider setting environment variable pointing to your Warnet project directory:"
414
+ )
415
+ click .secho (f"export { WARNET_USER_DIR_ENV_VAR } =/home/user/path/to/project/" , fg = "yellow" )
416
+ sys .exit (1 )
417
+
418
+
403
419
@util .command ()
404
420
def get_plugin_directory ():
405
421
click .secho (_get_plugin_directory ())
0 commit comments