Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions cortex_search_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env python3
"""
CLI for cortex smart package search.

Usage:
cortex search "web server"
cortex search "postgress" --category database
cortex search-history
cortex search-history --clear
"""

import sys
import argparse
from pathlib import Path
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'Path' is not used.

Suggested change
from pathlib import Path

Copilot uses AI. Check for mistakes.

from smart_search import (
SmartPackageSearch,
PackageCategory,
format_search_results
)


def main():

Check failure on line 23 in cortex_search_cli.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 25 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=cortexlinux_cortex&issues=AZrrB6UJbSnhvoFNqviJ&open=AZrrB6UJbSnhvoFNqviJ&pullRequest=241
"""Main CLI entry point."""
parser = argparse.ArgumentParser(
prog='cortex search',
description='Smart package search with fuzzy matching',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
cortex search "web server"
cortex search "postgress"
cortex search "database" --category database
cortex search "nginx" --limit 5
cortex search-history
cortex search-history --clear

Categories:
web_server, database, development, language, container,
editor, security, network, monitoring, compression,
version_control, media, system, cloud
"""
)

subparsers = parser.add_subparsers(dest='command', help='Available commands')

# Search command
search_parser = subparsers.add_parser('search', help='Search for packages')
Comment on lines +23 to +48
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLI uses subparsers.add_parser('search', ...) but the main parser is configured with prog='cortex search'. This creates inconsistent command invocation - users would need to type cortex search search "query" instead of the expected cortex search "query". The standalone CLI should either be invoked as a separate script or properly integrated into the main Cortex CLI as a subcommand.

Copilot uses AI. Check for mistakes.
search_parser.add_argument('query', type=str, help='Search query (can include typos)')
search_parser.add_argument(
'--category',
type=str,
choices=[cat.name.lower() for cat in PackageCategory],
help='Filter by package category'
)
search_parser.add_argument(
'--limit',
type=int,
default=10,
help='Maximum number of results (default: 10)'
)

# History command
history_parser = subparsers.add_parser('history', help='View search history')
history_parser.add_argument(
'--limit',
type=int,
default=20,
help='Number of history entries to show (default: 20)'
)
history_parser.add_argument(
'--clear',
action='store_true',
help='Clear search history'
)

args = parser.parse_args()

if not args.command:
parser.print_help()
return 1

# Initialize search
search = SmartPackageSearch()

try:
if args.command == 'search':
# Convert category string to enum
category = None
if args.category:
category = PackageCategory[args.category.upper()]

# Perform search
results, suggestions = search.search(
args.query,
category=category,
limit=args.limit
)

# Format and display results
output = format_search_results(results, suggestions)
print(output)

return 0

elif args.command == 'history':
if args.clear:
# Clear history
search.clear_history()
print("✓ Search history cleared")
return 0
else:
# Display history
history = search.get_history(limit=args.limit)

if not history:
print("No search history found.")
return 0

print("\nSearch History:")
print("=" * 80)
print(f"{'Timestamp':<20} {'Query':<30} {'Results':<10} {'Top Result':<20}")
print("-" * 80)

for entry in history:
timestamp = entry.timestamp[:19].replace('T', ' ')
query = entry.query[:28] + '..' if len(entry.query) > 30 else entry.query
top_result = entry.top_result or "N/A"
top_result = top_result[:18] + '..' if len(top_result) > 20 else top_result

print(f"{timestamp:<20} {query:<30} {entry.results_count:<10} {top_result:<20}")

print("=" * 80)
return 0

except KeyboardInterrupt:
print("\n❌ Operation cancelled by user", file=sys.stderr)
return 130
except Exception as e:
print(f"❌ Error: {e}", file=sys.stderr)
return 1


if __name__ == '__main__':
sys.exit(main())
Comment on lines +1 to +145
Copy link

Copilot AI Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The standalone CLI (cortex_search_cli.py) is not integrated into the main Cortex CLI (cortex/cli.py). According to the PR description, integration code should be added to enable users to run cortex search commands. Consider adding the search command to the main CLI's subparsers and routing it to the SmartPackageSearch functionality, similar to how other commands like 'install' and 'history' are handled.

Copilot uses AI. Check for mistakes.
Loading
Loading