2323 WARNET_USER_DIR_ENV_VAR ,
2424)
2525
26+ # TODO Add inquirer test
27+ # TODO get rid of piping
28+ # TODO iron out input (and test it)
29+
2630
2731class PluginError (Exception ):
2832 pass
@@ -99,35 +103,21 @@ def toggle(plugin: str):
99103@plugin .command ()
100104@click .argument ("plugin_name" , type = str , default = "" )
101105@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 ):
104109 """Explore and run plugins"""
105-
106110 show_explainer = False
107111
108112 plugin_dir = _get_plugin_directory ()
109113 if plugin_dir is None :
110114 direct_user_to_plugin_directory_and_exit ()
111115
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-
126116 plugins = get_plugins_with_status (plugin_dir )
127117 for plugin_path , status in plugins :
128118 if plugin_path .stem == plugin_name and not status :
129119 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 ." )
131121 sys .exit (0 )
132122
133123 if plugin_name == "" and sys .stdin .isatty ():
@@ -162,7 +152,20 @@ def run(plugin_name: str, function_name: str, json_dict: str):
162152 if not func :
163153 sys .exit (0 )
164154
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 :
166169 params = {}
167170 sig = inspect .signature (func )
168171 for name , param in sig .parameters .items ():
@@ -201,24 +204,21 @@ def run(plugin_name: str, function_name: str, json_dict: str):
201204 )
202205 else :
203206 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 )} '" ,
209208 fg = "green" ,
210209 )
210+
211211 else :
212- params = json .loads (json_dict )
212+ params = json .loads (json_input )
213213
214214 try :
215- processed_params = process_obj (params , func )
216- return_value = func (** processed_params )
215+ return_value = func (** params )
217216 if return_value is not None :
218217 jsonified = json .dumps (return_value )
219- print (jsonified )
218+ print (f"' { jsonified } '" )
220219 except Exception as e :
221220 click .secho (f"Exception: { e } " , fg = "yellow" )
221+ sys .exit (1 )
222222
223223
224224def process_obj (some_obj , func ) -> dict :
@@ -238,7 +238,7 @@ def process_obj(some_obj, func) -> dict:
238238 if isinstance (some_obj , dict ):
239239 return some_obj
240240 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
242242 raise ValueError ("Function parameters are fewer than the list items." )
243243 # If the function expects a single list parameter, use it directly
244244 if len (param_names ) == 1 :
0 commit comments