feat(aurweb-config): add unset action and simplify

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-27 22:34:15 -08:00
parent 199622c53f
commit 0e938209af
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 78 additions and 26 deletions

View file

@ -11,35 +11,43 @@ import sys
import aurweb.config
def action_set(args):
def do_action(func, *args, save: bool = True):
# If AUR_CONFIG_IMMUTABLE is defined, skip out on config setting.
if os.environ.get("AUR_CONFIG_IMMUTABLE", 0):
if int(os.environ.get("AUR_CONFIG_IMMUTABLE", 0)):
return
value = None
try:
value = func(*args)
if save:
aurweb.config.save()
except configparser.NoSectionError:
print("error: no section found", file=sys.stderr)
except configparser.NoOptionError:
print("error: no option found", file=sys.stderr)
return value
def action_set(args):
if not args.value:
print("error: no value provided", file=sys.stderr)
return
do_action(aurweb.config.set_option, args.section, args.option, args.value)
try:
aurweb.config.replace_key(args.section, args.option, args.value)
aurweb.config.save()
except configparser.NoSectionError:
print("error: no section found", file=sys.stderr)
def action_unset(args):
do_action(aurweb.config.unset_option, args.section, args.option)
def action_get(args):
try:
value = aurweb.config.get(args.section, args.option)
print(value)
except (configparser.NoSectionError):
print("error: no section found", file=sys.stderr)
except (configparser.NoOptionError):
print("error: no option found", file=sys.stderr)
val = do_action(aurweb.config.get, args.section, args.option, save=False)
print(val)
def parse_args():
fmt_cls = argparse.RawDescriptionHelpFormatter
actions = ["get", "set"]
actions = ["get", "set", "unset"]
parser = argparse.ArgumentParser(
description="aurweb configuration tool",
formatter_class=lambda prog: fmt_cls(prog=prog, max_help_position=80))