import argparse
import yaml
import os
import time

def load_yaml(file_path):
    with open(file_path, 'r') as file:
        data = yaml.safe_load(file)
    return data

def main():
    
    config_file = "charts/values.yaml"
    parser = argparse.ArgumentParser(description="Parse command")

    # Add arguments
    parser.add_argument("-p", "--project-name", dest="project_name", type=str, required=True, help="Project Name")
    parser.add_argument("-v", "--project-version", dest="project_version", type=str, required=False, help="Project Version")    

    # Parse arguments
    args = parser.parse_args()

    if args.project_name == "":
        print("[*] No service proxy to apply")
    else:
        s = load_yaml(config_file)
    
        isExists = False
        if s['services'] is None: 
            s['services'] = list()
            
        for svc in s['services']:
            if svc['name'] == args.project_name:
                print('[*] Service already exists, do nothing ...')
                isExists = True
                break
        
        if isExists == False:
            print('[*] Service is not exists, adding service to config file ...')
            s['services'].append(
                {
                    'name': args.project_name
                }
            )

            with open(config_file, 'w') as f:
                yaml.dump(s, f, default_flow_style=False)

if __name__ == "__main__":
    main()