From c0c4aaf77909b9650212c4b430fe828666554fe9 Mon Sep 17 00:00:00 2001 From: Demetry Pascal Date: Fri, 7 Jul 2023 22:26:43 +0300 Subject: [PATCH] add file functions --- envsubst.py | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/envsubst.py b/envsubst.py index 481aaad..7ab39ac 100644 --- a/envsubst.py +++ b/envsubst.py @@ -7,7 +7,7 @@ """ # MIT License # -# Copyright (c) 2019 Alex Shafer +# Copyright (c) 2019 Alex Shafer, Demetry Pascal # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -27,16 +27,21 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from typing import Union, Optional + import re import os import sys +from pathlib import Path + +#region UTILS _simple_re = re.compile(r'(? str: """ Substitute environment variables in the given string @@ -102,6 +116,28 @@ def envsubst(string): return b +def envsubst_from_file(path: Union[str, os.PathLike], encoding: Optional[str] = 'utf-8') -> str: + """reads file with env variables substitutions""" + text = Path(path).read_text(encoding=encoding) + return envsubst(text) + + +def envsubst_file_convert( + path_in: Union[str, os.PathLike], + path_out: Optional[Union[str, os.PathLike]] = None, + encoding: Optional[str] = 'utf-8' +) -> None: + """performs env variables substitutions on file and saves result to file""" + + path_out = path_out or path_in + mkdir_of_file(path_out) + Path(path_out).write_text( + envsubst_from_file(path_in, encoding=encoding), + encoding=encoding + ) + + + def main(): opened = False f = sys.stdin