From 73ca8b99288a9475256daae149c4768cc31f7b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rare=C8=99=20Cosma?= Date: Tue, 15 Oct 2024 11:10:17 +0300 Subject: [PATCH] validate repo argument --- octotail/cli.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/octotail/cli.py b/octotail/cli.py index 229541f..3745c05 100644 --- a/octotail/cli.py +++ b/octotail/cli.py @@ -1,5 +1,6 @@ """ Options class + some magic so we can define our args in one place only. """ +import re from contextvars import ContextVar from dataclasses import dataclass from functools import wraps @@ -25,6 +26,13 @@ def _sha_callback(value: str) -> str: return value +def _repo_callback(value: str | None) -> str | None: + pattern = r"^[a-zA-Z0-9_-]{1,100}/[a-zA-Z0-9_-]{1,100}$" + if value is not None and re.match(pattern, value) is None: + raise BadParameter(f"invalid format for repo: {value}") + return value + + @dataclass(frozen=True) class Opts: """ @@ -77,7 +85,13 @@ class Opts: ] = None repo: Annotated[ str | None, - Option("-R", "--repo", help=_REPO_HELP, show_default=False), + Option( + "-R", + "--repo", + help=_REPO_HELP, + show_default=False, + callback=_repo_callback, + ), ] = None headless: Annotated[bool, Option(envvar="_HEADLESS")] = True port: Annotated[