78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
"""
|
|
Configuration management for CodiMD CLI.
|
|
"""
|
|
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
|
|
class Config:
|
|
"""Configuration manager for CodiMD CLI."""
|
|
|
|
DEFAULT_SERVER = "http://localhost:3000"
|
|
CONFIG_DIR = Path.home() / ".config" / "cli-anything-codimd"
|
|
|
|
def __init__(self):
|
|
self.config_dir = self.CONFIG_DIR
|
|
self.config_dir.mkdir(parents=True, exist_ok=True)
|
|
self.config_file = self.config_dir / "config.json"
|
|
self.session_file = self.config_dir / "session.json"
|
|
self.cache_file = self.config_dir / "cache.json"
|
|
|
|
self._config = self._load_config()
|
|
|
|
def _load_config(self) -> dict:
|
|
"""Load configuration from file or create default."""
|
|
if self.config_file.exists():
|
|
with open(self.config_file, "r") as f:
|
|
return json.load(f)
|
|
|
|
default_config = {
|
|
"server": os.environ.get("CODIMD_SERVER", self.DEFAULT_SERVER),
|
|
"timeout": 30,
|
|
"verify_ssl": True,
|
|
}
|
|
self.save_config(default_config)
|
|
return default_config
|
|
|
|
def save_config(self, config: dict):
|
|
"""Save configuration to file."""
|
|
with open(self.config_file, "w") as f:
|
|
json.dump(config, f, indent=2)
|
|
self._config = config
|
|
|
|
@property
|
|
def server(self) -> str:
|
|
"""Get configured server URL."""
|
|
return self._config.get("server", self.DEFAULT_SERVER)
|
|
|
|
@server.setter
|
|
def server(self, value: str):
|
|
"""Set server URL."""
|
|
self._config["server"] = value
|
|
self.save_config(self._config)
|
|
|
|
@property
|
|
def timeout(self) -> int:
|
|
"""Get request timeout."""
|
|
return self._config.get("timeout", 30)
|
|
|
|
@timeout.setter
|
|
def timeout(self, value: int):
|
|
"""Set request timeout."""
|
|
self._config["timeout"] = value
|
|
self.save_config(self._config)
|
|
|
|
@property
|
|
def verify_ssl(self) -> bool:
|
|
"""Get SSL verification setting."""
|
|
return self._config.get("verify_ssl", True)
|
|
|
|
@verify_ssl.setter
|
|
def verify_ssl(self, value: bool):
|
|
"""Set SSL verification setting."""
|
|
self._config["verify_ssl"] = value
|
|
self.save_config(self._config)
|