Skip to content

Commit

Permalink
Merge pull request #34 from limaa/feat/set-array-element
Browse files Browse the repository at this point in the history
feat: add support for setting array element
  • Loading branch information
mrijken authored Jun 26, 2024
2 parents f21f48b + 2d551c5 commit 8435a80
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ continuous development steps.
## Set a value

`toml set --toml-path pyproject.toml tool.poetry.version 0.2.0`
`toml set --toml-path pyproject.toml tool.poetry.authors[0] "Marc Rijken <[email protected]>"`

When the index exists, the item is changed. Otherwise, the item will be added to the list.

## Add a section

Expand Down
16 changes: 16 additions & 0 deletions tests/toml_cli/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def test_set_value(tmp_path: pathlib.Path):
name = "MyName"
happy = false
age = 12
skills = ["python", "pip"]
[person.education]
name = "University"
Expand Down Expand Up @@ -140,6 +141,21 @@ def test_set_value(tmp_path: pathlib.Path):
assert result.exit_code == 0
assert 'addresses = ["Amsterdam", "London"]' in test_toml_path.read_text()

result = runner.invoke(app, ["set", "--toml-path", str(test_toml_path), "person.KEY_THAT_DOES_NOT_EXIST[0]", "git"])
assert result.exit_code == 1

result = runner.invoke(app, ["set", "--toml-path", str(test_toml_path), "person.skills[1]", "toml"])
assert result.exit_code == 0
assert 'skills = ["python", "toml"]' in test_toml_path.read_text()

result = runner.invoke(app, ["set", "--toml-path", str(test_toml_path), "person.skills[2]", "git"])
assert result.exit_code == 0
assert 'skills = ["python", "toml", "git"]' in test_toml_path.read_text()

result = runner.invoke(app, ["set", "--toml-path", str(test_toml_path), "person.skills[1337]", "h4ck3rm4n"])
assert result.exit_code == 0
assert 'skills = ["python", "toml", "git", "h4ck3rm4n"]' in test_toml_path.read_text()


def test_add_section(tmp_path: pathlib.Path):
test_toml_path = tmp_path / "test.toml"
Expand Down
18 changes: 17 additions & 1 deletion toml_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,23 @@ def set_(
if to_array:
value = json.loads(value)

toml_part[key.split(".")[-1]] = value
last_key = key.split(".")[-1] # 'key' may access an array with index, example: tool.poetry.authors[0]
match = re.search(r"(?P<array>.*?)\[(?P<index>\d+)\]", last_key)
if match:
array = match.group("array")
try:
toml_part = toml_part[array]
except tomlkit.exceptions.NonExistentKey:
typer.echo(f"error: non-existent array '{array}'", err=True)
exit(1)

index = int(match.group("index"))
if len(toml_part) <= index:
toml_part.insert(index, value)
else:
toml_part[index] = value
else:
toml_part[last_key] = value

toml_path.write_text(tomlkit.dumps(toml_file))

Expand Down

0 comments on commit 8435a80

Please sign in to comment.