Read a project's credentials from the env file its config names, and keep every value out of messages and logs
CI / tests (pull_request) Successful in 2m21s
CI / tests (pull_request) Successful in 2m21s
This commit is contained in:
+17
-9
@@ -9,6 +9,7 @@ import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
from failure import Failure
|
||||
from project_config.main import located
|
||||
|
||||
KEYS = ("MATTERMOST_URL", "MATTERMOST_TOKEN", "MATTERMOST_RECIPIENT")
|
||||
# Seconds each connect and each read may wait, so an unreachable server holds loop-claude's interrupt only briefly.
|
||||
@@ -35,10 +36,6 @@ class Config:
|
||||
recipient: str
|
||||
|
||||
|
||||
def env_file(home: pathlib.Path) -> pathlib.Path:
|
||||
return home / ".claude" / ".env"
|
||||
|
||||
|
||||
def read_config(path: pathlib.Path) -> Config | Failure:
|
||||
"""The server, bot token and recipient that path's KEY=value lines hold."""
|
||||
try:
|
||||
@@ -126,10 +123,21 @@ def error_message(failure: urllib.error.HTTPError) -> str:
|
||||
return message if isinstance(message, str) else ""
|
||||
|
||||
|
||||
def dm(text: str, home: pathlib.Path, http: Http) -> Failure | None:
|
||||
"""Sends text as home's .claude/.env configures it. A machine without that file hasn't asked for DMs, so there the DM is skipped."""
|
||||
path = env_file(home)
|
||||
if not path.exists():
|
||||
def dm(text: str, directory: str | None, home: pathlib.Path, http: Http) -> Failure | None:
|
||||
"""Sends text as the env file that the config of the project holding directory names configures it. A project whose config names none, or has none, hasn't asked for DMs, so there the DM is skipped."""
|
||||
project = located(directory, home)
|
||||
# A config that cannot be read has already stopped loop-claude with its own reason, so it needs no second notice here.
|
||||
if isinstance(project, Failure) or project.env_file is None:
|
||||
return None
|
||||
config = read_config(path)
|
||||
config = read_config(project.env_file)
|
||||
return config if isinstance(config, Failure) else send(config, text, http)
|
||||
|
||||
|
||||
def config_of(directory: str | None, home: pathlib.Path) -> Config | Failure:
|
||||
"""The server, token and recipient in the env file that the config of the project holding directory names."""
|
||||
project = located(directory, home)
|
||||
if isinstance(project, Failure):
|
||||
return project
|
||||
if project.env_file is None:
|
||||
return Failure(f"{project.folder / 'config.toml'} sets no `env-file`; it needs the file that holds {', '.join(KEYS[:-1])} and {KEYS[-1]}")
|
||||
return read_config(project.env_file)
|
||||
|
||||
Reference in New Issue
Block a user