Skip to content

Commit 389310a

Browse files
committed
improve ergonomics
1 parent 585f33d commit 389310a

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

resources/plugins/simln/simln.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def everyone_has_a_host() -> bool:
115115
result = warnet(f"ln host {name}")
116116
if len(result) > 1:
117117
host_havers += 1
118-
return host_havers == len(pods)
118+
return host_havers == len(pods) and host_havers != 0
119119

120120

121121
def wait_for_everyone_to_have_a_host():

src/warnet/plugin.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,8 @@ def util():
5151
def ls():
5252
"""List all available plugins and whether they are activated"""
5353
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()
6056

6157
for plugin, status in get_plugins_with_status(plugin_dir):
6258
if status:
@@ -70,6 +66,8 @@ def ls():
7066
def toggle(plugin: str):
7167
"""Toggle a plugin on or off"""
7268
plugin_dir = _get_plugin_directory()
69+
if plugin_dir is None:
70+
direct_user_to_plugin_directory_and_exit()
7371

7472
if plugin == "":
7573
plugin_list = get_plugins_with_status(plugin_dir)
@@ -99,21 +97,27 @@ def toggle(plugin: str):
9997

10098

10199
@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="")
104102
@click.option("--args", default="", type=str, help="Apply positional arguments to the function")
105103
@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):
107105
"""Run a command available in a plugin"""
108106
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("\nConsider setting environment variable containing your project directory:")
112+
sys.exit(0)
109113
plugins = get_plugins_with_status(plugin_dir)
110114
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:
112116
click.secho(f"The plugin '{plugin_path.stem}' is not enabled", fg="yellow")
113117
click.secho("Please toggle it on to run commands.")
114118
sys.exit(0)
115119

116-
if plugin == "":
120+
if plugin_name == "":
117121
plugin_names = [
118122
plugin_name.stem for plugin_name, status in get_plugins_with_status() if status
119123
]
@@ -122,18 +126,18 @@ def run(plugin: str, function: str, args: tuple[str, ...], json_dict: str):
122126
plugin_answer = inquirer.prompt(q, theme=GreenPassion())
123127
if not plugin_answer:
124128
sys.exit(0)
125-
plugin = plugin_answer.get("plugin")
129+
plugin_name = plugin_answer.get("plugin")
126130

127-
if function == "":
128-
module = imported_modules.get(f"plugins.{plugin}")
131+
if function_name == "":
132+
module = imported_modules.get(f"plugins.{plugin_name}")
129133
funcs = [name for name, _func in inspect.getmembers(module, inspect.isfunction)]
130134
q = [inquirer.List(name="func", message="Please choose a function", choices=funcs)]
131135
function_answer = inquirer.prompt(q, theme=GreenPassion())
132136
if not function_answer:
133137
sys.exit(0)
134-
function = function_answer.get("func")
138+
function_name = function_answer.get("func")
135139

136-
func = get_func(function_name=function, plugin_name=plugin)
140+
func = get_func(function_name=function_name, plugin_name=plugin_name)
137141
hints = get_type_hints(func)
138142
if not func:
139143
sys.exit(0)
@@ -178,20 +182,20 @@ def run(plugin: str, function: str, args: tuple[str, ...], json_dict: str):
178182
params[name] = cast_to_hint(user_input, hint)
179183
if not params:
180184
click.secho(
181-
f"\nwarnet plugin run {plugin} {function}\n",
185+
f"\nwarnet plugin run {plugin_name} {function_name}\n",
182186
fg="green",
183187
)
184188
else:
185189
click.secho(
186-
f"\nwarnet plugin run {plugin} {function} --json-dict '{json.dumps(params)}'\n",
190+
f"\nwarnet plugin run {plugin_name} {function_name} --json-dict '{json.dumps(params)}'\n",
187191
fg="green",
188192
)
189193
else:
190194
params = json.loads(json_dict)
191195

192196
try:
193197
return_value = func(**params)
194-
if return_value:
198+
if return_value is not None:
195199
click.secho(return_value)
196200
except Exception as e:
197201
click.secho(f"Exception: {e}", fg="yellow")
@@ -306,7 +310,7 @@ def create_hooks(directory: Path):
306310
)
307311
)
308312

309-
click.secho("\nConsider setting environment variable containing your project directory:")
313+
click.secho("\nConsider setting an environment variable containing your project directory:")
310314
click.secho(f"export {WARNET_USER_DIR_ENV_VAR}={directory.parent}\n", fg="yellow")
311315

312316

@@ -400,6 +404,18 @@ def _get_plugin_directory() -> Optional[Path]:
400404
return None
401405

402406

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+
403419
@util.command()
404420
def get_plugin_directory():
405421
click.secho(_get_plugin_directory())

0 commit comments

Comments
 (0)