58 lines
1.7 KiB
Python
Executable File
58 lines
1.7 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import subprocess
|
|
import argparse
|
|
import sys
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
config_file = Path(args.config)
|
|
config = config_file.read_text().strip()
|
|
|
|
if args.outfile and args.outfile.exists():
|
|
print("The file {args.outfile} already exists. Do you want to overwrite? y/N ", end="")
|
|
overwrite_input = input()
|
|
overwrite = overwrite_input.strip()[0].lower() == 'y'
|
|
if not overwrite:
|
|
sys.exit(0)
|
|
|
|
if not args.quiet:
|
|
print_qr_code(config)
|
|
|
|
if args.outfile:
|
|
save_qr_code_image(args.outfile, config)
|
|
|
|
|
|
def print_qr_code(config: str):
|
|
print("### QR Code:")
|
|
print(get_qr_code(config))
|
|
|
|
def get_qr_code(config: str) -> str:
|
|
process = subprocess.Popen(
|
|
["qrencode", "-t", "utf8"], stdin=subprocess.PIPE, stdout=subprocess.PIPE
|
|
)
|
|
result = process.communicate(input=config.encode("utf-8"))
|
|
return result[0].decode("utf-8")
|
|
|
|
|
|
def save_qr_code_image(filename: Path, config: str):
|
|
process = subprocess.Popen(
|
|
["qrencode", "-t", "png", "-o", filename], stdin=subprocess.PIPE
|
|
)
|
|
process.communicate(input=config.encode("utf-8"))
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
parser = argparse.ArgumentParser(description="WireGuard QR Code Generator")
|
|
parser.add_argument("config", help="The WireGuard config file to generate a qr code for")
|
|
parser.add_argument("-o", "--outfile", type=Path, help="The ouput file for the generated PNG image")
|
|
parser.add_argument("-q", "--quiet", action="store_true", help="Suppress the output of the UTF-8 QR Code")
|
|
return parser.parse_args()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|