diff options
author | John Estabrook <jestabro@vyos.io> | 2023-10-05 12:39:48 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-05 12:39:48 -0500 |
commit | 669acb05c91aa90f28b9aee823b27c7a29afdec0 (patch) | |
tree | e65674b329776aed3fa25cfddcaa5b3afdda8a88 /src | |
parent | af17ef9b1069da7a00b4ed3ef03e7417e95150ec (diff) | |
parent | 0b832eca6d2da63bcebb324b44ceac22a925efbd (diff) | |
download | vyos-1x-669acb05c91aa90f28b9aee823b27c7a29afdec0.tar.gz vyos-1x-669acb05c91aa90f28b9aee823b27c7a29afdec0.zip |
Merge pull request #2341 from vyos/mergify/bp/sagitta/pr-2339
config: T5631: save copy of config in JSON format on commit (backport #2339)
Diffstat (limited to 'src')
-rwxr-xr-x | src/helpers/vyos-save-config.py | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/helpers/vyos-save-config.py b/src/helpers/vyos-save-config.py index 8af4a7916..518bd9864 100755 --- a/src/helpers/vyos-save-config.py +++ b/src/helpers/vyos-save-config.py @@ -19,6 +19,7 @@ import os import re import sys from tempfile import NamedTemporaryFile +from argparse import ArgumentParser from vyos.config import Config from vyos.remote import urlc @@ -28,8 +29,15 @@ from vyos.defaults import directories DEFAULT_CONFIG_PATH = os.path.join(directories['config'], 'config.boot') remote_save = None -if len(sys.argv) > 1: - save_file = sys.argv[1] +parser = ArgumentParser(description='Save configuration') +parser.add_argument('file', type=str, nargs='?', help='Save configuration to file') +parser.add_argument('--write-json-file', type=str, help='Save JSON of configuration to file') +args = parser.parse_args() +file = args.file +json_file = args.write_json_file + +if file is not None: + save_file = file else: save_file = DEFAULT_CONFIG_PATH @@ -51,6 +59,13 @@ with open(write_file, 'w') as f: f.write("\n") f.write(system_footer()) +if json_file is not None and ct is not None: + try: + with open(json_file, 'w') as f: + f.write(ct.to_json()) + except OSError as e: + print(f'failed to write JSON file: {e}') + if remote_save is not None: try: remote_save.upload(write_file) |