From 02f4919da2e3b17f48eeab36d5e67437900bccef Mon Sep 17 00:00:00 2001 From: Neill Cox Date: Tue, 24 Oct 2023 13:10:59 +1100 Subject: [PATCH] :construction: change to use click instead of argparse --- pyproject.toml | 3 +- src/gitea_gitlab_exporter/__init__.py | 7 -- src/gitea_gitlab_exporter/cli.py | 101 ++++++++++++++++++++++++-- src/gitea_gitlab_exporter/utils.py | 3 + 4 files changed, 101 insertions(+), 13 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 069d0e0..987bd65 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ classifiers = [ "Operating System :: OS Independent", ] dependencies = [ + "click", "prettytable", "requests", ] @@ -26,7 +27,7 @@ dependencies = [ "Bug Tracker" = "https://git.evatt.ingenious.com.au/neillc/gitea-gitlab-exporter" [project.scripts] -gitlab2gitea = "gitea_gitlab_exporter:exporter" +gl2gt = "gitea_gitlab_exporter.cli:cli" [project.optional-dependencies] dev = [ diff --git a/src/gitea_gitlab_exporter/__init__.py b/src/gitea_gitlab_exporter/__init__.py index fa7e287..53f46b0 100644 --- a/src/gitea_gitlab_exporter/__init__.py +++ b/src/gitea_gitlab_exporter/__init__.py @@ -5,10 +5,3 @@ Copyright 2023 Neill Cox """ - -from .cli import cli_list_projects, parse_args - - -def exporter(): - args = parse_args() - cli_list_projects(args) diff --git a/src/gitea_gitlab_exporter/cli.py b/src/gitea_gitlab_exporter/cli.py index 0c60986..27ea8b9 100644 --- a/src/gitea_gitlab_exporter/cli.py +++ b/src/gitea_gitlab_exporter/cli.py @@ -4,7 +4,9 @@ import logging import os import sys +import click from prettytable import PrettyTable +from requests.exceptions import HTTPError from .api import get_project_details, get_user, list_projects @@ -76,12 +78,56 @@ def cli_get_user(args): print(json.dumps(user)) -def cli_list_projects(args): - logging.debug("cli_list_projects called") +class Context: + pass - projects = list_projects(args) - if args.format == "table": +@click.group() +@click.option("--format", default="json") +@click.option("--log-file", default="json") +@click.option( + "-t", + "--gitlab-token", + help=( + "A private access token to access GitLab with. If not specified " + "will use $GL_TOKEN. Required." + ), + envvar="GL2GT_GL_TOKEN", + required=True, +) +@click.option("--debug", is_flag=True) +@click.option("--log-file", default="gitlab2gitea.log") +@click.pass_context +def cli(ctx, format, gitlab_token, debug, log_file): + ctx.ensure_object(Context) + + ctx.obj.format = format + ctx.obj.gitlab_token = gitlab_token + ctx.obj.debug = debug + ctx.obj.log_file = log_file + + log_level = logging.INFO + if debug: + log_level = logging.DEBUG + + logging.basicConfig(filename=log_file, level=log_level) + + +@click.command() +@click.pass_context +def list_projects_click(ctx): + format = ctx.obj.format + + try: + projects = list_projects(ctx.obj) + except HTTPError as err: + if err.response.status_code == 401: + print("Invalid gitlab credentials") + sys.exit(2) + else: + raise + + if format == "table": tbl = PrettyTable() tbl.align = "l" tbl.field_names = [ @@ -100,7 +146,52 @@ def cli_list_projects(args): ) print(tbl) - elif args.format == "json": + elif format == "json": print(json.dumps(projects)) else: print(projects) + + +cli.add_command(list_projects_click) + + +@click.command() +@click.pass_context +def click_get_user(ctx): + logging.debug("cli_get_user called") + + user = get_user(ctx.obj) + + print(json.dumps(user)) + + +cli.add_command(click_get_user) + +# def cli_list_projects(args): +# logging.debug("cli_list_projects called") + +# projects = list_projects(args) + +# if args.format == "table": +# tbl = PrettyTable() +# tbl.align = "l" +# tbl.field_names = [ +# "ID", +# "Name", +# # "Description" +# ] + +# for row in projects: +# tbl.add_row( +# [ +# row["id"], +# row["name"], +# # row["description"][:50] if row["description"] else "" +# ] +# ) + +# print(tbl) +# elif args.format == "json": +# print(json.dumps(projects)) +# else: +# print(projects) diff --git a/src/gitea_gitlab_exporter/utils.py b/src/gitea_gitlab_exporter/utils.py index 45a54d4..d8291d6 100644 --- a/src/gitea_gitlab_exporter/utils.py +++ b/src/gitea_gitlab_exporter/utils.py @@ -13,6 +13,9 @@ def get(url, args): response = requests.get(url, headers={"PRIVATE-TOKEN": args.gitlab_token}) + # if not response.ok: + response.raise_for_status() + logging.debug("response.status: %s", response.status_code) logging.debug("body: %s", response.text)