utils/src/cli.py

156 lines
4.1 KiB
Python
Raw Normal View History

2022-07-22 15:24:11 -04:00
import code
2022-07-28 13:28:23 -04:00
import random
import string
2022-07-22 15:24:11 -04:00
import sys
2022-07-28 13:28:23 -04:00
import uuid
import click
import httpx
2022-08-11 16:14:04 -04:00
from rich import pretty
2023-03-23 21:17:58 -04:00
from rich.status import Status
2022-08-16 13:17:43 -04:00
from rich.traceback import install
2022-07-29 11:43:40 -04:00
from shiv.bootstrap import current_zipfile
2022-07-22 15:24:11 -04:00
2022-07-23 14:48:28 -04:00
import src
2022-07-28 13:28:23 -04:00
from src.helpers import flip_char
from src.joplin import process_joplin_posts
2022-07-28 13:28:23 -04:00
from src.art import BANNERS
2022-07-23 14:48:28 -04:00
2022-07-22 15:24:11 -04:00
2022-07-28 13:28:23 -04:00
@click.group(
context_settings=dict(help_option_names=["-h", "--help", "--halp"]),
invoke_without_command=True,
)
@click.pass_context
@click.version_option(version=src.__version__, prog_name="utils")
2023-03-23 21:17:58 -04:00
@click.option(
"--update",
is_flag=True,
help="Check Gitea for a new version and auto-update.",
)
def main(ctx, update):
"""Launch a utility or drop into a command line REPL if no command is given."""
2023-03-23 21:17:58 -04:00
if update:
update_from_gitea()
sys.exit()
elif ctx.invoked_subcommand is None:
2022-07-28 13:28:23 -04:00
banner = random.choice(BANNERS)
2022-08-11 16:14:04 -04:00
2022-08-16 13:17:43 -04:00
pretty.install() # type: ignore
install() # traceback handler
code.interact(banner, None)
2022-07-28 13:28:23 -04:00
sys.exit()
2022-07-28 13:28:23 -04:00
@main.command()
def uuid4():
"""Generate a random UUID4."""
click.echo(uuid.uuid4())
@main.command()
def joplin():
"""Search Joplin for notes titled 'convert' and process them."""
process_joplin_posts()
2023-04-06 14:12:48 -04:00
@main.command()
def objectid():
"""Generate a random ObjectID."""
new_id = ""
2023-04-06 14:12:48 -04:00
for _ in range(24):
new_id += random.choice(string.ascii_lowercase[:6] + string.digits)
click.echo(new_id)
@main.command()
2022-07-29 14:29:14 -04:00
@click.argument("dice")
def roll(dice: str):
"""Roll some dice. Format: utils roll 3d8"""
if "d" not in dice:
click.echo("Missing part of the call. Example: 1d10")
return
if len(dice.split("d")) != 2:
click.echo("Error parsing dice. Example: 2d6")
return
num, sides = dice.split("d")
try:
num = int(num)
sides = int(sides)
except ValueError:
click.echo("Need numbers for the dice. Example: 30d4")
return
if num < 1 or sides < 1:
click.echo("Dude. Example: 2d8")
return
values: list[int] = []
for die in range(num):
values.append(random.randint(1, sides))
if num == 1:
click.echo(f"You rolled a {values[0]}.")
else:
click.echo(f"You rolled: {'+'.join([str(item) for item in values])}")
click.echo(f"Total: {sum(values)}")
2022-07-28 13:28:23 -04:00
@main.command()
@click.argument("words", nargs=-1)
def beautify(words: list[str]):
"""
MAkE YoUr mEsSaGe bEaUtIfUl!!!1!!
WORDS is either a single string surrounded by double quotes or multiple bare words,
e.g. `utils beautify "one two three"` or `utils beautify one two three`.
"""
message = " ".join(words)
new_beautiful_string = []
for num, letter in enumerate(message):
2023-03-31 23:55:45 -04:00
if num % 2:
letter = flip_char(letter)
new_beautiful_string.append(letter)
2022-07-28 13:28:23 -04:00
click.echo("".join(new_beautiful_string))
2023-03-23 21:17:58 -04:00
def update_from_gitea():
2023-03-22 22:27:33 -04:00
"""Get the newest release from Gitea and install it."""
2023-03-23 21:17:58 -04:00
status = Status("Checking for new release...")
status.start()
2022-07-29 11:43:40 -04:00
response = httpx.get(
2023-03-22 22:27:33 -04:00
"https://git.joekaufeld.com/api/v1/repos/jkaufeld/utils/releases/latest"
2022-07-23 14:48:28 -04:00
)
2022-07-29 11:43:40 -04:00
if response.status_code != 200:
2023-03-23 21:17:58 -04:00
status.stop()
2022-07-29 14:29:14 -04:00
click.echo(
2023-03-22 22:27:33 -04:00
f"Something went wrong when talking to Gitea; got a"
2022-07-29 11:43:40 -04:00
f" {response.status_code} with the following content:\n"
f"{response.content}"
2022-07-28 13:28:23 -04:00
)
return
2023-03-23 21:17:58 -04:00
status.update("Checking for new release...")
2022-07-29 11:43:40 -04:00
release_data = response.json()
2023-03-23 19:30:25 -04:00
if release_data["tag_name"] == src.__version__:
2023-03-23 21:17:58 -04:00
status.stop()
click.echo("Server version is the same as current version; nothing to update.")
2022-07-28 13:28:23 -04:00
return
2023-03-23 21:17:58 -04:00
status.update("Updating...")
2022-07-22 15:24:11 -04:00
2022-07-29 11:43:40 -04:00
url = release_data["assets"][0]["browser_download_url"]
with current_zipfile() as archive:
2023-03-23 21:17:58 -04:00
with open(archive.filename, "wb") as f, httpx.stream(
"GET", url, follow_redirects=True
) as r:
2022-07-29 11:43:40 -04:00
for line in r.iter_bytes():
f.write(line)
2023-03-23 21:17:58 -04:00
status.stop()
click.echo(f"Updated to {release_data['tag_name']}! 🎉")
2022-07-28 13:28:23 -04:00
if __name__ == "__main__":
main()