feat(sec): initial release v1.0.0-alpha - ergonomic query routing engine
This commit is contained in:
+118
@@ -0,0 +1,118 @@
|
||||
import sys
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
try:
|
||||
from sec.src.catalog import catalog
|
||||
from sec.src.client import CheatClient
|
||||
from sec.src.clipboard import copy_to_clipboard, extract_code_only, strip_ansi
|
||||
from sec.src.manifest import load_manifest
|
||||
from sec.src.renderer import SnippetRenderer
|
||||
from sec.src.router import QueryRouter
|
||||
except ImportError:
|
||||
from src.catalog import catalog
|
||||
from src.client import CheatClient
|
||||
from src.clipboard import copy_to_clipboard, extract_code_only, strip_ansi
|
||||
from src.manifest import load_manifest
|
||||
from src.renderer import SnippetRenderer
|
||||
from src.router import QueryRouter
|
||||
|
||||
|
||||
def parse_arguments(raw_args: List[str]) -> Tuple[List[str], Optional[str], bool]:
|
||||
"""Parse raw arguments into (query_tokens, copy_mode, is_version_flag)."""
|
||||
copy_mode = None
|
||||
is_version = False
|
||||
query_tokens: List[str] = []
|
||||
|
||||
i = 0
|
||||
while i < len(raw_args):
|
||||
arg = raw_args[i]
|
||||
|
||||
if arg in ["-V", "-v", "--version"]:
|
||||
is_version = True
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if arg in ["-c", "--copy"]:
|
||||
# Check if next token is 'a' or 'all'
|
||||
if i + 1 < len(raw_args) and raw_args[i + 1].lower() in ["a", "all"]:
|
||||
copy_mode = "all"
|
||||
i += 2
|
||||
continue
|
||||
else:
|
||||
copy_mode = "code"
|
||||
i += 1
|
||||
continue
|
||||
|
||||
if arg in ["--copy-all", "-ca"]:
|
||||
copy_mode = "all"
|
||||
i += 1
|
||||
continue
|
||||
|
||||
query_tokens.append(arg)
|
||||
i += 1
|
||||
|
||||
return query_tokens, copy_mode, is_version
|
||||
|
||||
|
||||
def run_cli(args: Optional[List[str]] = None) -> int:
|
||||
if hasattr(sys.stdout, "reconfigure"):
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
raw_args = args if args is not None else sys.argv[1:]
|
||||
query_tokens, copy_mode, is_version = parse_arguments(raw_args)
|
||||
|
||||
if is_version:
|
||||
manifest = load_manifest()
|
||||
version_text = catalog.format(
|
||||
"app.version_output",
|
||||
name=manifest.get("name", "SEC"),
|
||||
version=manifest.get("version", "1.0.0"),
|
||||
stage=manifest.get("stage", "Alpha"),
|
||||
description=manifest.get("description", ""),
|
||||
)
|
||||
print(version_text)
|
||||
return 0
|
||||
|
||||
if not query_tokens:
|
||||
manifest = load_manifest()
|
||||
print(f"{manifest.get('name', 'SEC')} v{manifest.get('version', '1.0.0')} [{manifest.get('stage', 'Alpha')}] - {manifest.get('description', '')}")
|
||||
print("Usage: sec <query...> [-c] [-c a]")
|
||||
print("Examples:")
|
||||
print(" sec python read json")
|
||||
print(" sec tar extract")
|
||||
print(" sec linux memory usage")
|
||||
print(" sec git undo commit -c # Copy code only to clipboard")
|
||||
print(" sec docker build -c a # Copy full response to clipboard")
|
||||
return 0
|
||||
|
||||
raw_query = " ".join(query_tokens)
|
||||
router = QueryRouter()
|
||||
routed = router.route(query_tokens)
|
||||
|
||||
client = CheatClient()
|
||||
renderer = SnippetRenderer()
|
||||
|
||||
result = client.fetch(routed)
|
||||
if not result:
|
||||
renderer.render_not_found(raw_query)
|
||||
return 0
|
||||
|
||||
copied = False
|
||||
is_all_copy = False
|
||||
if copy_mode:
|
||||
if copy_mode == "all":
|
||||
text_to_copy = strip_ansi(result.content)
|
||||
is_all_copy = True
|
||||
else:
|
||||
text_to_copy = extract_code_only(result.content)
|
||||
is_all_copy = False
|
||||
|
||||
if text_to_copy:
|
||||
copied = copy_to_clipboard(text_to_copy)
|
||||
|
||||
renderer.render_result(result, copied=copied, is_all_copy=is_all_copy)
|
||||
return 0
|
||||
Reference in New Issue
Block a user