Coverage for src/lilbee/cli/commands/meta.py: 100%

64 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-09-08 09:20 +0000

1"""Version, status, reset, and init commands.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6 

7import typer 

8 

9from lilbee.app.reset import perform_reset 

10from lilbee.app.services import reset_store 

11from lilbee.app.status import gather_status 

12from lilbee.app.version import get_version 

13from lilbee.cli import theme 

14from lilbee.cli.app import ( 

15 apply_overrides, 

16 console, 

17 data_dir_option, 

18 global_option, 

19) 

20from lilbee.cli.helpers import json_output, render_status 

21from lilbee.core.config import cfg 

22from lilbee.core.system import LOCAL_ROOT_DIRNAME 

23from lilbee.data.ingest.ignore import IGNORE_FILENAME, IGNORE_TEMPLATE 

24 

25_yes_option = typer.Option(False, "--yes", "-y", help="Skip confirmation prompt.") 

26 

27 

28def version() -> None: 

29 """Show the lilbee version.""" 

30 ver = get_version() 

31 if cfg.json_mode: 

32 json_output({"command": "version", "version": ver}) 

33 return 

34 console.print(f"lilbee {ver}") 

35 

36 

37def status( 

38 data_dir: Path | None = data_dir_option, 

39 use_global: bool = global_option, 

40) -> None: 

41 """Show indexed documents, paths, and chunk counts.""" 

42 apply_overrides(data_dir=data_dir, use_global=use_global) 

43 # Status only reads the store; don't warm the inference fleet for a read. 

44 cfg.worker_pool_eager_start = False 

45 if cfg.json_mode: 

46 json_output(gather_status().model_dump(exclude_none=True)) 

47 return 

48 render_status(console) 

49 

50 

51def reset( 

52 data_dir: Path | None = data_dir_option, 

53 use_global: bool = global_option, 

54 yes: bool = _yes_option, 

55) -> None: 

56 """Delete all documents and data (full factory reset).""" 

57 apply_overrides(data_dir=data_dir, use_global=use_global) 

58 if not yes: 

59 if cfg.json_mode: 

60 json_output({"error": "Use --yes to confirm reset in JSON mode"}) 

61 raise SystemExit(1) 

62 console.print( 

63 f"[{theme.ERROR_BOLD}]This will delete ALL documents and data.[/{theme.ERROR_BOLD}]\n" 

64 f" Documents: {cfg.documents_dir}\n" 

65 f" Data: {cfg.data_dir}" 

66 ) 

67 confirmed = typer.confirm("Are you sure?", default=False) 

68 if not confirmed: 

69 console.print("Aborted.") 

70 raise SystemExit(0) 

71 

72 result = perform_reset() 

73 # Reopen LanceDB against the empty data dir; keep providers loaded. 

74 reset_store() 

75 

76 if cfg.json_mode: 

77 json_output(result.model_dump()) 

78 return 

79 

80 console.print( 

81 f"Reset complete: {result.deleted_docs} document(s), " 

82 f"{result.deleted_data} data item(s) deleted." 

83 ) 

84 if result.skipped: 

85 console.print( 

86 f"[{theme.WARNING}]{len(result.skipped)} item(s) could not be deleted " 

87 f"(locked or permission denied).[/{theme.WARNING}]" 

88 ) 

89 

90 

91def init() -> None: 

92 """Initialize a local .lilbee/ knowledge base in the current directory.""" 

93 root = Path.cwd() / LOCAL_ROOT_DIRNAME 

94 if root.is_dir(): 

95 if cfg.json_mode: 

96 json_output({"command": "init", "path": str(root), "created": False}) 

97 return 

98 console.print(f"Already initialized: {root}") 

99 return 

100 

101 docs = root / "documents" 

102 data = root / "data" 

103 docs.mkdir(parents=True) 

104 data.mkdir(parents=True) 

105 (root / ".gitignore").write_text("data/\n", encoding="utf-8") 

106 (root / IGNORE_FILENAME).write_text(IGNORE_TEMPLATE, encoding="utf-8") 

107 

108 if cfg.json_mode: 

109 json_output({"command": "init", "path": str(root), "created": True}) 

110 return 

111 console.print(f"Initialized local knowledge base at {root}")