Coverage for src/onepass_env/config.py: 0%

33 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-18 11:04 -0300

1"""Configuration management for 1pass-env.""" 

2 

3import os 

4from pathlib import Path 

5from typing import Dict, Optional 

6 

7from pydantic import BaseModel, Field 

8 

9from onepass_env.exceptions import ConfigurationError 

10 

11 

12class Config(BaseModel): 

13 """Configuration model for 1pass-env.""" 

14 

15 default_vault: Optional[str] = Field(default=None, description="Default 1Password vault") 

16 default_env_file: str = Field(default=".env", description="Default environment file") 

17 auto_sync: bool = Field(default=False, description="Automatically sync with 1Password") 

18 verbose: bool = Field(default=False, description="Enable verbose logging") 

19 

20 class Config: 

21 """Pydantic configuration.""" 

22 env_prefix = "ONEPASS_ENV_" 

23 

24 

25def load_config() -> Config: 

26 """Load configuration from environment variables and config file.""" 

27 config_file = Path.home() / ".config" / "1pass-env" / "config.json" 

28 

29 if config_file.exists(): 

30 try: 

31 import json 

32 with open(config_file) as f: 

33 file_config = json.load(f) 

34 return Config(**file_config) 

35 except Exception as e: 

36 raise ConfigurationError(f"Failed to load config file: {e}") 

37 

38 # Load from environment variables 

39 return Config() 

40 

41 

42def save_config(config: Config) -> None: 

43 """Save configuration to file.""" 

44 config_dir = Path.home() / ".config" / "1pass-env" 

45 config_dir.mkdir(parents=True, exist_ok=True) 

46 

47 config_file = config_dir / "config.json" 

48 

49 try: 

50 import json 

51 with open(config_file, "w") as f: 

52 json.dump(config.dict(), f, indent=2) 

53 except Exception as e: 

54 raise ConfigurationError(f"Failed to save config file: {e}")