-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·62 lines (45 loc) · 1.5 KB
/
main.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import argparse
import logging
import string
import sys
import time
from random import choice, seed
import flask_bcrypt
DEFAULT_PASSWORD_LENGTH = 16
DEFAULT_LOG_LEVEL = "error"
PASSWORD_OUTPUT_KEY = "password"
PASSWORD_HASH_OUTPUT_KEY = "password-hash"
parser = argparse.ArgumentParser()
parser.add_argument(
"-l",
"--log-level",
type=str,
choices=["debug", "info", "warning", "error"],
default=DEFAULT_LOG_LEVEL,
)
parser.add_argument("-L", "--length", type=int, default=DEFAULT_PASSWORD_LENGTH)
parser.add_argument("-p", "--password", type=str, default=None)
def get_default_password(length: int) -> str:
return "".join(choice(string.ascii_letters + string.digits) for _ in range(length))
def hash_password(password: str) -> str:
return flask_bcrypt.generate_password_hash(password).decode("utf-8")
def output_result(password: str, password_hash: str):
print(
f"::set-output name={PASSWORD_OUTPUT_KEY}::{password}",
file=sys.stdout,
flush=True,
)
print(
f"::set-output name={PASSWORD_HASH_OUTPUT_KEY}::{password_hash}",
file=sys.stdout,
flush=True,
)
if __name__ == "__main__":
args = parser.parse_args(sys.argv[1:])
seed(time.time())
logging.basicConfig(level=args.log_level.upper())
password = args.password or get_default_password(args.length)
password_hash = hash_password(password)
logging.debug(f"password-hash: {password_hash}")
output_result(password, password_hash)