Files
claude-config/bin/project_config/main.py
T

70 lines
3.1 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"
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.
"""
import pathlib
import sys
from failure import Failure
from git_cli.git_repo import git, Git
from project_config.settings import load, ProjectConfig, SETTINGS, Unnamed
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):
print(f"usage: project-config [<setting>], where <setting> is one of {', '.join(SETTINGS)}", file=sys.stderr)
return 1
config = located(None, pathlib.Path.home())
if isinstance(config, Failure):
print(config.reason, file=sys.stderr)
return 1
print(config.paths[argv[0]] if argv else "\n".join(f"{name}: {config.paths[name]}" for name in SETTINGS))
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 Unnamed(found.reason)
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"]