Spaces:
Sleeping
Sleeping
#!/usr/bin/env python3 | |
import os | |
import argparse | |
def retrieve_secrets( | |
file_path: str | os.PathLike, | |
secret_from: str, | |
access_type: str | |
) -> str: | |
with open(file_path, 'r') as file: | |
lines = file.read().split('\n\n') | |
for block in lines: | |
if secret_from in block: | |
tokens = block.split('\n') | |
for token in tokens: | |
if token.startswith('#'): | |
continue | |
token_parts = token.split('#') | |
token_value = token_parts[0].strip() | |
token_type = token_parts[1].strip() if len(token_parts) > 1 else 'default' | |
if token_type == access_type: | |
return token_value | |
return None | |
def main(): | |
parser = argparse.ArgumentParser(description='Retrieve secrets from a file.') | |
parser.add_argument('--file-path', type=str, default="/home/el02/.ssh/lab_access.txt", help='Path to the secrets file') | |
parser.add_argument('--secret-from', type=str, default="Hugging Face Authentication", help='Source of the secret') | |
parser.add_argument('--access-type', type=str, default="write", help='Type of access') | |
args = parser.parse_args() | |
secret = retrieve_secrets(args.file_path, args.secret_from, args.access_type) | |
if secret: | |
print(secret) | |
else: | |
print("Secret not found.") | |
if __name__ == "__main__": | |
main() |