# apply_secrets.py -rw-r--r-- 882 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
import re
from json import load


with open('secrets.json', 'r') as file:
    secrets = load(file)

pattern_filename = re.compile(r'(.*)(yml|yaml|conf|txt|json)')
pattern_replace = r'{ ?text ?}'


def process_file(path):
    r = pattern_filename.findall(path)
    if not r:
        return
    with open(path, 'r') as f:
        text = f.read()
    for secret_name in secrets:
        text = re.sub(pattern_replace.replace('text', secret_name), secrets[secret_name], text, flags=re.I)
    with open(path, 'w') as f:
        f.write(text)


def process_folder(path=''):
    if path:
        os.chdir(path)
    for i in os.listdir():
        if i == 'secrets.json':
            continue
        if os.path.isdir(i):
            process_folder(i)
        elif os.path.isfile(i):
            process_file(i)
    os.chdir('..')


if __name__ == '__main__':
    process_folder()