2024-02-12 12:31:12 +00:00
|
|
|
import argparse
|
|
|
|
import importlib
|
2024-02-14 06:15:59 +00:00
|
|
|
import json
|
2024-02-12 12:31:12 +00:00
|
|
|
import logging
|
|
|
|
|
2024-05-30 17:51:53 +00:00
|
|
|
from ..completions import add_dynamic_completer, complete_machines
|
2024-02-12 12:31:12 +00:00
|
|
|
from ..machines.machines import Machine
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2024-03-23 04:05:31 +00:00
|
|
|
# TODO get also secret facts
|
2024-02-12 12:31:12 +00:00
|
|
|
def get_all_facts(machine: Machine) -> dict:
|
2024-03-23 04:05:31 +00:00
|
|
|
public_facts_module = importlib.import_module(machine.public_facts_module)
|
|
|
|
public_facts_store = public_facts_module.FactStore(machine=machine)
|
2024-02-12 12:31:12 +00:00
|
|
|
|
|
|
|
# for service in machine.secrets_data:
|
|
|
|
# facts[service] = {}
|
|
|
|
# for fact in machine.secrets_data[service]["facts"]:
|
|
|
|
# fact_content = fact_store.get(service, fact)
|
|
|
|
# if fact_content:
|
|
|
|
# facts[service][fact] = fact_content.decode()
|
|
|
|
# else:
|
|
|
|
# log.error(f"Fact {fact} for service {service} is missing")
|
2024-03-23 04:05:31 +00:00
|
|
|
return public_facts_store.get_all()
|
2024-02-12 12:31:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_command(args: argparse.Namespace) -> None:
|
|
|
|
machine = Machine(name=args.machine, flake=args.flake)
|
2024-05-26 12:26:08 +00:00
|
|
|
|
|
|
|
# the raw_facts are bytestrings making them not json serializable
|
|
|
|
raw_facts = get_all_facts(machine)
|
|
|
|
facts = dict()
|
|
|
|
for key in raw_facts["TODO"]:
|
|
|
|
facts[key] = raw_facts["TODO"][key].decode("utf8")
|
|
|
|
|
|
|
|
print(json.dumps(facts, indent=4))
|
2024-02-12 12:31:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register_list_parser(parser: argparse.ArgumentParser) -> None:
|
2024-05-30 17:51:53 +00:00
|
|
|
machines_parser = parser.add_argument(
|
2024-02-12 12:31:12 +00:00
|
|
|
"machine",
|
|
|
|
help="The machine to print facts for",
|
|
|
|
)
|
2024-05-30 17:51:53 +00:00
|
|
|
add_dynamic_completer(machines_parser, complete_machines)
|
|
|
|
|
2024-02-12 12:31:12 +00:00
|
|
|
parser.set_defaults(func=get_command)
|