feat(local-identity): add --username and --fullname overrides to init

Resolves the system identity mismatch between the Linux username (worsch)
and the bootstrap identity (tegwick / Bernd Worsch / custom email).

Resolution order for all three fields: flag > config > system derivation.
Config is updated on every init so --force reinits are idempotent without
repeating the flags.

- cli.py: extract _resolve_init_params(); add --username / --fullname args;
  persist all three fields to config.yaml on init
- tests/test_cli.py: 13 new tests covering flag priority, config fallback,
  system derivation, config persistence, idempotent --force reinit

54 tests passing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-02 00:11:04 +01:00
parent 45cba35054
commit 666a56f4ed
2 changed files with 181 additions and 9 deletions

View File

@@ -2,8 +2,10 @@
local-identity CLI — entry point.
Commands:
init [--force] [--email EMAIL] Derive primary user from Linux identity,
generate test users, write store.
init [--force] [--username U] [--fullname N] [--email E]
Derive primary user, generate test users,
write store. All three identity fields are
resolved flag > config > system derivation.
list List all users in the store.
show <username> Display a user's YAML record.
@@ -19,6 +21,20 @@ from .user import UserRecord, make_test_user
from . import store
def _resolve_init_params(args: argparse.Namespace, config: dict) -> tuple[str, str, str]:
"""
Resolve (username, fullname, email) for init from three sources in order:
1. CLI flags (--username, --fullname, --email)
2. Persisted config (~/.local-identity/config.yaml)
3. System derivation ($USER / /etc/passwd GECOS) — username + fullname only
Email has no system default; missing email falls through to prompt in cmd_init.
"""
username: str = args.username or config.get("username") or current_username()
fullname: str = args.fullname or config.get("fullname") or get_gecos_fullname(username)
email: str = args.email or config.get("email") or ""
return username, fullname, email
def cmd_init(args: argparse.Namespace) -> None:
if store.store_exists() and not args.force:
print(
@@ -28,12 +44,9 @@ def cmd_init(args: argparse.Namespace) -> None:
)
sys.exit(1)
username = current_username()
fullname = get_gecos_fullname(username)
# Email resolution: flag > existing config > interactive prompt
config = store.read_config() if store.store_exists() else {}
email: str = args.email or config.get("email", "")
username, fullname, email = _resolve_init_params(args, config)
if not email:
try:
email = input(f"Email address for {username}: ").strip()
@@ -44,7 +57,7 @@ def cmd_init(args: argparse.Namespace) -> None:
sys.exit(1)
store.init_dirs()
config["email"] = email
config.update({"username": username, "fullname": fullname, "email": email})
store.write_config(config)
primary = UserRecord(username=username, fullname=fullname, email=email)
@@ -102,9 +115,17 @@ def main() -> None:
"--force", action="store_true",
help="Reinitialise even if the store already exists",
)
p_init.add_argument(
"--username",
help="Bootstrap username (default: $USER / $LOGNAME)",
)
p_init.add_argument(
"--fullname",
help="Full display name (default: /etc/passwd GECOS field)",
)
p_init.add_argument(
"--email",
help="Email address (skips interactive prompt)",
help="Email address (default: interactive prompt)",
)
p_init.set_defaults(func=cmd_init)