Skip to content

Commit

Permalink
introduce repo argument
Browse files Browse the repository at this point in the history
Fixes: #4
  • Loading branch information
rarescosma committed Oct 15, 2024
1 parent d917c73 commit 8d2220e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
57 changes: 40 additions & 17 deletions octotail/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,83 @@
from functools import wraps
from typing import Annotated, Callable

import typer
from typer import Argument, BadParameter, Option, Typer

_REPO_HELP = "\n".join(
[
"Use this GitHub repo to look for workflow runs.",
"If unspecified, will look for a remote matching 'git@github.com:<user>/<repo>.git'",
"in the current directory.",
"\nExamples: 'user/repo' OR 'org_name/repo'",
]
)


def _sha_callback(value: str) -> str:
if len(value) != 40:
raise typer.BadParameter("need a full 40 character long commit sha")
raise BadParameter("need a full 40 character long commit sha")
if value == 40 * "0":
raise typer.BadParameter("refusing to work with the all-zero commit sha")
raise BadParameter("refusing to work with the all-zero commit sha")
return value


@dataclass(frozen=True)
class Opts:
"""
Look for an active workflow run for the given <COMMIT_SHA> (and optionally
--workflow-name and/or --ref-name) and attempt to tail its logs.
--workflow and/or --ref-name) and attempt to tail its logs.
NOTE: the <COMMIT_SHA> has to be of the full 40 characters length.
"""

commit_sha: Annotated[
str,
typer.Argument(callback=_sha_callback, help="Full commit SHA that triggered the workflow."),
Argument(callback=_sha_callback, help="Full commit SHA that triggered the workflow."),
]
gh_pat: Annotated[
str,
typer.Option(envvar="_GH_PAT", help="GitHub personal access token.", show_default=False),
Option(
envvar="_GH_PAT",
help="GitHub personal access token. (for API auth)",
show_default=False,
),
]
gh_user: Annotated[
str,
typer.Option(envvar="_GH_USER", help="GitHub username. (for web auth)", show_default=False),
Option(envvar="_GH_USER", help="GitHub username. (for web auth)", show_default=False),
]
gh_pass: Annotated[
str,
typer.Option(envvar="_GH_PASS", help="GitHub password. (for web auth)", show_default=False),
Option(envvar="_GH_PASS", help="GitHub password. (for web auth)", show_default=False),
]
gh_otp: Annotated[
str | None, typer.Option(envvar="_GH_OTP", help="GitHub OTP. (for web auth)")
] = None
gh_otp: Annotated[str | None, Option(envvar="_GH_OTP", help="GitHub OTP. (for web auth)")] = (
None
)
workflow_name: Annotated[
str | None,
typer.Option("-w", "--workflow", help="Look for workflows with this particular name."),
Option(
"-w",
"--workflow",
help="Only consider workflows with this name.",
show_default=False,
),
] = None
ref_name: Annotated[
str | None,
typer.Option(
Option(
"-r",
"--ref-name",
help="Look for workflows triggered by this ref.\n\nExample: 'refs/heads/main'",
help="Only consider workflows triggered by this ref.\n\nExample: 'refs/heads/main'",
show_default=False,
),
] = None
headless: Annotated[bool, typer.Option(envvar="_HEADLESS")] = True
repo: Annotated[
str | None,
Option("-R", "--repo", help=_REPO_HELP, show_default=False),
] = None
headless: Annotated[bool, Option(envvar="_HEADLESS")] = True
port: Annotated[
int | None, typer.Option(envvar="_PORT", show_default="random in range 8100-8500")
int | None, Option(envvar="_PORT", show_default="random in range 8100-8500")
] = None

def __post_init__(self) -> None:
Expand All @@ -79,7 +102,7 @@ def wrapped(opts: Opts) -> None:

@wraps(main_fn)
def wrapper() -> None:
app = typer.Typer(add_completion=False, rich_markup_mode="rich")
app = Typer(add_completion=False, rich_markup_mode="rich")
app.command(no_args_is_help=True)(Opts)
_post_init.set(wrapped)
app()
Expand Down
2 changes: 1 addition & 1 deletion octotail/gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def guess_repo() -> str | None:
return None
repos = list(filter(None, map(_guess_repo, remotes)))
if len(repos) > 1:
log("fatal: found multiple remotes with github URLs")
log("fatal: found multiple remotes pointing to GitHub")
return None
return repos[0] if repos else None

Expand Down
3 changes: 2 additions & 1 deletion octotail/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def _replace_streamer(self, job_id: int, streamer: mp.Process) -> None:
def _main(opts: Opts) -> None:
_stop = Event()

if (repo_id := guess_repo()) is None:
if (repo_id := (opts.repo or guess_repo())) is None:
log("could not establish repo")
sys.exit(1)

gh_client = Github(auth=Auth.Token(opts.gh_pat))
Expand Down

0 comments on commit 8d2220e

Please sign in to comment.