From 6d3d4ff4d9678551956f2e03a32d48ca263c00c8 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 12:41:41 +0000 Subject: [PATCH] Add shellgen core modules: main, config, history --- app/shellgen/main.py | 102 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 app/shellgen/main.py diff --git a/app/shellgen/main.py b/app/shellgen/main.py new file mode 100644 index 0000000..2df0384 --- /dev/null +++ b/app/shellgen/main.py @@ -0,0 +1,102 @@ +"""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())