#!/usr/bin/env python3
'''
Pieter Smit 2020-07
$ export EDITOR=/home/pieter/git/helmchart/k8sAzure/k8s-rotate-pwd-in-file.py
$ sops /env/nspreact/dev/secrets.yaml
'''
import sys
import os
import re
#print("argv",sys.argv)
with open(sys.argv[1], "r") as f:
    data = f.readlines()
countReplacement = 0
with open(sys.argv[1], "w") as f:
    for l in data:
        if "creds_password:" in l:
            countReplacement += 1
            # print("old:", l , end="")
            u = os.popen("openssl rand -base64 48").read().strip()
            nl = re.sub('creds_password: .+', f'creds_password: "{u}-202007"', l)
            # print("new:", nl)
        else:
            nl = l
        f.write(nl)
print(f"The End. {countReplacement} {sys.argv[1]}")


