🚧 change to use click instead of argparse
This commit is contained in:
parent
024f01c9d1
commit
02f4919da2
4 changed files with 101 additions and 13 deletions
|
|
@ -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 = [
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue