From 12f020b0c88bb7ba0187f87045efb2e6e8aba0cf Mon Sep 17 00:00:00 2001 From: Joe Kaufeld Date: Thu, 23 Mar 2023 21:17:58 -0400 Subject: [PATCH] move the update command to a flag --- .github/workflows/build_and_release.yml | 3 --- src/cli.py | 36 +++++++++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build_and_release.yml b/.github/workflows/build_and_release.yml index a21ed27..5fb80df 100644 --- a/.github/workflows/build_and_release.yml +++ b/.github/workflows/build_and_release.yml @@ -29,8 +29,6 @@ jobs: make build - name: Create release! run: | - echo ${{ github.event.repository.name }} - echo ${GITHUB_REPOSITORY#*/} echo """release_id=$(\ curl -s https://git.joekaufeld.com/api/v1/repos/${GITHUB_REPOSITORY%/*}/${{ github.event.repository.name }}/releases \ -H "Authorization: token ${{ secrets.PAT }}" \ @@ -39,7 +37,6 @@ jobs: )""" >> $GITHUB_ENV - name: Upload assets! run: | - echo ${{ env.release_id }} curl https://git.joekaufeld.com/api/v1/repos/${GITHUB_REPOSITORY%/*}/${{ github.event.repository.name }}/releases/${{ env.release_id }}/assets \ -H "Authorization: token ${{ secrets.PAT }}" \ -F attachment=@utils diff --git a/src/cli.py b/src/cli.py index b5cb78d..7f0b35e 100644 --- a/src/cli.py +++ b/src/cli.py @@ -7,6 +7,7 @@ import uuid import click import httpx from rich import pretty +from rich.status import Status from rich.traceback import install from shiv.bootstrap import current_zipfile @@ -21,9 +22,17 @@ from src.art import BANNERS ) @click.pass_context @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.""" - if ctx.invoked_subcommand is None: + if update: + update_from_gitea() + sys.exit() + elif ctx.invoked_subcommand is None: banner = random.choice(BANNERS) pretty.install() # type: ignore @@ -91,31 +100,42 @@ def beautify(words: list[str]): click.echo("".join(new_beautiful_string)) -@main.command() -def update(): +def update_from_gitea(): """Get the newest release from Gitea and install it.""" + status = Status("Checking for new release...") + status.start() + response = httpx.get( - # "https://api.github.com/repos/itsthejoker/utils/releases/latest" "https://git.joekaufeld.com/api/v1/repos/jkaufeld/utils/releases/latest" ) if response.status_code != 200: + status.stop() click.echo( f"Something went wrong when talking to Gitea; got a" f" {response.status_code} with the following content:\n" f"{response.content}" ) return + status.update("Checking for new release...") release_data = response.json() 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 + status.update("Updating...") url = release_data["assets"][0]["browser_download_url"] 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(): 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__":