From 33d416b313b0d18f333e1442ce6fece23effab33 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 15 Dec 2025 02:52:37 -0800 Subject: add output dir option --- wg-genconf.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/wg-genconf.py b/wg-genconf.py index 10a57d5..f94022d 100755 --- a/wg-genconf.py +++ b/wg-genconf.py @@ -256,22 +256,22 @@ def buf_to_file(buf, path, mode=None): buf.seek(0) shutil.copyfileobj(buf, f) -def create_if_files(if_): +def create_if_files(if_, output_dir): if_data = gc_if_data(if_) - file_prefix = f'out/{if_.get('file-prefix', '')}' + prefix = os.path.join(output_dir, if_.get('file-prefix', '')) if if_.get('type') == 'systemd': buf_to_file( gc_if_systemd_netdev(if_data), - f'{file_prefix}{if_.formatted_name}.netdev', + f'{prefix}{if_.formatted_name}.netdev', mode=0o640) buf_to_file( gc_if_systemd_network(if_data), - f'{file_prefix}{if_.formatted_name}.network') + f'{prefix}{if_.formatted_name}.network') else: buf_to_file( gc_if_wgquick(if_data), - f'{file_prefix}{if_.formatted_name}.conf') + f'{prefix}{if_.formatted_name}.conf') def load_network_peer_interfaces(config, peer): ifs = peer.pop('if', {}) @@ -318,13 +318,23 @@ def get_privkey(if_): return read_privkey(if_.format_string(os.path.expanduser( if_.get('privkey-path', '~/.wg-genconf/${network}.privkey')))) +def argparse_dir(s): + if not os.path.exists(s): + raise argparse.ArgumentTypeError(f'{s}: no such file or directory') + if not os.path.isdir(s): + raise argparse.ArgumentTypeError(f'{s}: not a directory') + return s + def main(): parser = argparse.ArgumentParser() - parser.add_argument('-c', '--config', default='~/.wg-genconf/config.toml') + parser.add_argument( + '-c', '--config', dest='config_file', + default='~/.wg-genconf/config.toml') + parser.add_argument('-o', '--output-dir', default='.', type=argparse_dir) parser.add_argument('peer_name') args = parser.parse_args() - with open(os.path.expanduser(args.config), 'rb') as f: + with open(os.path.expanduser(args.config_file), 'rb') as f: config = tomllib.load(f) networks = load_networks(config) @@ -338,6 +348,6 @@ def main(): # config files, useful when an auto-peer interface is being # separately created if if_.get('file', True): - create_if_files(if_) + create_if_files(if_, args.output_dir) main() -- cgit v1.2.3-70-g09d2