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:
@@ -21,6 +21,10 @@ starts with / or ~/. In a worktree, a "checkout:" path is the worktree's own fil
|
||||
goals-and-audience = "folder:goals.md"
|
||||
principles = "folder:principles"
|
||||
|
||||
`env-file` is the one optional setting: the file of KEY=value credentials the project's tools read,
|
||||
a path that starts with / or ~/ and lies outside the checkout and the config's folder, such as
|
||||
"~/.claude-projects/acme-billing.env". Where it is left out, the project has asked for none.
|
||||
|
||||
Exit 1, naming each fault on stderr, where no config names the checkout, or its config leaves out a
|
||||
setting, names an unknown one or misspells a path. No setting has a default.
|
||||
"""
|
||||
|
||||
@@ -11,7 +11,10 @@ PROJECTS = ".claude-projects"
|
||||
CONFIG = "config.toml"
|
||||
# Every path a project sets, by its role. Three readers, seated as the human who writes the config, picked the spelling from four candidates. They picked role names over file names, which a client's project doesn't share.
|
||||
SETTINGS = ("plan", "working-rules", "decisions", "score-anchors", "comprehension-history", "reading-cost", "changelog", "goals-and-audience", "principles")
|
||||
# The one setting a project may leave out: the file holding the credentials its tools read. It has no default either, so that a project without it asks for none, and never reads another project's.
|
||||
ENV_FILE = "env-file"
|
||||
# They also picked a prefix over bare relative paths: each line says its own place, and a relative path with no prefix fails instead of landing somewhere by default.
|
||||
OUTSIDE = "a path that starts with / or ~/ and lies outside the checkout and the config's folder, so that no git remote receives the credentials"
|
||||
SPELLING = '"checkout:<path>" for a path in the checkout, "folder:<path>" for one in the config\'s folder, or a path that starts with / or ~/'
|
||||
|
||||
# Where a project keeps a file, for a tool that reads it at a commit: a path from the checkout's top, as git names it; or, outside the checkout, the file itself, which the tool reads as it stands, since the checkout's commits hold no version of it.
|
||||
@@ -25,6 +28,8 @@ class ProjectConfig:
|
||||
folder: pathlib.Path
|
||||
top: pathlib.Path
|
||||
paths: dict[str, pathlib.Path]
|
||||
# The file of KEY=value credentials the project's tools read, or None where the config sets none. It lies outside the checkout and the config's folder, whose git repos can reach a remote.
|
||||
env_file: pathlib.Path | None = None
|
||||
|
||||
def tracked(self, setting: str) -> str | None:
|
||||
"""setting's path from top, as git names a file in a commit; None where the config puts it outside the checkout, whose commits hold no version of it."""
|
||||
@@ -54,14 +59,22 @@ def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectCo
|
||||
return Failure(f"{' and '.join(str(file) for file, _ in named)} each name the checkout {main}; only one config may")
|
||||
file, values = named[0]
|
||||
paths = {name: placed(values[name], top, file.parent, home) for name in SETTINGS if name in values}
|
||||
env_file = anywhere(values.get(ENV_FILE), home)
|
||||
misplaced = ENV_FILE in values and (env_file is None or inside(env_file, top, file.parent))
|
||||
faults = [
|
||||
*(f"`{name}` is missing" for name in SETTINGS if name not in values),
|
||||
*(f"`{name}` is not a setting" for name in values if name not in SETTINGS),
|
||||
*(f"`{name}` is not a setting" for name in values if name not in (*SETTINGS, ENV_FILE)),
|
||||
*(f"`{name}` is {values[name]!r}, not {SPELLING}" for name, path in paths.items() if path is None),
|
||||
*([f"`{ENV_FILE}` is {values[ENV_FILE]!r}, not {OUTSIDE}"] if misplaced else []),
|
||||
]
|
||||
if faults:
|
||||
return Failure(f"{file}: {'; '.join(faults)}. The settings are {', '.join(SETTINGS)}.")
|
||||
return ProjectConfig(file.parent.name, file.parent, top, {name: path for name, path in paths.items() if path})
|
||||
return Failure(f"{file}: {'; '.join(faults)}. The settings are {', '.join(SETTINGS)}, and optionally {ENV_FILE}.")
|
||||
return ProjectConfig(file.parent.name, file.parent, top, {name: path for name, path in paths.items() if path}, env_file)
|
||||
|
||||
|
||||
def inside(path: pathlib.Path, top: pathlib.Path, folder: pathlib.Path) -> bool:
|
||||
"""Whether path lies in the checkout or in the config's folder."""
|
||||
return any(path.resolve().is_relative_to(place.resolve()) for place in (top, folder))
|
||||
|
||||
|
||||
def held(folder: pathlib.Path, home: pathlib.Path) -> ProjectConfig | Failure | None:
|
||||
|
||||
@@ -149,6 +149,36 @@ class LoadTest(ConfigCase):
|
||||
self.assertIn(named, reason)
|
||||
self.assertIn("`decisions`", reason)
|
||||
|
||||
def test_env_file_is_the_one_optional_setting_and_names_a_file_outside_the_checkout_and_the_configs_folder(self) -> None:
|
||||
self.write("acme", CLIENT)
|
||||
found = load(self.checkout, self.checkout, self.home)
|
||||
assert isinstance(found, ProjectConfig), found
|
||||
self.assertIsNone(found.env_file, "a project that sets none has asked for no credentials, and never falls back to another project's")
|
||||
for case, spelled in {"~/": '"~/.claude-projects/acme.env"', "absolute": f'"{self.home}/.claude-projects/acme.env"'}.items():
|
||||
with self.subTest(case=case):
|
||||
self.write(f"acme-{case[0]}", CLIENT.replace("acme-billing", f"acme-{case[0]}") + f"env-file = {spelled}\n")
|
||||
named = load(self.home / "code" / f"acme-{case[0]}", self.home / "code" / f"acme-{case[0]}", self.home)
|
||||
assert isinstance(named, ProjectConfig), named
|
||||
self.assertEqual(named.env_file, self.home / ".claude-projects" / "acme.env")
|
||||
|
||||
def test_an_env_file_in_the_checkout_or_the_configs_folder_or_spelt_loosely_fails_naming_it(self) -> None:
|
||||
cases = {
|
||||
"a checkout: path": lambda home: '"checkout:.env"',
|
||||
"a folder: path": lambda home: '"folder:acme.env"',
|
||||
"no prefix": lambda home: '".env"',
|
||||
"a path in the checkout": lambda home: f'"{home}/code/acme-billing/.env"',
|
||||
"a path in the config's folder": lambda home: f'"{home}/.claude-projects/acme/acme.env"',
|
||||
"not text": lambda home: "3",
|
||||
}
|
||||
for case, spelled in cases.items():
|
||||
with self.subTest(case=case), tempfile.TemporaryDirectory() as made:
|
||||
self.home = pathlib.Path(made).resolve()
|
||||
self.checkout = self.home / "code" / "acme-billing"
|
||||
self.write("acme", CLIENT + f"env-file = {spelled(self.home)}\n")
|
||||
reason = self.failure()
|
||||
self.assertIn("`env-file`", reason)
|
||||
self.assertNotIn("is not a setting", reason)
|
||||
|
||||
def test_two_configs_naming_one_checkout_fail_naming_both(self) -> None:
|
||||
self.write("acme", CLIENT)
|
||||
self.write("acme-copy", CLIENT)
|
||||
|
||||
Reference in New Issue
Block a user