Skip to content

Commit 15264c8

Browse files
committed
refactor: Centralize save_config_yaml function (Phase 3)
- Add save_config_yaml to vcspull.config module using ConfigReader._dump - Remove duplicate implementations from cli/add.py and cli/add_from_fs.py - Update imports to use the centralized function - Remove duplicate from test helpers - Follows DRY principle and provides consistent YAML formatting
1 parent df9027e commit 15264c8

File tree

5 files changed

+22
-24
lines changed

5 files changed

+22
-24
lines changed

src/vcspull/cli/add.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from vcspull.config import (
1212
expand_dir,
1313
find_home_config_files,
14+
save_config_yaml,
1415
)
1516

1617
if t.TYPE_CHECKING:
@@ -251,17 +252,7 @@ def add_repo(
251252
raw_config[actual_base_dir_key][repo_name] = repo_url
252253

253254
try:
254-
# save_config_yaml(config_file_path, raw_config) # Original call to local/helper
255-
# The main application code should use a robust save mechanism.
256-
# For now, directly writing, similar to the old save_config_yaml.
257-
with config_file_path.open("w", encoding="utf-8") as f:
258-
yaml.dump(
259-
raw_config,
260-
f,
261-
sort_keys=False,
262-
indent=2,
263-
default_flow_style=False,
264-
)
255+
save_config_yaml(config_file_path, raw_config)
265256
log.info(
266257
f"Successfully added '{repo_name}' ({repo_url}) to {config_file_path} "
267258
f"under '{actual_base_dir_key}'.",

src/vcspull/cli/add_from_fs.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from vcspull.config import ( # Assuming these are useful
1414
expand_dir,
1515
find_home_config_files,
16+
save_config_yaml,
1617
)
1718

1819
if t.TYPE_CHECKING:
@@ -38,12 +39,6 @@ def get_git_origin_url(repo_path: pathlib.Path) -> str | None:
3839
return None
3940

4041

41-
def save_config_yaml(config_file_path: pathlib.Path, data: dict[t.Any, t.Any]) -> None:
42-
"""Save a dictionary to a YAML file."""
43-
with config_file_path.open("w", encoding="utf-8") as f:
44-
yaml.dump(data, f, sort_keys=False, indent=2, default_flow_style=False)
45-
46-
4742
def create_add_from_fs_subparser(parser: argparse.ArgumentParser) -> None:
4843
"""Configure :py:class:`argparse.ArgumentParser` for ``vcspull add-from-fs``."""
4944
parser.add_argument(

src/vcspull/config.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,21 @@ def is_config_file(
424424
extensions = [".yml", ".yaml", ".json"]
425425
extensions = [extensions] if isinstance(extensions, str) else extensions
426426
return any(filename.endswith(e) for e in extensions)
427+
428+
429+
def save_config_yaml(config_file_path: pathlib.Path, data: dict[t.Any, t.Any]) -> None:
430+
"""Save configuration data to a YAML file.
431+
432+
Parameters
433+
----------
434+
config_file_path : pathlib.Path
435+
Path to the configuration file to write
436+
data : dict
437+
Configuration data to save
438+
"""
439+
yaml_content = ConfigReader._dump(
440+
fmt="yaml",
441+
content=data,
442+
indent=2,
443+
)
444+
config_file_path.write_text(yaml_content, encoding="utf-8")

tests/cli/test_add_from_fs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import pytest
1111
import yaml
1212

13-
from tests.helpers import save_config_yaml
1413
from vcspull.cli.add_from_fs import add_from_filesystem, get_git_origin_url
14+
from vcspull.config import save_config_yaml
1515

1616
if t.TYPE_CHECKING:
1717
from _pytest.logging import LogCaptureFixture

tests/helpers.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,3 @@ def load_raw(data: str, fmt: t.Literal["yaml", "json"]) -> dict[str, t.Any]:
6363
return ConfigReader._load(fmt=fmt, content=data)
6464

6565

66-
def save_config_yaml(config_file_path: pathlib.Path, data: dict[t.Any, t.Any]) -> None:
67-
"""Save a dictionary to a YAML file."""
68-
import yaml
69-
70-
yaml_content = yaml.dump(data, sort_keys=False, indent=2, default_flow_style=False)
71-
write_config(config_file_path, yaml_content)

0 commit comments

Comments
 (0)