Skip to content

Commit

Permalink
feat(scripts): add password generator
Browse files Browse the repository at this point in the history
  • Loading branch information
rudenkornk committed Apr 27, 2024
1 parent 5652ca1 commit 034edc7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scripts/_support/password.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import secrets


def generate_password(alphabet: str, length: int) -> str:
password = "".join(secrets.choice(alphabet) for _ in range(length))
return password
23 changes: 23 additions & 0 deletions scripts/_support/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import argparse as _argparse
import logging as _logging
import string as _string
from argparse import Namespace as _Namespace
from argparse import _SubParsersAction
from pathlib import Path as _Path

from . import password as _password
from . import roles_graph as _roles_graph
from . import update as _update

_logger = _logging.getLogger(__name__)
_repo_path = _Path(__file__).parent.parent.parent


def _add_update_parser(subparsers: _SubParsersAction) -> None: # type: ignore
Expand Down Expand Up @@ -39,6 +43,15 @@ def _add_roles_graph_parser(subparsers: _SubParsersAction) -> None: # type: ign
)


def _add_password_parser(subparsers: _SubParsersAction) -> None: # type: ignore
graph_parser = subparsers.add_parser("password", help="Generate new password.")
graph_parser.add_argument("-a", "--alphabet", type=str, default=_string.ascii_lowercase, help="Password alphabet.")
graph_parser.add_argument("-l", "--length", type=int, default=24, help="Password length.")
graph_parser.add_argument(
"-o", "--output", type=_Path, default=_repo_path / "__build__" / "password.txt", help="Where to store password."
)


def _log_level_choices() -> list[str]:
return ["debug", "info", "warning", "error", "critical", "d", "i", "w", "e", "c"]

Expand Down Expand Up @@ -69,6 +82,7 @@ def _get_parser() -> _argparse.ArgumentParser:
)
_add_update_parser(subparsers)
_add_roles_graph_parser(subparsers)
_add_password_parser(subparsers)
return parser


Expand Down Expand Up @@ -105,6 +119,13 @@ def _process_roles_graph(args: _Namespace) -> None:
_roles_graph.generate_png(args.silent)


def _process_password(args: _Namespace) -> None:
password = _password.generate_password(args.alphabet, args.length)
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(password, encoding="utf-8")
_logger.info(f"Password saved to {args.output}")


def process_shell_args(shell_args: list[str]) -> None:
args = _parse_shell_args(shell_args)
log_level = _log_level(args.log_level)
Expand All @@ -113,3 +134,5 @@ def process_shell_args(shell_args: list[str]) -> None:
_process_update(args)
if args.command == "graph":
_process_roles_graph(args)
if args.command == "password":
_process_password(args)

0 comments on commit 034edc7

Please sign in to comment.