103 lines
3.3 KiB
Python
103 lines
3.3 KiB
Python
"""Main entry point for ShellGen CLI."""
|
|
|
|
import sys
|
|
from .ui.argparse import create_parser
|
|
from .ui.console import ConsoleUI
|
|
from .core.generator import CommandGenerator
|
|
from .backends.factory import BackendFactory
|
|
from .safety.checker import SafetyChecker
|
|
from .history import HistoryManager
|
|
from .config import Config
|
|
|
|
|
|
def main() -> int:
|
|
"""Main entry point for the CLI."""
|
|
parser = create_parser()
|
|
args = parser.parse_args()
|
|
|
|
config = Config()
|
|
|
|
console = ConsoleUI()
|
|
console.print_header()
|
|
|
|
try:
|
|
backend = BackendFactory.create_backend(
|
|
backend_type=args.backend or config.get("default_backend", "ollama"),
|
|
host=config.get("OLLAMA_HOST"),
|
|
model=config.get("OLLAMA_MODEL") or "codellama",
|
|
)
|
|
|
|
generator = CommandGenerator(backend)
|
|
safety_checker = SafetyChecker()
|
|
history_manager = HistoryManager(
|
|
path=config.get("SHELLGEN_HISTORY_PATH", "~/.shellgen/history.db")
|
|
)
|
|
|
|
if args.command == "generate":
|
|
result = generator.generate(
|
|
description=args.description,
|
|
shell=args.shell or config.get("shell.default", "bash"),
|
|
)
|
|
|
|
console.display_generated_command(result.command, result.explanation)
|
|
|
|
is_safe, warning = safety_checker.check(result.command)
|
|
|
|
if not is_safe and not args.force:
|
|
console.print_safety_warning(warning)
|
|
if not console.confirm_execution():
|
|
console.print_cancelled()
|
|
return 0
|
|
|
|
if args.auto_execute and is_safe:
|
|
console.execute_command(result.command)
|
|
history_manager.add_entry(
|
|
prompt=args.description,
|
|
command=result.command,
|
|
shell=args.shell or "bash",
|
|
executed=True,
|
|
)
|
|
elif args.execute or (args.auto_execute and is_safe):
|
|
if console.confirm_execution():
|
|
console.execute_command(result.command)
|
|
history_manager.add_entry(
|
|
prompt=args.description,
|
|
command=result.command,
|
|
shell=args.shell or "bash",
|
|
executed=True,
|
|
)
|
|
else:
|
|
console.print_cancelled()
|
|
else:
|
|
history_manager.add_entry(
|
|
prompt=args.description,
|
|
command=result.command,
|
|
shell=args.shell or "bash",
|
|
executed=False,
|
|
)
|
|
|
|
elif args.command == "history":
|
|
entries = history_manager.get_history(limit=args.limit)
|
|
console.display_history(entries)
|
|
|
|
elif args.command == "feedback":
|
|
history_manager.add_feedback(
|
|
entry_id=args.id,
|
|
corrected_command=args.corrected,
|
|
feedback=args.feedback,
|
|
)
|
|
console.print_feedback_submitted()
|
|
|
|
return 0
|
|
|
|
except ConnectionError as e:
|
|
console.print_error(f"Connection error: {e}")
|
|
return 1
|
|
except Exception as e:
|
|
console.print_error(f"Error: {e}")
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|