feat(config): support MYTOOLKIT_HOME env var and migrate to ~/.mytoolkit

This commit is contained in:
Zhengshou Lai
2026-06-04 00:33:44 +08:00
parent 58a141416c
commit 953ea9231d
+11 -4
View File
@@ -1,10 +1,13 @@
"""Configuration management for bin."""
import json
import os
from pathlib import Path
CONFIG_PATH = Path.home() / ".config" / "mytoolkit" / "env.json"
_MYTOOLKIT_HOME = Path(os.environ.get("MYTOOLKIT_HOME", Path.home() / ".mytoolkit"))
CONFIG_PATH = _MYTOOLKIT_HOME / "env.json"
_LEGACY_PATH = Path(__file__).parent / "config.json"
_LEGACY_CONFIG_DIR = Path.home() / ".config" / "mytoolkit" / "env.json"
class Config:
@@ -16,10 +19,14 @@ class Config:
self._data = self._load()
def _migrate_legacy(self) -> None:
"""One-shot move of legacy bin/config.json into the user config dir."""
if _LEGACY_PATH.exists() and not self._config_path.exists():
self._config_path.parent.mkdir(parents=True, exist_ok=True)
"""One-shot move of legacy configs into the user config dir."""
if self._config_path.exists():
return
self._config_path.parent.mkdir(parents=True, exist_ok=True)
if _LEGACY_PATH.exists():
_LEGACY_PATH.rename(self._config_path)
elif _LEGACY_CONFIG_DIR.exists():
_LEGACY_CONFIG_DIR.rename(self._config_path)
def _load(self) -> dict:
if self._config_path.exists():