Skip to content

Commit

Permalink
feat: instead of setting environment variables, render input template…
Browse files Browse the repository at this point in the history
… to output file
  • Loading branch information
cfm committed Mar 12, 2024
1 parent 6415129 commit 71bca13
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions with-qubesdb
@@ -1,24 +1,38 @@
#!/usr/bin/env python3
import argparse
import subprocess
from jinja2 import Template

from qubesdb import QubesDB

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Use -k/--key one or more times to read the specified key from QubesDB. All subsequent arguments will be interpreted as the command to be invoked with those keys set as environment variables.")
parser.add_argument("-k", "--key", action="append")
args = parser.parse_known_args()

def get_env(args):
env = {}
try:
db = QubesDB()
for key in args[0].key or []:
for key in args or []:
value = db.read(f"/vm-config/{key}")
env[key] = value or ''

if len(args[1]) > 0:
cmd = subprocess.Popen(args[1], env=env)
cmd.wait()
env[key] = (value or "").decode()

finally:
db.close()
db.close()

return env


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Use -k/--key one or more times to read the specified key from QubesDB. The input template will be written to the output file with those variables substituted."
)
parser.add_argument("-k", "--key", action="append")
parser.add_argument("input")
parser.add_argument("output")
args = parser.parse_args()
env = get_env(args.key)

with open(args.input) as input_f:
template = Template(input_f.read())

with open(args.output, "w") as output_f:
rendered = template.render(**env)
output_f.write(rendered)

0 comments on commit 71bca13

Please sign in to comment.