@@ -117,23 +117,33 @@ def run(plugin: str, function: str, args: tuple[str, ...], json_dict: str):
117
117
plugin_names = [
118
118
plugin_name .stem for plugin_name , status in get_plugins_with_status () if status
119
119
]
120
-
121
120
q = [inquirer .List (name = "plugin" , message = "Please choose a plugin" , choices = plugin_names )]
122
- plugin = inquirer .prompt (q , theme = GreenPassion ()).get ("plugin" )
121
+
122
+ plugin_answer = inquirer .prompt (q , theme = GreenPassion ())
123
+ if not plugin_answer :
124
+ sys .exit (0 )
125
+ plugin = plugin_answer .get ("plugin" )
123
126
124
127
if function == "" :
125
128
module = imported_modules .get (f"plugins.{ plugin } " )
126
129
funcs = [name for name , _func in inspect .getmembers (module , inspect .isfunction )]
127
130
q = [inquirer .List (name = "func" , message = "Please choose a function" , choices = funcs )]
128
- function = inquirer .prompt (q , theme = GreenPassion ()).get ("func" )
131
+ function_answer = inquirer .prompt (q , theme = GreenPassion ())
132
+ if not function_answer :
133
+ sys .exit (0 )
134
+ function = function_answer .get ("func" )
129
135
130
136
func = get_func (function_name = function , plugin_name = plugin )
131
137
hints = get_type_hints (func )
132
138
if not func :
133
139
sys .exit (0 )
134
140
135
141
if args :
136
- func (* args )
142
+ try :
143
+ func (* args )
144
+ except Exception as e :
145
+ click .secho (f"Exception: { e } " , fg = "yellow" )
146
+ sys .exit (1 )
137
147
sys .exit (0 )
138
148
139
149
if not json_dict :
@@ -157,16 +167,28 @@ def run(plugin: str, function: str, args: tuple[str, ...], json_dict: str):
157
167
message = f"Enter a value for '{ name } ' ({ hint_name } )" ,
158
168
)
159
169
]
160
- user_input = inquirer .prompt (q ).get ("input" )
161
- params [name ] = cast_to_hint (user_input , hint )
170
+ user_input_answer = inquirer .prompt (q )
171
+ if not user_input_answer :
172
+ sys .exit (0 )
173
+ user_input = user_input_answer .get ("input" )
174
+
175
+ if hint is None :
176
+ params [name ] = user_input
177
+ else :
178
+ params [name ] = cast_to_hint (user_input , hint )
162
179
click .secho (
163
180
f"\n warnet plugin run { plugin } { function } --json-dict '{ json .dumps (params )} '\n " ,
164
181
fg = "green" ,
165
182
)
166
183
else :
167
184
params = json .loads (json_dict )
168
185
169
- func (** params )
186
+ try :
187
+ return_value = func (** params )
188
+ if return_value :
189
+ click .secho (return_value )
190
+ except Exception as e :
191
+ click .secho (f"Exception: { e } " , fg = "yellow" )
170
192
171
193
172
194
def cast_to_hint (value : str , hint : Any ) -> Any :
@@ -197,6 +219,8 @@ def cast_to_hint(value: str, hint: Any) -> Any:
197
219
198
220
199
221
def get_type_name (type_hint ) -> str :
222
+ if type_hint is None :
223
+ return "Unknown type"
200
224
if hasattr (type_hint , "__name__" ):
201
225
return type_hint .__name__
202
226
return str (type_hint )
0 commit comments