This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
76 lines (53 loc) · 2.42 KB
/
config.py
File metadata and controls
76 lines (53 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from pydantic import BaseModel
from rich import print
from ruamel.yaml import YAML
from dreadnode_cli.defaults import DEFAULT_PROFILE_NAME, USER_CONFIG_PATH
class ServerConfig(BaseModel):
"""Server specific authentication data and API URL."""
url: str
email: str
username: str
api_key: str
access_token: str
refresh_token: str
class UserConfig(BaseModel):
"""User configuration supporting multiple server profiles."""
active: str | None = None
servers: dict[str, ServerConfig] = {}
def _update_active(self) -> None:
"""If active is not set, set it to the first available server and raise an error if no servers are configured."""
if self.active not in self.servers:
self.active = next(iter(self.servers)) if self.servers else None
@classmethod
def read(cls) -> "UserConfig":
"""Read the user configuration from the file system or return an empty instance."""
if not USER_CONFIG_PATH.exists():
return cls()
with USER_CONFIG_PATH.open("r") as f:
return cls.model_validate(YAML().load(f))
def write(self) -> None:
"""Write the user configuration to the file system."""
self._update_active()
if not USER_CONFIG_PATH.parent.exists():
print(f":rocket: Creating config at {USER_CONFIG_PATH.parent}")
USER_CONFIG_PATH.parent.mkdir(parents=True)
with USER_CONFIG_PATH.open("w") as f:
YAML().dump(self.model_dump(mode="json"), f)
@property
def active_profile_name(self) -> str | None:
"""Get the name of the active profile."""
self._update_active()
return self.active
def get_server_config(self, profile: str | None = None) -> ServerConfig:
"""Get the server configuration for the given profile or None if not set."""
profile = profile or self.active
if not profile:
raise Exception("No profile is set, use [bold]dreadnode login[/] to authenticate")
if profile not in self.servers:
raise Exception(f"No server configuration for profile: {profile}")
return self.servers[profile]
def set_server_config(self, config: ServerConfig, profile: str | None = None) -> "UserConfig":
"""Set the server configuration for the given profile."""
profile = profile or self.active or DEFAULT_PROFILE_NAME
self.servers[profile] = config
return self