Skip to content

Commit d147b22

Browse files
committed
clean up prompts
1 parent 20978fd commit d147b22

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/warnet/hooks.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,33 @@ def run(plugin: str, function: str, args: tuple[str, ...], json_dict: str):
117117
plugin_names = [
118118
plugin_name.stem for plugin_name, status in get_plugins_with_status() if status
119119
]
120-
121120
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")
123126

124127
if function == "":
125128
module = imported_modules.get(f"plugins.{plugin}")
126129
funcs = [name for name, _func in inspect.getmembers(module, inspect.isfunction)]
127130
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")
129135

130136
func = get_func(function_name=function, plugin_name=plugin)
131137
hints = get_type_hints(func)
132138
if not func:
133139
sys.exit(0)
134140

135141
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)
137147
sys.exit(0)
138148

139149
if not json_dict:
@@ -157,16 +167,28 @@ def run(plugin: str, function: str, args: tuple[str, ...], json_dict: str):
157167
message=f"Enter a value for '{name}' ({hint_name})",
158168
)
159169
]
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)
162179
click.secho(
163180
f"\nwarnet plugin run {plugin} {function} --json-dict '{json.dumps(params)}'\n",
164181
fg="green",
165182
)
166183
else:
167184
params = json.loads(json_dict)
168185

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")
170192

171193

172194
def cast_to_hint(value: str, hint: Any) -> Any:
@@ -197,6 +219,8 @@ def cast_to_hint(value: str, hint: Any) -> Any:
197219

198220

199221
def get_type_name(type_hint) -> str:
222+
if type_hint is None:
223+
return "Unknown type"
200224
if hasattr(type_hint, "__name__"):
201225
return type_hint.__name__
202226
return str(type_hint)

0 commit comments

Comments
 (0)