Skip to content

Commit cad5d18

Browse files
authored
Merge pull request #798 from pdxlocations/export-utf8
Allow forced UTF-8 encoding for --export-config
2 parents 706d064 + 0ae23ee commit cad5d18

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

meshtastic/__main__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,20 @@ def onConnected(interface):
755755
if args.dest != BROADCAST_ADDR:
756756
print("Exporting configuration of remote nodes is not supported.")
757757
return
758-
# export the configuration (the opposite of '--configure')
758+
759759
closeNow = True
760-
export_config(interface)
760+
config_txt = export_config(interface)
761+
762+
if args.export_config == "-":
763+
# Output to stdout (preserves legacy use of `> file.yaml`)
764+
print(config_txt)
765+
else:
766+
try:
767+
with open(args.export_config, "w", encoding="utf-8") as f:
768+
f.write(config_txt)
769+
print(f"Exported configuration to {args.export_config}")
770+
except Exception as e:
771+
meshtastic.util.our_exit(f"ERROR: Failed to write config file: {e}")
761772

762773
if args.ch_set_url:
763774
closeNow = True
@@ -1160,7 +1171,6 @@ def export_config(interface) -> str:
11601171
config_txt = "# start of Meshtastic configure yaml\n" #checkme - "config" (now changed to config_out)
11611172
#was used as a string here and a Dictionary above
11621173
config_txt += yaml.dump(configObj)
1163-
print(config_txt)
11641174
return config_txt
11651175

11661176

@@ -1460,8 +1470,10 @@ def addImportExportArgs(parser: argparse.ArgumentParser) -> argparse.ArgumentPar
14601470
)
14611471
group.add_argument(
14621472
"--export-config",
1463-
help="Export the configuration in yaml(.yml) format.",
1464-
action="store_true",
1473+
nargs="?",
1474+
const="-", # default to "-" if no value provided
1475+
metavar="FILE",
1476+
help="Export device config as YAML (to stdout if no file given)"
14651477
)
14661478
return parser
14671479

meshtastic/tests/test_main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,8 @@ def test_main_export_config(capsys):
17381738
fixed_position: true
17391739
position_flags: 35"""
17401740
export_config(mo)
1741-
out, err = capsys.readouterr()
1741+
out = export_config(mo)
1742+
err = ""
17421743

17431744
# ensure we do not output this line
17441745
assert not re.search(r"Connected to radio", out, re.MULTILINE)

0 commit comments

Comments
 (0)