Skip to content

Commit

Permalink
Add _ClickHandler for logging with click.echo().
Browse files Browse the repository at this point in the history
  • Loading branch information
elibon99 committed Jan 19, 2024
1 parent e3b380a commit 2d7f835
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
34 changes: 16 additions & 18 deletions gitbark/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def add_rules(ctx):
),
)
logger.info("Commit rules added successfully")
click.echo("Commit rules configuration was committed successfully!")


@cli.command()
Expand All @@ -152,7 +151,6 @@ def add_modules(ctx):
)
checkout_or_orphan(project, branch)
logger.info("Bark modules added successfully")
click.echo("Bark modules configuration was committed successfully!")


@cli.command()
Expand All @@ -174,8 +172,7 @@ def protect(ctx):
),
)
checkout_or_orphan(project, branch)
logger.info(f"'{branch}' added to 'bark_rules' successfully")
click.echo("Bark modules configuration was committed successfully!")
logger.info(f"'{branch}' added to 'bark_rules'")


@cli.command()
Expand All @@ -198,7 +195,6 @@ def install(ctx):
try:
install_cmd(project)
logger.info("Hooks installed successfully")
click.echo("Installed GitBark successfully!")
except RuleViolation as e:
pp_violation(e)
sys.exit(1)
Expand Down Expand Up @@ -238,9 +234,7 @@ def ref_update(ctx, old, new, ref):
if os.path.exists(fail_head):
os.remove(fail_head)
# TODO: Need to enable logging through env variable
logger.info(
f"Reference update ({old}->{new}) on {ref} " "validated successfully"
)
logger.info(f"{ref} is valid")
except RuleViolation as e:
with open(fail_head, "w") as f:
f.write(head.hash.hex())
Expand Down Expand Up @@ -282,22 +276,19 @@ def verify(ctx, target, all, bootstrap):
try:
if all:
verify_all(project)
logger.info("All branches verified successfully")
click.echo("Repository is in valid state!")
logger.info("All references are valid")
else:
head, ref = project.repo.resolve(target)
if ref:
verify_ref(project, ref, head)
logger.info(f"'{ref}' verified successfully")
click.echo(f"{format_ref(ref)} is in a valid state!")
logger.info(f"{ref} is valid")
elif not bootstrap:
raise CliFail(
"verifying a single commit requires specifying a bootstrap with -b"
"Verifying a single commit requires specifying a bootstrap with -b"
)
else:
verify_commit(project, head, bootstrap)
logger.info(f"Commit {head.hash.hex()} verified successfully")
click.echo(f"Commit {head.hash.hex()} is in a valid state!")
logger.info(f"Commit {head.hash.hex()} is valid")
except RuleViolation as e:
# TODO: Error message here?
pp_violation(e)
Expand All @@ -317,13 +308,20 @@ def format(self, record):
return message


class _ClickHandler(logging.Handler):
def emit(self, record) -> None:
try:
click.echo(record.getMessage())
except Exception:
self.handleError(record)


def main():
handler = logging.StreamHandler()
handler.setLevel(logging.INFO)
handler = _ClickHandler()
formatter = _DefaultFormatter()
handler.setFormatter(formatter)
logging.getLogger().addHandler(handler)

logging.getLogger().setLevel(logging.INFO)
try:
_add_subcommands(cli)
cli(obj={})
Expand Down
7 changes: 5 additions & 2 deletions gitbark/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import pkg_resources
import os
import stat
import logging

logger = logging.getLogger(__name__)


def install(project: Project) -> None:
Expand All @@ -28,7 +31,7 @@ def install(project: Project) -> None:


def install_hooks(project: Project):
print("Installing hooks....")
logger.debug("Installing hooks...")
reference_transaction_data = pkg_resources.resource_string(
__name__, "hooks/reference_transaction"
)
Expand All @@ -40,7 +43,7 @@ def install_hooks(project: Project):
f.write(reference_transaction_data)
make_executable(reference_transaction_path)

print(f"Hooks installed in {hooks_path}")
logger.info(f"Hooks installed in {hooks_path}")


def make_executable(path: str):
Expand Down
10 changes: 1 addition & 9 deletions gitbark/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ def _validate_rules(commit: Commit, cache: Cache) -> None:
validators = _nearest_valid_ancestors(commit, cache)
if not validators:
raise RuleViolation("No valid ancestors")
logger.debug(
f"Validators for commit {commit.hash.hex()}: "
f"[{', '.join([v.hash.hex() for v in validators])}]"
)
if len(validators) > 1:
rule: CommitRule = AllCommitRule(
"all",
Expand All @@ -62,7 +58,7 @@ def _validate_rules(commit: Commit, cache: Cache) -> None:
)
else:
rule = _get_commit_rule(validators.pop(), cache)
logger.debug(f"Applying rules for commit {commit.hash.hex()}")
logger.debug(f"Validating rules for commit {commit.hash.hex()}")
rule.validate(commit)

# Also ensure that commit has valid rules
Expand All @@ -79,10 +75,6 @@ def _validate_commit(
on_valid: Callable[[Commit], None],
) -> None:
if not is_descendant(bootstrap, commit):
logger.error(
f"Commit {commit.hash.hex()} is not a descendant "
f"to bootstrap {bootstrap.hash.hex()}."
)
raise RuleViolation(f"Bootstrap '{bootstrap.hash.hex()}' is not an ancestor")

# Re-validate if previously invalid
Expand Down

0 comments on commit 2d7f835

Please sign in to comment.