73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
import os
|
|
import json
|
|
|
|
from typing import Optional
|
|
|
|
from rich.console import Console
|
|
from rich.prompt import Prompt
|
|
|
|
console = Console()
|
|
|
|
class Config:
|
|
def __init__(self, name: str, path: str = os.path.expanduser("~/.secnex")) -> None:
|
|
self.name = name
|
|
self.path = path
|
|
self.config = self.__load_config()
|
|
if self.config is None:
|
|
host = Prompt.ask("Enter the host of the SecNex API", default="http://localhost:3003")
|
|
ui = Prompt.ask("Enter the UI to use", default="http://localhost:3000")
|
|
client_id = Prompt.ask("Enter the ID of your client application")
|
|
client_secret = Prompt.ask("Enter the secret of your client application")
|
|
tenant_id = Prompt.ask("Enter the ID of your tenant")
|
|
self.config = {
|
|
"host": host,
|
|
"ui": ui,
|
|
"client": {
|
|
"id": client_id,
|
|
"secret": client_secret,
|
|
"tenant": tenant_id,
|
|
}
|
|
}
|
|
self.__save_config()
|
|
|
|
@property
|
|
def host(self) -> Optional[str]:
|
|
if self.config is not None:
|
|
return self.config["host"]
|
|
return None
|
|
|
|
@property
|
|
def ui(self) -> Optional[str]:
|
|
if self.config is not None:
|
|
return self.config["ui"]
|
|
return None
|
|
|
|
@property
|
|
def client_id(self) -> Optional[str]:
|
|
if self.config is not None:
|
|
return self.config["client"]["id"]
|
|
return None
|
|
|
|
@property
|
|
def client_secret(self) -> Optional[str]:
|
|
if self.config is not None:
|
|
return self.config["client"]["secret"]
|
|
return None
|
|
|
|
@property
|
|
def tenant(self) -> Optional[str]:
|
|
if self.config is not None:
|
|
return self.config["client"]["tenant"]
|
|
return None
|
|
|
|
def __load_config(self) -> dict | None:
|
|
path = os.path.join(self.path, f"{self.name}.json")
|
|
if not os.path.exists(path):
|
|
return None
|
|
with open(path, "r") as f:
|
|
return json.load(f)
|
|
|
|
def __save_config(self) -> None:
|
|
path = os.path.join(self.path, f"{self.name}.json")
|
|
with open(path, "w") as f:
|
|
json.dump(self.config, f) |