mirror of
https://git.srhtnin.garden/sarah/SaraSuite.git
synced 2026-10-13 18:18:12 +02:00
71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
"""Command-line mode, for machines without a desktop (e.g. a Fedora Server box):
|
|
|
|
SaraSuite list
|
|
SaraSuite install <Name> [--set Id=Value ...] [--option Id=on|off ...]
|
|
SaraSuite update <Name>
|
|
SaraSuite remove <Name> [--configs] [--keep-runtime]
|
|
|
|
Install uses each question's default and each option's default unless set.
|
|
"""
|
|
|
|
import argparse
|
|
|
|
from Installer import Apply, GetStatus, Remove
|
|
from Manifests import LoadPrograms
|
|
|
|
|
|
def _Pairs(Items: list[str]) -> dict[str, str]:
|
|
Pairs = {}
|
|
for Item in Items:
|
|
Key, Separator, Value = Item.partition("=")
|
|
if not Separator:
|
|
raise SystemExit(f"Expected Id=Value, got {Item!r}")
|
|
Pairs[Key] = Value
|
|
return Pairs
|
|
|
|
|
|
def Run(Arguments: list[str]) -> int:
|
|
Parser = argparse.ArgumentParser(prog="SaraSuite")
|
|
Commands = Parser.add_subparsers(dest="Command", required=True)
|
|
Commands.add_parser("list")
|
|
Install = Commands.add_parser("install")
|
|
Install.add_argument("Name")
|
|
Install.add_argument("--set", action="append", default=[], dest="Answers")
|
|
Install.add_argument("--option", action="append", default=[], dest="Options")
|
|
Commands.add_parser("update").add_argument("Name")
|
|
RemoveCommand = Commands.add_parser("remove")
|
|
RemoveCommand.add_argument("Name")
|
|
RemoveCommand.add_argument("--configs", action="store_true")
|
|
RemoveCommand.add_argument("--keep-runtime", action="store_true")
|
|
Args = Parser.parse_args(Arguments)
|
|
|
|
Programs = {Program.Name.lower(): Program for Program in LoadPrograms()}
|
|
if Args.Command == "list":
|
|
for Program in Programs.values():
|
|
Current = GetStatus(Program)
|
|
State = "update available" if Current.UpdateAvailable else "installed" if Current.Installed else "not installed"
|
|
print(f"{Program.Name:12} {State:16} {Current.Error}")
|
|
return 0
|
|
|
|
Program = Programs.get(Args.Name.lower())
|
|
if Program is None:
|
|
print(f"Unknown program {Args.Name!r}. Known: {', '.join(P.Name for P in Programs.values())}")
|
|
return 1
|
|
Installed = GetStatus(Program).Installed
|
|
|
|
if Args.Command == "install":
|
|
if Installed:
|
|
print(f"{Program.Name} is already installed; use update.")
|
|
return 1
|
|
Options = {Option.Id: Option.Default for Option in Program.Options}
|
|
Options.update({Key: Value.lower() in ("on", "yes", "true", "1") for Key, Value in _Pairs(Args.Options).items()})
|
|
Apply(Program, print, Options, _Pairs(Args.Answers))
|
|
elif Args.Command == "update":
|
|
if not Installed:
|
|
print(f"{Program.Name} isn't installed.")
|
|
return 1
|
|
Apply(Program, print)
|
|
elif Args.Command == "remove":
|
|
Remove(Program, Args.configs, not Args.keep_runtime, print)
|
|
return 0
|