Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: instead of setting environment variables, render input template…
… to output file
- Loading branch information
Showing
1 changed file
with
25 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |