# /// script # requires-python = ">=3.12" # dependencies = [ "typer"] # /// # uv run splitfgconfig.py fg-short.conf import sys import os import typer from contextlib import suppress def split_configfile(config_fn:str): dirname=os.path.dirname(config_fn) redacted_fn = f"{dirname}/redacted.cfg" cmd="sed -r 's/(password ENC|key ENC|token ENC|random-number ENC) .*/\\1 *HIDDEN*/' "+config_fn+" | sed -r '/BEGIN ENCRYPTED PRIVATE KEY/,/END ENCRYPTED PRIVATE KEY/{/^#/!{/^$/!d;};}' >"+redacted_fn os.system(cmd) outfile=None vdom_def=False in_vdom_def=False vdom_list=[] with open(redacted_fn) as infile: for line in infile: if line.find("uuid")>0: continue if not vdom_def and line.startswith("config vdom"): in_vdom_def=True vdom_list_ptr=0 continue if in_vdom_def: if line.startswith("edit"): vdom_name=line.strip().split()[1] vdom_list.append(vdom_name) elif line.startswith("end"): in_vdom_def=False vdom_def=True continue if line.startswith("config global"): subdir="global" continue elif line.startswith("config vdom"): subdir=f"vdom_{vdom_list[vdom_list_ptr]}" vdom_list_ptr+=1 continue if line.startswith("edit "): continue if line.startswith("config "): section_name="_".join(line.split()[1:2]) #print(section_name) with suppress(FileExistsError): os.mkdir(f"{dirname}/{subdir}") section_fn=f"{dirname}/{subdir}/{section_name}.cfg" if outfile: outfile.close() outfile=open(section_fn,"a") if outfile: outfile.write(line) #print(dirname) if __name__ == "__main__": typer.run(split_configfile) sys.exit()