Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions hoard/core/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import copy
import os
import tempfile
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict
Expand Down Expand Up @@ -250,14 +252,27 @@ def ensure_config_file(path: Path | None = None) -> Path:
config_path = path or get_default_config_path()
config_path.parent.mkdir(parents=True, exist_ok=True)
if not config_path.exists():
config_path.write_text(yaml.safe_dump(copy.deepcopy(DEFAULT_CONFIG), sort_keys=False))
save_config(copy.deepcopy(DEFAULT_CONFIG), config_path)
return config_path


def save_config(config: Dict[str, Any], path: Path | None = None) -> Path:
config_path = path or get_default_config_path()
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(yaml.safe_dump(config, sort_keys=False))
data = yaml.safe_dump(config, sort_keys=False)
fd, tmp = tempfile.mkstemp(dir=config_path.parent, suffix=".tmp")
try:
with os.fdopen(fd, "w") as f:
f.write(data)
f.flush()
os.fsync(f.fileno())
os.replace(tmp, config_path)
except BaseException:
try:
os.unlink(tmp)
except OSError:
pass
raise
return config_path


Expand Down