Refactor codebase to by DRY
This commit is contained in:
parent
c925079e8b
commit
83a526c533
13 changed files with 320 additions and 131 deletions
56
ops_bot/cli.py
Normal file
56
ops_bot/cli.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import secrets
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
import click
|
||||
|
||||
from ops_bot.config import RoutingKey, load_config, save_config
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.option(
|
||||
"--config-file", help="the path to the config file", default="config.json"
|
||||
)
|
||||
@click.pass_context
|
||||
def cli(ctx: Any, config_file: str) -> None:
|
||||
ctx.obj = config_file
|
||||
pass
|
||||
|
||||
|
||||
@cli.command(help="Add a new routing key to the configuration file")
|
||||
@click.option(
|
||||
"--name", help="a friendly detailed name for the hook so you can remember it later"
|
||||
)
|
||||
@click.option(
|
||||
"--hook-type",
|
||||
help="The type of webhook to add",
|
||||
type=click.Choice(["gitlab", "pagerduty", "aws-sns"], case_sensitive=False),
|
||||
)
|
||||
@click.option("--room-id", help="The room ID to send the messages to")
|
||||
@click.pass_obj
|
||||
def add_hook(config_file: str, name: str, hook_type: str, room_id: str) -> None:
|
||||
settings = load_config(config_file)
|
||||
path_key = secrets.token_urlsafe(30)
|
||||
secret_token = secrets.token_urlsafe(30)
|
||||
if name in set([key.name for key in settings.routing_keys]):
|
||||
print("Error: A hook with that name already exists")
|
||||
sys.exit(1)
|
||||
settings.routing_keys.append(
|
||||
RoutingKey(
|
||||
name=name,
|
||||
path_key=path_key,
|
||||
secret_token=secret_token,
|
||||
room_id=room_id,
|
||||
hook_type=hook_type,
|
||||
)
|
||||
)
|
||||
save_config(settings)
|
||||
|
||||
url = f"/hook/{path_key}"
|
||||
|
||||
print("Hook added successfully")
|
||||
print()
|
||||
print("Your webhook URL is:")
|
||||
print(f"\t{url}")
|
||||
print("The secret token is:")
|
||||
print(f"\t{secret_token}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue