diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b9a2df..bb5c4af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## \[Unreleased\] +## \[0.8.0\] + +### Added + +- Search with jmspath (@tiberium) +- Add validation whether path exists + +## \[0.7.0\] ### Added diff --git a/pyproject.toml b/pyproject.toml index 0df36e7..725b6a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "toml-cli" -version = "0.7.0" +version = "0.8.0" description = "Command line interface to read and write keys/values to/from toml files" requires-python = ">=3.10" dependencies = [ @@ -14,11 +14,10 @@ scripts = { toml = 'toml_cli:main'} [tool.ruff] line-length = 120 -target-version = "py38" +target-version = "py310" [tool.ruff.lint.isort] force-single-line = true -known-first-party = ["pytest_nlcov"] [tool.hatch.build.targets.wheel] packages = ["toml_cli"] diff --git a/tests/toml_cli/test_init.py b/tests/toml_cli/test_init.py index 10240cd..d36c34e 100644 --- a/tests/toml_cli/test_init.py +++ b/tests/toml_cli/test_init.py @@ -7,19 +7,6 @@ runner = CliRunner() -# def normalized(item): -# if isinstance(item, dict): -# return sorted((key, normalized(values)) for key, values in item.items()) -# if isinstance(item, list): -# return sorted(normalized(x) for x in item) -# else: -# return item - - -# def compare(item1, item2): -# assert normalized(eval(item1)) == normalized(item2) - - def test_get_value(tmp_path: pathlib.Path): test_toml_path = tmp_path / "test.toml" test_toml_path.write_text( diff --git a/toml_cli/__init__.py b/toml_cli/__init__.py index d3bb41c..256ba68 100644 --- a/toml_cli/__init__.py +++ b/toml_cli/__init__.py @@ -13,10 +13,19 @@ app = typer.Typer(no_args_is_help=True) +DEFAULT_TOML_PATH = pathlib.Path("config.toml") + + +def path_callback(value: pathlib.Path): + if not value.exists(): + raise typer.BadParameter(f"{value} does not exists") + return value + + @app.command("get") def get( key: Optional[str] = typer.Argument(None), - toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml")), + toml_path: pathlib.Path = typer.Option(DEFAULT_TOML_PATH, callback=path_callback), default: Optional[str] = typer.Option(None), ): """Get a value from a toml file.""" @@ -63,7 +72,7 @@ def get( def set_( key: str, value: str, - toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml")), + toml_path: pathlib.Path = typer.Option(DEFAULT_TOML_PATH, callback=path_callback), to_int: bool = typer.Option(False), to_float: bool = typer.Option(False), to_bool: bool = typer.Option(False), @@ -125,7 +134,7 @@ def set_( @app.command("add_section") def add_section( key: str, - toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml")), + toml_path: pathlib.Path = typer.Option(DEFAULT_TOML_PATH, callback=path_callback), ): """Add a section with the given key.""" toml_part = toml_file = tomlkit.parse(toml_path.read_text()) @@ -139,7 +148,10 @@ def add_section( @app.command("unset") -def unset(key: str, toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml"))): +def unset( + key: str, + toml_path: pathlib.Path = typer.Option(DEFAULT_TOML_PATH, callback=path_callback), +): """Unset a value from a toml file.""" toml_part = toml_file = tomlkit.parse(toml_path.read_text()) @@ -157,7 +169,7 @@ def unset(key: str, toml_path: pathlib.Path = typer.Option(pathlib.Path("config. @app.command("search") def search( jmespath_expression: str, - toml_path: pathlib.Path = typer.Option(pathlib.Path("config.toml")), + toml_path: pathlib.Path = typer.Option(DEFAULT_TOML_PATH, callback=path_callback), ): """Search for a value in a toml file using JMESPath query.""" toml_data = tomlkit.parse(toml_path.read_text())