# codex_cli/config.pyimportosfromrich.consoleimportConsolefromrich.markdownimportMarkdownfrompathlibimportPath# Use Path for type hinting# Import the utility for making OpenAI API callsfrom.core.openai_utilsimportget_openai_response# Initialize console for outputconsole=Console()
[docs]defexplain_config(file_path:Path):""" Reads a configuration file and asks an AI model to explain it. Args: file_path: Path object pointing to the configuration file. """console.print(f"Analyzing configuration file: [cyan]{file_path}[/cyan]")# --- Read File Content ---try:content=file_path.read_text(encoding='utf-8')ifnotcontent.strip():console.print("[yellow]Warning: The configuration file is empty.[/yellow]")# Decide if we should proceed or return# Let's proceed for now, the model might still comment on the filename/type# returnexceptExceptionase:console.print(f"[bold red]Error reading file {file_path}: {e}[/bold red]")# Note: Typer's `readable=True` might catch permission errors before thisreturn# --- Determine File Type (Simple version based on extension/name for the prompt) ---file_extension=file_path.suffix.lower()filename_lower=file_path.name.lower()# Get lower case filename# Basic type guessing, can be expanded significantlyiffile_extensionin['.yaml','.yml']:config_type="YAML"eliffile_extension=='.json':config_type="JSON"eliffile_extension=='.toml':config_type="TOML"eliffile_extension=='.ini':config_type="INI"eliffile_extension=='.conf':config_type="CONF-style"eliffile_extension=='.xml':config_type="XML"# --- FIX: Handle common known filenames without extensions ---eliffilename_lower=='dockerfile':config_type="Dockerfile"eliffilename_lower=='makefile':config_type="Makefile"# --- FIX: Use filename if extension is missing or unknown ---elifnotfile_extension:# If no extension, use the filename itself (maybe it's e.g. 'hosts')config_type=f"'{filename_lower}' (no extension)"else:# Otherwise, use the extensionconfig_type=f"'{file_extension}'"# Note: This doesn't guarantee correctness, just helps the prompt.# --- Construct the Prompt ---prompt=f""" Act as an expert DevOps engineer and system administrator. Explain the following configuration file (likely {config_type} format). File Path: "{file_path.name}" Content: ```{content} ``` Instructions: 1. Identify the primary purpose or technology this configuration file relates to (e.g., Nginx, Docker Compose, Kubernetes, application settings, etc.). 2. Explain the overall structure of the file. 3. Describe the meaning and purpose of the key sections, directives, or parameters found in the content. 4. If possible, mention any potential best practices or common pitfalls related to this type of configuration. 5. Respond clearly using Markdown formatting. """# --- Get Explanation from OpenAI ---explanation=get_openai_response(prompt,model="gpt-4o")# Using a capable model# --- Display the Explanation ---ifexplanation:ifisinstance(explanation,str):console.print("\n✨ [bold green]Configuration File Explanation:[/bold green]")md=Markdown(explanation)console.print(md)else:# Handle unexpected non-string dataconsole.print("\n[bold yellow]Warning: Received non-string data as explanation.[/bold yellow]")console.print(f"Raw data: {str(explanation)}")else:# Handle API call failureconsole.print("[bold red]Failed to get explanation from OpenAI.[/bold red]")