import argparse from requests import Session def forgejo_from_args(args, c: Session): from .forgejo import Forgejo return Forgejo( host=args.host, username=args.username, password=args.password, email=args.email, c=c, ) class Forgejo: def __init__(self, parser, c: Session): self.c = c self.parser = parser self.subparser = self.parser.add_subparsers() self.install() self.register() self.login() self.create_repository() self.create_issue() self.create_comment() self.create_access_token() def __add_credentials_parser(self, parser): group = parser.add_argument_group("credentials", "User credentials") group.add_argument("username", type=str, help="Forgejo user's username") group.add_argument("password", type=str, help="Forgejo user's password") group.add_argument("email", type=str, help="Forgejo user's email") group.add_argument("host", type=str, help="URI at which Forgejo is running") def install(self): def run(args, c: Session): forgejo = forgejo_from_args(args, c=c) forgejo.install() self.install_parser = self.subparser.add_parser( name="install", description="Install Forgejo", help="Install Forgejo" ) self.__add_credentials_parser(self.install_parser) self.install_parser.set_defaults(func=run) def register(self): def run(args, c: Session): forgejo = forgejo_from_args(args, c=c) forgejo.register() self.register_parser = self.subparser.add_parser( name="register", description="Forgejo user registration", help="Register a user on Forgejo", ) self.__add_credentials_parser(self.register_parser) self.register_parser.set_defaults(func=run) def login(self): def run(args, c: Session): forgejo = forgejo_from_args(args, c=c) forgejo.login() self.login_parser = self.subparser.add_parser( name="login", description="Forgejo user login", help="Login on Forgejo" ) self.__add_credentials_parser(self.login_parser) self.login_parser.set_defaults(func=run) def create_repository(self): def run(args, c: Session): forgejo = forgejo_from_args(args, c=c) forgejo.login() forgejo.create_repository(name=args.repo_name) self.create_repository_parser = self.subparser.add_parser( name="create_repo", description="Create repository on Forgejo", help="Create repository on Forgejo", ) self.__add_credentials_parser(self.create_repository_parser) self.create_repository_parser.set_defaults(func=run) self.create_repository_parser.add_argument( "repo_name", type=str, help="Name of the repository to be created" ) def create_issue(self): def run(args, c: Session): forgejo = forgejo_from_args(args, c=c) forgejo.login() forgejo.create_issue( owner=args.owner, repo=args.repo, title=args.title, body=args.body ) self.create_issue_parser = self.subparser.add_parser( name="create_issue", description="Create issue on a repository owned by someone on Forgejo", help="Create issue on Forgejo", ) self.__add_credentials_parser(self.create_issue_parser) self.create_issue_parser.set_defaults(func=run) self.create_issue_parser.add_argument( "owner", type=str, help="Owner of the repo" ) self.create_issue_parser.add_argument( "repo", type=str, help="Name of the repository" ) self.create_issue_parser.add_argument("title", type=str, help="title") self.create_issue_parser.add_argument("body", type=str, help="body") def create_comment(self): def run(args, c: Session): forgejo = forgejo_from_args(args, c=c) forgejo.login() forgejo.create_comment( owner=args.owner, repo=args.repo, issue=args.issue, body=args.body ) self.create_comment_parser = self.subparser.add_parser( name="create_comment", description="Create comment on a repository owned by someone on Forgejo", help="Create comment on Forgejo", ) self.__add_credentials_parser(self.create_comment_parser) self.create_comment_parser.set_defaults(func=run) self.create_comment_parser.add_argument( "owner", type=str, help="Owner of the repo" ) self.create_comment_parser.add_argument( "repo", type=str, help="Name of the repository" ) self.create_comment_parser.add_argument( "issue", type=int, help="ID of the issue" ) self.create_comment_parser.add_argument("body", type=str, help="body") def create_access_token(self): def run(args, c: Session): forgejo = forgejo_from_args(args, c=c) forgejo.login() forgejo.create_access_token(name=args.name, file=args.file) self.create_access_token_parser = self.subparser.add_parser( name="create_access_token", description="Create access token for user", help="Create access toekn for user", ) self.__add_credentials_parser(self.create_access_token_parser) self.create_access_token_parser.set_defaults(func=run) self.create_access_token_parser.add_argument( "name", type=str, help="name of the access token" ) self.create_access_token_parser.add_argument( "file", type=str, help="filepath to write the token value" ) class Cli: def __init__(self): c = Session() self.c = c self.parser = argparse.ArgumentParser( description="Install and Bootstrap Forgejo and Hostea Dashboard" ) self.subparser = self.parser.add_subparsers() self.check_env() self.forgejo() def __add_credentials_parser(self, parser): group = parser.add_argument_group("credentials", "User credentials") group.add_argument("username", type=str, help="Forgejo user's username") group.add_argument("password", type=str, help="Forgejo user's password") group.add_argument("email", type=str, help="Forgejo user's email") def check_env(self): def run(args, c: Session): from .forgejo import Forgejo Forgejo.check_online(host=args.forgejo_host) self.check_env_parser = self.subparser.add_parser( name="check_env", description="Check and block until environment is ready", help="Check and block until environment is ready", ) self.check_env_parser.add_argument( "forgejo_host", type=str, help="URI at which Forgejo is running" ) self.check_env_parser.set_defaults(func=run) def forgejo(self): self.forgejo = self.subparser.add_parser( name="forgejo", description="Forgejo", help="Forgejo-related functionality", ) Forgejo(parser=self.forgejo, c=self.c) def parse(self): return self.parser.parse_args()