88 lines
4.3 KiB
Python
88 lines
4.3 KiB
Python
"""Print the paths a project's config sets.
|
|
|
|
usage: project-config [<setting>]
|
|
|
|
Run inside a checkout, or a worktree of one. It finds the config that names the checkout,
|
|
~/.claude-projects/<key>/config.toml, and prints each setting and its absolute path, as in
|
|
`plan: /home/me/code/acme/todo.md`. Given a setting, it prints only that setting's path.
|
|
|
|
The config is TOML. `checkout` names the project's main checkout, and every other setting is a path:
|
|
"checkout:<path>" inside the checkout, "folder:<path>" inside the config's own folder, or one that
|
|
starts with / or ~/. In a worktree, a "checkout:" path is the worktree's own file.
|
|
|
|
checkout = "~/code/acme-billing"
|
|
plan = "folder:todo.md"
|
|
working-rules = "folder:AGENTS.md"
|
|
decisions = "folder:docs/decisions.md"
|
|
score-anchors = "folder:docs/score-anchors.md"
|
|
comprehension-history = "folder:docs/comprehension-history.md"
|
|
reading-cost = "folder:docs/reading-cost.md"
|
|
changelog = "checkout:CHANGELOG.md"
|
|
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 no credentials file, and
|
|
a tool that needs one fails naming the setting. It is printed with the others only where it is set.
|
|
|
|
`wait-for-approval` is the other optional setting, true or false: whether `chunk-pr merge` waits for a human's
|
|
approval of the pull request, as GitHub's branch protection asks for it, before it merges. Where it is left out it
|
|
is false. Asked for by name it prints true or false; it is listed with the others only where it is true.
|
|
|
|
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, and where `env-file` is asked for and not set.
|
|
No setting has a default.
|
|
"""
|
|
import pathlib
|
|
import sys
|
|
|
|
from project_config.settings import CONFIG, ENV_FILE, load, ProjectConfig, SETTINGS, WAIT_FOR_APPROVAL
|
|
from failure import Failure
|
|
from git_cli.git_repo import git, Git
|
|
|
|
|
|
def main(argv: list[str]) -> int:
|
|
if argv[:1] in (["-h"], ["--help"]):
|
|
print(__doc__.strip())
|
|
return 0
|
|
if len(argv) > 1 or (argv and argv[0] not in (*SETTINGS, ENV_FILE, WAIT_FOR_APPROVAL)):
|
|
print(f"usage: project-config [<setting>], where <setting> is one of {', '.join((*SETTINGS, ENV_FILE, WAIT_FOR_APPROVAL))}", file=sys.stderr)
|
|
return 1
|
|
config = located(None, pathlib.Path.home())
|
|
if isinstance(config, Failure):
|
|
print(config.reason, file=sys.stderr)
|
|
return 1
|
|
if argv == [WAIT_FOR_APPROVAL]:
|
|
print("true" if config.wait_for_approval else "false")
|
|
return 0
|
|
paths = {**config.paths, **({ENV_FILE: config.env_file} if config.env_file else {})}
|
|
if argv and argv[0] not in paths:
|
|
print(f"{config.folder / CONFIG} sets no `{ENV_FILE}`", file=sys.stderr)
|
|
return 1
|
|
listed = [f"{name}: {path}" for name, path in paths.items()] + ([f"{WAIT_FOR_APPROVAL}: true"] if config.wait_for_approval else [])
|
|
print(paths[argv[0]] if argv else "\n".join(listed))
|
|
return 0
|
|
|
|
|
|
def located(directory: str | None, home: pathlib.Path, run: Git = git) -> ProjectConfig | Failure:
|
|
"""The config under home of the checkout that holds directory, else the current directory."""
|
|
found = run("rev-parse", "--path-format=absolute", "--show-toplevel", "--git-common-dir", cwd=directory)
|
|
if isinstance(found, Failure):
|
|
return found
|
|
if len(lines := found.splitlines()) != 2:
|
|
return Failure(f"git rev-parse printed {found!r}, not a checkout's top folder and git directory on two lines")
|
|
# The common dir is the main checkout's .git, whichever worktree runs this.
|
|
return load(pathlib.Path(lines[0]), pathlib.Path(lines[1]).parent, home)
|
|
|
|
|
|
def project() -> ProjectConfig | Failure:
|
|
"""The config of the project whose checkout holds the current directory, under the user's home."""
|
|
return located(None, pathlib.Path.home())
|
|
|
|
|
|
def goals_file(directory: str, home: pathlib.Path) -> pathlib.Path | Failure:
|
|
"""The file holding the premises, `## Goals` and `## Audience`, of the project whose checkout holds directory, as its config under home names it."""
|
|
config = located(directory, home)
|
|
return config if isinstance(config, Failure) else config.paths["goals-and-audience"]
|