72 lines
1.8 KiB
Python
72 lines
1.8 KiB
Python
import sys
|
|
from typing import Dict
|
|
|
|
import base32_crockford as base32
|
|
import click
|
|
import tabulate
|
|
|
|
import api.projects
|
|
from config import Config
|
|
from util import require_login
|
|
from .cli import cli
|
|
|
|
|
|
@cli.group('projects', invoke_without_command=True)
|
|
@click.pass_context
|
|
@require_login
|
|
def projects(context: click.Context) -> None:
|
|
if context.invoked_subcommand is not None:
|
|
# click calls this function when a subcommand is
|
|
# invoked as well. In this case, do nothing.
|
|
return
|
|
|
|
projects_ = api.projects.get(Config['user']['api_token'])
|
|
projects_.sort(key=lambda project_: project_['name'])
|
|
|
|
aliases: Dict[int, str] = {}
|
|
for alias_, alias_id in Config['project_aliases'].items():
|
|
aliases[int(alias_id)] = alias_
|
|
|
|
table = []
|
|
for project in sorted(projects_, key=lambda project_: project_['name']):
|
|
code = base32.encode(project['id'])
|
|
alias_ = aliases.get(project['id'])
|
|
table.append((code, project['name'], alias_))
|
|
|
|
print(tabulate.tabulate(table, headers=('Code', 'Name', 'Alias')))
|
|
|
|
|
|
@projects.group('alias')
|
|
def alias() -> None:
|
|
pass
|
|
|
|
|
|
@alias.command('add')
|
|
@click.argument('code')
|
|
@click.argument('name')
|
|
def alias_add(code: str, name: str) -> None:
|
|
project_id = base32.decode(code)
|
|
Config['project_aliases'][name] = str(project_id)
|
|
Config.write()
|
|
|
|
|
|
@alias.command('rm')
|
|
@click.argument('name')
|
|
def alias_rm(name: str) -> None:
|
|
del Config['project_aliases'][name]
|
|
Config.write()
|
|
|
|
|
|
@projects.command('info')
|
|
@click.argument('name')
|
|
def info(name: str) -> None:
|
|
try:
|
|
token = Config['user']['api_token']
|
|
project_id = int(Config['project_aliases'][name])
|
|
except KeyError:
|
|
print(f'unknown alias {name}')
|
|
sys.exit(1)
|
|
|
|
project_info = api.projects.get_project(token, project_id)
|
|
print(tabulate.tabulate(project_info.items()))
|