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

61 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-05-15 20:55 +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 

23 

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

25 

26 

27def version() -> None: 

28 """Show the lilbee version.""" 

29 ver = get_version() 

30 if cfg.json_mode: 

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

32 return 

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

34 

35 

36def status( 

37 data_dir: Path | None = data_dir_option, 

38 use_global: bool = global_option, 

39) -> None: 

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

41 apply_overrides(data_dir=data_dir, use_global=use_global) 

42 if cfg.json_mode: 

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

44 return 

45 render_status(console) 

46 

47 

48def reset( 

49 data_dir: Path | None = data_dir_option, 

50 use_global: bool = global_option, 

51 yes: bool = _yes_option, 

52) -> None: 

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

54 apply_overrides(data_dir=data_dir, use_global=use_global) 

55 if not yes: 

56 if cfg.json_mode: 

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

58 raise SystemExit(1) 

59 console.print( 

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

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

62 f" Data: {cfg.data_dir}" 

63 ) 

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

65 if not confirmed: 

66 console.print("Aborted.") 

67 raise SystemExit(0) 

68 

69 result = perform_reset() 

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

71 reset_store() 

72 

73 if cfg.json_mode: 

74 json_output(result.model_dump()) 

75 return 

76 

77 console.print( 

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

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

80 ) 

81 if result.skipped: 

82 console.print( 

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

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

85 ) 

86 

87 

88def init() -> None: 

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

90 root = Path.cwd() / LOCAL_ROOT_DIRNAME 

91 if root.is_dir(): 

92 if cfg.json_mode: 

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

94 return 

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

96 return 

97 

98 docs = root / "documents" 

99 data = root / "data" 

100 docs.mkdir(parents=True) 

101 data.mkdir(parents=True) 

102 (root / ".gitignore").write_text("data/\n") 

103 

104 if cfg.json_mode: 

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

106 return 

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