refactor: replace global command class to typer functions
This commit is contained in:
+50
-56
@@ -1,7 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import locale
|
||||
from argparse import Namespace, ArgumentParser
|
||||
from argparse import Namespace
|
||||
from typing import Optional
|
||||
import typer
|
||||
|
||||
from git_flow import GitFlowError
|
||||
from git_flow.command.base import Command
|
||||
@@ -19,62 +21,56 @@ REPOSITORY_TOKEN_PATH = ".repository-token"
|
||||
LOCALE = "es_AR.UTF-8"
|
||||
|
||||
|
||||
class GitFlowCommand(Command):
|
||||
commands: tuple[Command, ...]
|
||||
|
||||
def __init__(self, *args: Command) -> None:
|
||||
self.commands = args
|
||||
|
||||
def name(self) -> str:
|
||||
return "git-flow"
|
||||
|
||||
def description(self) -> str:
|
||||
return """Git flow es una herramienta para automatizar un workflow siguiendo conventional commits, conventional branch y semver."""
|
||||
|
||||
def setup_parser(self, parser: ArgumentParser) -> ArgumentParser:
|
||||
subparsers = parser.add_subparsers(
|
||||
title="comandos", dest="command", required=True
|
||||
)
|
||||
|
||||
for command in self.commands:
|
||||
subparser = subparsers.add_parser(
|
||||
command.name(), description=command.description()
|
||||
)
|
||||
command.setup_parser(subparser)
|
||||
|
||||
return parser
|
||||
|
||||
def get_parser(self) -> ArgumentParser:
|
||||
parser = ArgumentParser(self.name(), description=self.description())
|
||||
|
||||
return self.setup_parser(parser)
|
||||
|
||||
def run(self, args: Namespace = Namespace()):
|
||||
args = self.get_parser().parse_args()
|
||||
|
||||
for command in self.commands:
|
||||
if command.name() == args.command:
|
||||
command.init()
|
||||
return command.run(args)
|
||||
app = typer.Typer()
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
locale.setlocale(locale.LC_ALL, LOCALE)
|
||||
except locale.Error as e:
|
||||
print_warning(f"No se pudo configurar el locale '{LOCALE}': {e}")
|
||||
@app.command()
|
||||
def init():
|
||||
run(InitCommand())
|
||||
|
||||
command = GitFlowCommand(
|
||||
InitCommand(),
|
||||
NewCommand(),
|
||||
CommitCommand(),
|
||||
MergeCommand(),
|
||||
TagCommand(),
|
||||
ReleaseCommand(),
|
||||
BranchCommand()
|
||||
|
||||
@app.command()
|
||||
def new():
|
||||
run(NewCommand())
|
||||
|
||||
|
||||
@app.command()
|
||||
def commit():
|
||||
run(CommitCommand())
|
||||
|
||||
|
||||
@app.command()
|
||||
def merge():
|
||||
run(MergeCommand())
|
||||
|
||||
|
||||
@app.command()
|
||||
def tag(token: Optional[str] = None):
|
||||
run(TagCommand(), Namespace(token = token))
|
||||
|
||||
|
||||
@app.command()
|
||||
def release(group: Optional[str] = None):
|
||||
run(ReleaseCommand(), Namespace(group = group))
|
||||
|
||||
|
||||
@app.command()
|
||||
def branch(env: Optional[str] = None, trash: bool = False, wip: bool = False, all: bool = False):
|
||||
args = Namespace(
|
||||
environment = env,
|
||||
trash = trash,
|
||||
wip = wip,
|
||||
all = all,
|
||||
)
|
||||
|
||||
run(BranchCommand(), args)
|
||||
|
||||
|
||||
def run(command: Command, args: Namespace = Namespace()):
|
||||
command.init()
|
||||
|
||||
try:
|
||||
command.run()
|
||||
command.run(args)
|
||||
except GitFlowError as e:
|
||||
command.error(str(e))
|
||||
except Exception as e:
|
||||
@@ -83,8 +79,6 @@ def main():
|
||||
print()
|
||||
command.error("Ejecución abortada")
|
||||
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
def main():
|
||||
app()
|
||||
|
||||
Reference in New Issue
Block a user