23
23
WARNET_USER_DIR_ENV_VAR ,
24
24
)
25
25
26
+ # TODO Add inquirer test
27
+ # TODO get rid of piping
28
+ # TODO iron out input (and test it)
29
+
26
30
27
31
class PluginError (Exception ):
28
32
pass
@@ -99,35 +103,21 @@ def toggle(plugin: str):
99
103
@plugin .command ()
100
104
@click .argument ("plugin_name" , type = str , default = "" )
101
105
@click .argument ("function_name" , type = str , default = "" )
102
- @click .option ("--json-dict" , default = "" , type = str , help = "Use json dict to populate parameters" )
103
- def run (plugin_name : str , function_name : str , json_dict : str ):
106
+ @click .option ("--params" , type = str , default = "" )
107
+ @click .option ("--json-input" , type = str , default = "" )
108
+ def run (plugin_name : str , function_name : str , params : str , json_input : str ):
104
109
"""Explore and run plugins"""
105
-
106
110
show_explainer = False
107
111
108
112
plugin_dir = _get_plugin_directory ()
109
113
if plugin_dir is None :
110
114
direct_user_to_plugin_directory_and_exit ()
111
115
112
- if not json_dict and not sys .stdin .isatty ():
113
- # read from a pipe: $ echo "something" | warnet plugin run
114
- json_dict = sys .stdin .read ()
115
- if not plugin_name or not function_name :
116
- click .secho (
117
- "You must specify a plugin name and function name when piping in data." , fg = "yellow"
118
- )
119
- click .secho ("Alternative: warnet plugin run --json-dict YOUR_DATA_HERE" )
120
- sys .exit (1 )
121
-
122
- if not plugin_dir :
123
- click .secho ("\n Consider setting environment variable containing your project directory:" )
124
- sys .exit (0 )
125
-
126
116
plugins = get_plugins_with_status (plugin_dir )
127
117
for plugin_path , status in plugins :
128
118
if plugin_path .stem == plugin_name and not status :
129
119
click .secho (f"The plugin '{ plugin_path .stem } ' is not enabled" , fg = "yellow" )
130
- click .secho ("Please toggle it on to run commands ." )
120
+ click .secho ("Please toggle it on to use it ." )
131
121
sys .exit (0 )
132
122
133
123
if plugin_name == "" and sys .stdin .isatty ():
@@ -162,7 +152,20 @@ def run(plugin_name: str, function_name: str, json_dict: str):
162
152
if not func :
163
153
sys .exit (0 )
164
154
165
- if not json_dict :
155
+ if params :
156
+ print (params )
157
+ params = json .loads (params )
158
+ try :
159
+ return_value = func (* params )
160
+ if return_value is not None :
161
+ jsonified = json .dumps (return_value )
162
+ print (f"'{ jsonified } '" )
163
+ sys .exit (0 )
164
+ except Exception as e :
165
+ click .secho (f"Exception: { e } " , fg = "yellow" )
166
+ sys .exit (1 )
167
+
168
+ if not json_input and not params :
166
169
params = {}
167
170
sig = inspect .signature (func )
168
171
for name , param in sig .parameters .items ():
@@ -201,24 +204,21 @@ def run(plugin_name: str, function_name: str, json_dict: str):
201
204
)
202
205
else :
203
206
click .secho (
204
- f"\n warnet plugin run { plugin_name } { function_name } --json-dict '{ json .dumps (params )} '" ,
205
- fg = "green" ,
206
- )
207
- click .secho (
208
- f"echo '{ json .dumps (params )} ' | warnet plugin run { plugin_name } { function_name } \n " ,
207
+ f"\n warnet plugin run { plugin_name } { function_name } --json-input '{ json .dumps (params )} '" ,
209
208
fg = "green" ,
210
209
)
210
+
211
211
else :
212
- params = json .loads (json_dict )
212
+ params = json .loads (json_input )
213
213
214
214
try :
215
- processed_params = process_obj (params , func )
216
- return_value = func (** processed_params )
215
+ return_value = func (** params )
217
216
if return_value is not None :
218
217
jsonified = json .dumps (return_value )
219
- print (jsonified )
218
+ print (f"' { jsonified } '" )
220
219
except Exception as e :
221
220
click .secho (f"Exception: { e } " , fg = "yellow" )
221
+ sys .exit (1 )
222
222
223
223
224
224
def process_obj (some_obj , func ) -> dict :
@@ -238,7 +238,7 @@ def process_obj(some_obj, func) -> dict:
238
238
if isinstance (some_obj , dict ):
239
239
return some_obj
240
240
elif isinstance (some_obj , list ):
241
- if len (param_names ) < len (some_obj ):
241
+ if len (param_names ) < len (some_obj ): # TODO: Move this b/c it shortcuts
242
242
raise ValueError ("Function parameters are fewer than the list items." )
243
243
# If the function expects a single list parameter, use it directly
244
244
if len (param_names ) == 1 :
0 commit comments