diff options
author | Christian Breunig <christian@breunig.cc> | 2023-10-05 09:02:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-05 09:02:19 +0200 |
commit | 61bdc12b20c7f91889ad466beea926dc90f8c9af (patch) | |
tree | e5f8b6c04d97ba58259cf8c1aaa7f8eed56f9a8b /src | |
parent | df042ef016c9cbf62aa5325ba3c7f8deb515975a (diff) | |
parent | 27605426a4ad613f45d36e7db5b1664dc3192981 (diff) | |
download | vyos-1x-61bdc12b20c7f91889ad466beea926dc90f8c9af.tar.gz vyos-1x-61bdc12b20c7f91889ad466beea926dc90f8c9af.zip |
Merge pull request #2339 from jestabro/save-json-on-commit
config: T5631: save copy of config in JSON format on commit
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) |