refactor: replace global command class to typer functions

This commit is contained in:
jt
2026-05-16 18:59:45 -03:00
parent e87571af8b
commit fec30cd1e8
6 changed files with 66 additions and 103 deletions
+9 -41
View File
@@ -1,6 +1,8 @@
from argparse import Namespace, ArgumentParser
from abc import ABC, abstractmethod
from os.path import isfile
import rich
from rich.prompt import Prompt, Confirm
from git_flow import (
BRANCH_TYPES,
@@ -44,16 +46,16 @@ class Command(ABC):
)
def success(self, msg: str):
self._print(TYPE_SUCCESS, msg)
rich.print(f":white_check_mark: [green]{msg}[/green]")
def warning(self, msg: str):
self._print(TYPE_WARNING, msg)
rich.print(f":warning: [yellow]{msg}[/yellow]")
def error(self, msg: str):
self._print(TYPE_ERROR, msg)
rich.print(f":x: [red]{msg}[/red]")
def info(self, msg: str):
self._print(TYPE_INFO, msg)
rich.print(f":information: [blue]{msg}[/blue]")
def prompt(
self,
@@ -65,11 +67,7 @@ class Command(ABC):
onetime = not persistent
while onetime or persistent:
if default:
prompt += f" [{default}]"
prompt += ": "
result = input(prompt)
result = Prompt.ask(prompt)
result = result.strip() if strip else result
if result:
@@ -82,40 +80,10 @@ class Command(ABC):
return result
def confirm(self, question: str, default: bool = True):
suffix = " [Y/n]: " if default else " [y/N]: "
answer = input(question + suffix)
return default if len(answer) == 0 else answer.startswith("y")
return Confirm.ask(question, default=default)
def choice(self, prompt: str, options: list[str]):
print(prompt)
for i, option in enumerate(options):
print(f"\t{i+1}. {option}")
selection = None
while selection is None:
answer = input(f"Seleccione una opción [1-{len(options)}] o escribala: ")
if answer.isdigit():
answer = int(answer)
if 1 <= answer and answer <= len(options):
selection = options[answer - 1]
if not self.confirm(
f"Seleccionó la opción {answer} ({selection}), ¿es correcto?"
):
selection = None
else:
self.error(f"La opción {answer} está fuera del rango permitido.")
elif answer in options:
selection = answer
else:
self.error(f"La opción '{answer}' es inválida.")
return selection
return Prompt.ask(prompt, choices = options)
def setup_parser(self, parser: ArgumentParser) -> ArgumentParser:
return parser
+50 -56
View File
@@ -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()