124 lines
3.7 KiB
Python
124 lines
3.7 KiB
Python
|
|
"""
|
||
|
|
local-identity CLI — entry point.
|
||
|
|
|
||
|
|
Commands:
|
||
|
|
init [--force] [--email EMAIL] Derive primary user from Linux identity,
|
||
|
|
generate test users, write store.
|
||
|
|
list List all users in the store.
|
||
|
|
show <username> Display a user's YAML record.
|
||
|
|
|
||
|
|
Environment:
|
||
|
|
LOCAL_IDENTITY_HOME Override the store directory (default: ~/.local-identity).
|
||
|
|
"""
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
import sys
|
||
|
|
|
||
|
|
from .gecos import current_username, get_gecos_fullname
|
||
|
|
from .user import UserRecord, make_test_user
|
||
|
|
from . import store
|
||
|
|
|
||
|
|
|
||
|
|
def cmd_init(args: argparse.Namespace) -> None:
|
||
|
|
if store.store_exists() and not args.force:
|
||
|
|
print(
|
||
|
|
f"Store already exists at {store._store_dir()}. "
|
||
|
|
"Use --force to reinitialise.",
|
||
|
|
file=sys.stderr,
|
||
|
|
)
|
||
|
|
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", "")
|
||
|
|
if not email:
|
||
|
|
try:
|
||
|
|
email = input(f"Email address for {username}: ").strip()
|
||
|
|
except EOFError:
|
||
|
|
email = ""
|
||
|
|
if not email:
|
||
|
|
print("Error: email address is required.", file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
store.init_dirs()
|
||
|
|
config["email"] = email
|
||
|
|
store.write_config(config)
|
||
|
|
|
||
|
|
primary = UserRecord(username=username, fullname=fullname, email=email)
|
||
|
|
store.write_user(primary)
|
||
|
|
|
||
|
|
test1 = make_test_user(primary, 1)
|
||
|
|
test2 = make_test_user(primary, 2)
|
||
|
|
store.write_user(test1)
|
||
|
|
store.write_user(test2)
|
||
|
|
|
||
|
|
print(f"Initialised local-identity store at {store._store_dir()}")
|
||
|
|
print(f" Primary : {primary.username} ({primary.fullname}) <{primary.email}>")
|
||
|
|
print(f" Test 1 : {test1.username} <{test1.email}>")
|
||
|
|
print(f" Test 2 : {test2.username} <{test2.email}>")
|
||
|
|
|
||
|
|
|
||
|
|
def cmd_list(args: argparse.Namespace) -> None:
|
||
|
|
users = store.list_users()
|
||
|
|
if not users:
|
||
|
|
print("No users found. Run 'local-identity init' first.")
|
||
|
|
return
|
||
|
|
header = f"{'USERNAME':<20} {'FULLNAME':<30} {'EMAIL':<40} TYPE"
|
||
|
|
print(header)
|
||
|
|
print("-" * len(header))
|
||
|
|
for u in users:
|
||
|
|
utype = "test " if u.generated else "primary"
|
||
|
|
print(f"{u.username:<20} {u.fullname:<30} {u.email:<40} {utype}")
|
||
|
|
|
||
|
|
|
||
|
|
def cmd_show(args: argparse.Namespace) -> None:
|
||
|
|
try:
|
||
|
|
user = store.read_user(args.username)
|
||
|
|
except FileNotFoundError as exc:
|
||
|
|
print(str(exc), file=sys.stderr)
|
||
|
|
sys.exit(1)
|
||
|
|
print(user.to_yaml(), end="")
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> None:
|
||
|
|
parser = argparse.ArgumentParser(
|
||
|
|
prog="local-identity",
|
||
|
|
description="Zero-dependency bootstrap user store for net-kingdom environments.",
|
||
|
|
epilog=(
|
||
|
|
"Store location: ~/.local-identity "
|
||
|
|
"(override with LOCAL_IDENTITY_HOME)"
|
||
|
|
),
|
||
|
|
)
|
||
|
|
sub = parser.add_subparsers(dest="command", required=True)
|
||
|
|
|
||
|
|
p_init = sub.add_parser(
|
||
|
|
"init",
|
||
|
|
help="Initialise store from Linux identity",
|
||
|
|
)
|
||
|
|
p_init.add_argument(
|
||
|
|
"--force", action="store_true",
|
||
|
|
help="Reinitialise even if the store already exists",
|
||
|
|
)
|
||
|
|
p_init.add_argument(
|
||
|
|
"--email",
|
||
|
|
help="Email address (skips interactive prompt)",
|
||
|
|
)
|
||
|
|
p_init.set_defaults(func=cmd_init)
|
||
|
|
|
||
|
|
p_list = sub.add_parser("list", help="List all users in the store")
|
||
|
|
p_list.set_defaults(func=cmd_list)
|
||
|
|
|
||
|
|
p_show = sub.add_parser("show", help="Display a user record")
|
||
|
|
p_show.add_argument("username", help="Username to display")
|
||
|
|
p_show.set_defaults(func=cmd_show)
|
||
|
|
|
||
|
|
args = parser.parse_args()
|
||
|
|
args.func(args)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|