Skip to content

Commit af44fda

Browse files
committed
list and toggle plugins; clean up cruft
1 parent 1889f39 commit af44fda

File tree

6 files changed

+51
-17
lines changed

6 files changed

+51
-17
lines changed

resources/plugins/ark/plugin.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
enabled: true
1+
enabled: false

resources/plugins/demo.py

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
enabled: true
1+
enabled: false
-293 Bytes
Binary file not shown.

resources/plugins/simln/charts/simln/files/sim.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"nodes": [
33
{
44
"id": "tank-0000-ln",
5-
"address": "https://tank-0000-ln:10009",
5+
"address": "https://tank-0004-ln:10009",
66
"macaroon": "/config/admin.macaroon",
77
"cert": "/config/tls.cert"
88
},
99
{
1010
"id": "tank-0001-ln",
11-
"address": "https://tank-0001-ln:10009",
11+
"address": "https://tank-0005-ln:10009",
1212
"macaroon": "/config/admin.macaroon",
1313
"cert": "/config/tls.cert"
1414
}

src/warnet/hooks.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import copy
12
import importlib.util
23
import inspect
34
import os
45
import sys
6+
import tempfile
57
from importlib.metadata import PackageNotFoundError, version
68
from pathlib import Path
79
from typing import Any, Callable, Optional
810

911
import click
12+
import inquirer
1013
import yaml
14+
from inquirer.themes import GreenPassion
1115

1216
from warnet.constants import (
1317
HOOK_NAME_KEY,
@@ -34,6 +38,7 @@ def plugin():
3438

3539
@plugin.command()
3640
def ls():
41+
"""List all available plugins and whether they are activated."""
3742
plugin_dir = get_plugin_directory()
3843

3944
if not plugin_dir:
@@ -49,6 +54,35 @@ def ls():
4954
click.secho(f"{plugin.stem:<20} disabled", fg="yellow")
5055

5156

57+
@plugin.command()
58+
@click.argument("plugin", type=str, default="")
59+
def toggle(plugin: str):
60+
"""Turn a plugin on or off"""
61+
plugin_dir = get_plugin_directory()
62+
63+
if plugin == "":
64+
plugin_list = get_plugins_with_status(plugin_dir)
65+
formatted_list = [
66+
f"{str(name.stem):<25}| enabled: {active}" for name, active in plugin_list
67+
]
68+
69+
plugins_tag = "plugins"
70+
q = [
71+
inquirer.List(
72+
name=plugins_tag,
73+
message="Toggle a plugin, or ctrl-c to cancel",
74+
choices=formatted_list,
75+
)
76+
]
77+
selected = inquirer.prompt(q, theme=GreenPassion())
78+
plugin = selected[plugins_tag].split("|")[0].strip()
79+
80+
plugin_settings = read_yaml(plugin_dir / Path(plugin) / "plugin.yaml")
81+
updated_settings = copy.deepcopy(plugin_settings)
82+
updated_settings["enabled"] = not plugin_settings["enabled"]
83+
write_yaml(updated_settings, plugin_dir / Path(plugin) / Path("plugin.yaml"))
84+
85+
5286
@plugin.command()
5387
@click.argument("plugin", type=str)
5488
@click.argument("function", type=str)
@@ -228,7 +262,7 @@ def get_version(package_name: str) -> str:
228262
sys.exit(1)
229263

230264

231-
def open_yaml(path: Path) -> dict:
265+
def read_yaml(path: Path) -> dict:
232266
try:
233267
with open(path) as file:
234268
return yaml.safe_load(file)
@@ -238,10 +272,21 @@ def open_yaml(path: Path) -> dict:
238272
raise PluginError(f"Error parsing yaml: {e}") from e
239273

240274

275+
def write_yaml(yaml_dict: dict, path: Path) -> None:
276+
dir_name = os.path.dirname(path)
277+
try:
278+
with tempfile.NamedTemporaryFile("w", dir=dir_name, delete=False) as temp_file:
279+
yaml.safe_dump(yaml_dict, temp_file)
280+
os.replace(temp_file.name, path)
281+
except Exception as e:
282+
os.remove(temp_file.name)
283+
raise PluginError(f"Error writing kubeconfig: {path}") from e
284+
285+
241286
def check_if_plugin_enabled(path: Path) -> bool:
242287
enabled = None
243288
try:
244-
plugin_dict = open_yaml(path / Path("plugin.yaml"))
289+
plugin_dict = read_yaml(path / Path("plugin.yaml"))
245290
enabled = plugin_dict.get("enabled")
246291
except PluginError as e:
247292
click.secho(e)

0 commit comments

Comments
 (0)