summaryrefslogtreecommitdiff
path: root/src/helpers
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2023-10-04 11:10:22 -0500
committerMergify <37929162+mergify[bot]@users.noreply.github.com>2023-10-05 17:34:48 +0000
commit0b832eca6d2da63bcebb324b44ceac22a925efbd (patch)
treee65674b329776aed3fa25cfddcaa5b3afdda8a88 /src/helpers
parentaf17ef9b1069da7a00b4ed3ef03e7417e95150ec (diff)
downloadvyos-1x-0b832eca6d2da63bcebb324b44ceac22a925efbd.tar.gz
vyos-1x-0b832eca6d2da63bcebb324b44ceac22a925efbd.zip
config: T5631: save copy of config in JSON format on commit
(cherry picked from commit 27605426a4ad613f45d36e7db5b1664dc3192981)
Diffstat (limited to 'src/helpers')
-rwxr-xr-xsrc/helpers/vyos-save-config.py19
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)