move the update command to a flag
All checks were successful
build

This commit is contained in:
Joe Kaufeld 2023-03-23 21:17:58 -04:00
parent e0e7eadbe0
commit 12f020b0c8
2 changed files with 28 additions and 11 deletions

View File

@ -29,8 +29,6 @@ jobs:
make build make build
- name: Create release! - name: Create release!
run: | run: |
echo ${{ github.event.repository.name }}
echo ${GITHUB_REPOSITORY#*/}
echo """release_id=$(\ echo """release_id=$(\
curl -s https://git.joekaufeld.com/api/v1/repos/${GITHUB_REPOSITORY%/*}/${{ github.event.repository.name }}/releases \ curl -s https://git.joekaufeld.com/api/v1/repos/${GITHUB_REPOSITORY%/*}/${{ github.event.repository.name }}/releases \
-H "Authorization: token ${{ secrets.PAT }}" \ -H "Authorization: token ${{ secrets.PAT }}" \
@ -39,7 +37,6 @@ jobs:
)""" >> $GITHUB_ENV )""" >> $GITHUB_ENV
- name: Upload assets! - name: Upload assets!
run: | run: |
echo ${{ env.release_id }}
curl https://git.joekaufeld.com/api/v1/repos/${GITHUB_REPOSITORY%/*}/${{ github.event.repository.name }}/releases/${{ env.release_id }}/assets \ curl https://git.joekaufeld.com/api/v1/repos/${GITHUB_REPOSITORY%/*}/${{ github.event.repository.name }}/releases/${{ env.release_id }}/assets \
-H "Authorization: token ${{ secrets.PAT }}" \ -H "Authorization: token ${{ secrets.PAT }}" \
-F attachment=@utils -F attachment=@utils

View File

@ -7,6 +7,7 @@ import uuid
import click import click
import httpx import httpx
from rich import pretty from rich import pretty
from rich.status import Status
from rich.traceback import install from rich.traceback import install
from shiv.bootstrap import current_zipfile from shiv.bootstrap import current_zipfile
@ -21,9 +22,17 @@ from src.art import BANNERS
) )
@click.pass_context @click.pass_context
@click.version_option(version=src.__version__, prog_name="utils") @click.version_option(version=src.__version__, prog_name="utils")
def main(ctx): @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.""" """Launch a utility or drop into a command line REPL if no command is given."""
if ctx.invoked_subcommand is None: if update:
update_from_gitea()
sys.exit()
elif ctx.invoked_subcommand is None:
banner = random.choice(BANNERS) banner = random.choice(BANNERS)
pretty.install() # type: ignore pretty.install() # type: ignore
@ -91,31 +100,42 @@ def beautify(words: list[str]):
click.echo("".join(new_beautiful_string)) click.echo("".join(new_beautiful_string))
@main.command() def update_from_gitea():
def update():
"""Get the newest release from Gitea and install it.""" """Get the newest release from Gitea and install it."""
status = Status("Checking for new release...")
status.start()
response = httpx.get( response = httpx.get(
# "https://api.github.com/repos/itsthejoker/utils/releases/latest"
"https://git.joekaufeld.com/api/v1/repos/jkaufeld/utils/releases/latest" "https://git.joekaufeld.com/api/v1/repos/jkaufeld/utils/releases/latest"
) )
if response.status_code != 200: if response.status_code != 200:
status.stop()
click.echo( click.echo(
f"Something went wrong when talking to Gitea; got a" f"Something went wrong when talking to Gitea; got a"
f" {response.status_code} with the following content:\n" f" {response.status_code} with the following content:\n"
f"{response.content}" f"{response.content}"
) )
return return
status.update("Checking for new release...")
release_data = response.json() release_data = response.json()
if release_data["tag_name"] == src.__version__: if release_data["tag_name"] == src.__version__:
click.echo("Server version is the same as current version; nothing to update.") status.stop()
click.echo(
"Server version is the same as current version;" " nothing to update."
)
return return
status.update("Updating...")
url = release_data["assets"][0]["browser_download_url"] url = release_data["assets"][0]["browser_download_url"]
with current_zipfile() as archive: with current_zipfile() as archive:
with open(archive.filename, "wb") as f, httpx.stream("GET", url, follow_redirects=True) as r: with open(archive.filename, "wb") as f, httpx.stream(
"GET", url, follow_redirects=True
) as r:
for line in r.iter_bytes(): for line in r.iter_bytes():
f.write(line) f.write(line)
click.echo(f"Updated to {release_data['name']}! 🎉")
status.stop()
click.echo(f"Updated to {release_data['tag_name']}! 🎉")
if __name__ == "__main__": if __name__ == "__main__":