1+ import copy
12import importlib .util
23import inspect
34import os
45import sys
6+ import tempfile
57from importlib .metadata import PackageNotFoundError , version
68from pathlib import Path
79from typing import Any , Callable , Optional
810
911import click
12+ import inquirer
1013import yaml
14+ from inquirer .themes import GreenPassion
1115
1216from warnet .constants import (
1317 HOOK_NAME_KEY ,
@@ -34,6 +38,7 @@ def plugin():
3438
3539@plugin .command ()
3640def 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+
241286def 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