Added comments
This commit is contained in:
71
cli.py
71
cli.py
@@ -20,24 +20,61 @@ TEMPLATE_DIR = "./templates"
|
||||
|
||||
|
||||
#region util
|
||||
def gen_pass() -> str:
|
||||
def gen_pass(pass_length: int = 20) -> str:
|
||||
"""Generates a simple password with the provided length
|
||||
|
||||
Args:
|
||||
pass_length (int, optional): Length of password. Defaults to 20.
|
||||
|
||||
Returns:
|
||||
str: The generated password.
|
||||
"""
|
||||
alphabet = string.ascii_letters + string.digits
|
||||
password = "".join(secrets.choice(alphabet) for _ in range(20))
|
||||
password = "".join(secrets.choice(alphabet) for _ in range(pass_length))
|
||||
return password
|
||||
|
||||
|
||||
def check_positive(value: str):
|
||||
def check_positive(value: str) -> int:
|
||||
"""`argparse` type helper to check whether a given input is a positive integer.
|
||||
|
||||
Args:
|
||||
value (str): Input to validate
|
||||
|
||||
Raises:
|
||||
Exception: Input is neither a decimal or positive
|
||||
|
||||
Returns:
|
||||
int: The parsed input if valid
|
||||
"""
|
||||
ivalue = int(value)
|
||||
if ivalue <= 0:
|
||||
raise Exception("Supplied number must be >= 0")
|
||||
if ivalue < 0:
|
||||
raise Exception(f"Supplied number must be >= 0")
|
||||
return ivalue
|
||||
|
||||
|
||||
def encode_member(member: str, mapping: Mapping[str, Any]) -> str:
|
||||
"""Encodes the member-entry of an inventory file with additional mappings.
|
||||
|
||||
Args:
|
||||
member (str)
|
||||
mapping (Mapping[str, Any])
|
||||
|
||||
Returns:
|
||||
str: member with mapping encoded
|
||||
"""
|
||||
return member + " " + " ".join([f"{k}={v}" for k, v in mapping.items()])
|
||||
|
||||
|
||||
def iter_ips(ip_format: str, start_octet: int):
|
||||
"""Simple iterator for generating ip's
|
||||
|
||||
Args:
|
||||
ip_format (str)
|
||||
start_octet (int)
|
||||
|
||||
Yields:
|
||||
Yeah idk too lazy too look up what the type annotation for a generator is
|
||||
"""
|
||||
ip_int = start_octet
|
||||
ip_fmt = ip_format
|
||||
while ip_int < 255:
|
||||
@@ -46,6 +83,13 @@ def iter_ips(ip_format: str, start_octet: int):
|
||||
|
||||
|
||||
def copy_template(src: str, dest: str, mapping: Mapping[str, Any] = {}):
|
||||
"""Templates and writes a template file using Jinja as templating engine.
|
||||
|
||||
Args:
|
||||
src (str): content of the file
|
||||
dest (str): place to write templated file to
|
||||
mapping (Mapping[str, Any], optional): datamapping. Defaults to {}.
|
||||
"""
|
||||
c = Path(src).read_text()
|
||||
t: Template = Template(c)
|
||||
r = t.render(mapping)
|
||||
@@ -63,6 +107,15 @@ class MachineResources:
|
||||
|
||||
@staticmethod
|
||||
def from_prompt() -> "MachineResources":
|
||||
"""Generate `MachineResources` from prompt.
|
||||
|
||||
Raises:
|
||||
Exception
|
||||
Exception
|
||||
|
||||
Returns:
|
||||
MachineResources
|
||||
"""
|
||||
cpus = input(
|
||||
"How many processors would you like to assign (default=1): ")
|
||||
if not cpus:
|
||||
@@ -84,7 +137,9 @@ class MachineResources:
|
||||
|
||||
|
||||
class InventoryWriter:
|
||||
|
||||
"""
|
||||
Helper class for generating Ansible inventory files.
|
||||
"""
|
||||
def __init__(self, location: str) -> None:
|
||||
self._file_handle = Path(location)
|
||||
self._groups: dict[str, set[str]] = DefaultDict(set)
|
||||
@@ -112,7 +167,7 @@ def list_envs(args: argparse.Namespace):
|
||||
customer_path = path.join("customers", args.customer_name, "envs")
|
||||
print(" ".join(os.listdir(customer_path)))
|
||||
except FileNotFoundError:
|
||||
raise Exception(f"Customer `{args.customer_name}` does not exist.")
|
||||
print(f"Customer `{args.customer_name}` does not exist.")
|
||||
|
||||
|
||||
def delete_env(args: argparse.Namespace):
|
||||
@@ -282,7 +337,7 @@ def main() -> int:
|
||||
cenv_parser.add_argument("--ip-int",
|
||||
type=check_positive,
|
||||
help="4th octet to start at",
|
||||
default=10)
|
||||
default="10")
|
||||
cenv_parser.set_defaults(func=create_env)
|
||||
|
||||
# CLI definition for positional arg "delete"
|
||||
|
||||
Reference in New Issue
Block a user