Skip to content

Commit 9c9f160

Browse files
mrutunjay-kinagiMrutunjay Kinagi
andauthored
Add --prefix option for REST catalog (#3061)
<!-- Closes #3059 --> # Rationale for this change The CLI already supports `--uri` and `--credential`, but REST catalogs that require `{prefix}` could only be configured via environment/config file. This change adds a first-class `--prefix` CLI option and forwards it to `load_catalog`. ## Are these changes tested? Yes. - Added `test_prefix_cli_option_forwarded_to_catalog` in `tests/cli/test_console.py`. - Verified with targeted pytest run. ## Are there any user-facing changes? Yes. - New CLI option: `--prefix`. - Updated CLI docs in `mkdocs/docs/cli.md`. Co-authored-by: Mrutunjay Kinagi <you@example.com>
1 parent 9687d08 commit 9c9f160

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

mkdocs/docs/cli.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ hide:
2626

2727
Pyiceberg comes with a CLI that's available after installing the `pyiceberg` package.
2828

29-
You can pass the path to the Catalog using the `--uri` and `--credential` argument, but it is recommended to setup a `~/.pyiceberg.yaml` config as described in the [Catalog](configuration.md) section.
29+
You can pass the path to the Catalog using `--uri` and `--credential`. For REST catalogs that require a path prefix, you can also set `--prefix`. It is still recommended to set up a `~/.pyiceberg.yaml` config as described in the [Catalog](configuration.md) section.
3030

3131
```sh
3232
➜ pyiceberg --help
@@ -39,6 +39,7 @@ Options:
3939
--ugi TEXT
4040
--uri TEXT
4141
--credential TEXT
42+
--prefix TEXT
4243
--help Show this message and exit.
4344

4445
Commands:

pyiceberg/cli/console.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def wrapper(*args: Any, **kwargs: Any): # type: ignore
6666
@click.option("--ugi")
6767
@click.option("--uri")
6868
@click.option("--credential")
69+
@click.option("--prefix")
6970
@click.pass_context
7071
def run(
7172
ctx: Context,
@@ -76,6 +77,7 @@ def run(
7677
ugi: str | None,
7778
uri: str | None,
7879
credential: str | None,
80+
prefix: str | None,
7981
) -> None:
8082
logging.basicConfig(
8183
level=getattr(logging, log_level.upper()),
@@ -89,6 +91,8 @@ def run(
8991
properties[URI] = uri
9092
if credential:
9193
properties["credential"] = credential
94+
if prefix:
95+
properties["prefix"] = prefix
9296

9397
ctx.ensure_object(dict)
9498
if output == "text":

tests/cli/test_console.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,3 +1029,18 @@ def test_log_level_cli_overrides_env(mocker: MockFixture) -> None:
10291029
mock_basicConfig.assert_called_once()
10301030
call_kwargs = mock_basicConfig.call_args[1]
10311031
assert call_kwargs["level"] == logging.ERROR
1032+
1033+
1034+
def test_prefix_cli_option_forwarded_to_catalog(mocker: MockFixture) -> None:
1035+
mock_basicConfig = mocker.patch("logging.basicConfig")
1036+
mock_catalog = MagicMock(spec=InMemoryCatalog)
1037+
mock_catalog.list_tables.return_value = []
1038+
mock_catalog.list_namespaces.return_value = []
1039+
mock_load_catalog = mocker.patch("pyiceberg.cli.console.load_catalog", return_value=mock_catalog)
1040+
1041+
runner = CliRunner()
1042+
result = runner.invoke(run, ["--catalog", "rest", "--uri", "https://example.invalid", "--prefix", "v1/ws", "list"])
1043+
1044+
assert result.exit_code == 0
1045+
mock_basicConfig.assert_called_once()
1046+
mock_load_catalog.assert_called_once_with("rest", uri="https://example.invalid", prefix="v1/ws")

0 commit comments

Comments
 (0)