|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/RoseSecurity/kuzco/internal" |
| 8 | + u "github.com/RoseSecurity/kuzco/pkg/utils" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +var fixCmd = &cobra.Command{ |
| 13 | + Use: "fix", |
| 14 | + Short: "Diagnose configuration errors", |
| 15 | + Long: `This command analyzes and diagnoses Terraform configuration errors`, |
| 16 | + Example: "kuzco fix -f path/to/config.tf -t terraform", |
| 17 | + Run: Diagnose, |
| 18 | +} |
| 19 | + |
| 20 | +func init() { |
| 21 | + fixCmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the Terraform and OpenTofu file (required)") |
| 22 | + fixCmd.Flags().StringVarP(&tool, "tool", "t", "terraform", "Specifies the configuration tooling for configurations. Valid values include: `terraform` and `opentofu`") |
| 23 | + fixCmd.Flags().StringVarP(&model, "model", "m", "llama3.2", "LLM model to use for generating recommendations") |
| 24 | + fixCmd.Flags().StringVarP(&addr, "address", "a", "http://localhost:11434", "IP Address and port to use for the LLM model (ex: http://localhost:11434)") |
| 25 | +} |
| 26 | + |
| 27 | +func Diagnose(cmd *cobra.Command, args []string) { |
| 28 | + // Ensure file path is provided |
| 29 | + if filePath == "" { |
| 30 | + fmt.Fprintf(os.Stderr, "Error: file path is required. Use the -f flag to specify the configuration file.\n") |
| 31 | + os.Exit(1) |
| 32 | + } |
| 33 | + |
| 34 | + // Validate that the specified model exists in Ollama |
| 35 | + if err := internal.ValidateModel(model, addr); err != nil { |
| 36 | + fmt.Fprintf(os.Stderr, "Model validation error: %v\n", err) |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | + |
| 40 | + // Read the configuration file content |
| 41 | + config, err := os.ReadFile(filePath) |
| 42 | + if err != nil { |
| 43 | + u.LogErrorAndExit(err) |
| 44 | + } |
| 45 | + |
| 46 | + if len(config) == 0 { |
| 47 | + fmt.Fprintf(os.Stderr, "Error: configuration file is empty\n") |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + // Generate a formatted prompt for the recommendations function |
| 52 | + formattedPrompt := fmt.Sprintf(`Error detected in configuration file '%s': |
| 53 | +
|
| 54 | +Error Details: |
| 55 | +%%v |
| 56 | +
|
| 57 | +Resolution Steps: |
| 58 | +1. Identify the attribute(s) or syntax causing the error. |
| 59 | +2. Refer to the Terraform or OpenTofu documentation for valid syntax and attribute usage the resources. |
| 60 | +3. Correct the invalid attribute(s), fix the syntax, or remove the invalid attributes if they are unnecessary. |
| 61 | +4. Reformat the corrected resource block if needed. |
| 62 | +
|
| 63 | +Example Corrected Configuration: |
| 64 | +resource "type" "name" { |
| 65 | + # Explanation of attribute1's purpose |
| 66 | + attribute1 = "value1" |
| 67 | + |
| 68 | + # Optional comment for attribute2 |
| 69 | + attribute2 = "value2" |
| 70 | +} |
| 71 | +
|
| 72 | +Please review and update the configuration file as outlined above to resolve the issue.`, filePath) |
| 73 | + |
| 74 | + // Pass the prompt and file content to GetRecommendations |
| 75 | + recommendations, err := internal.GetRecommendations(string(config), nil, model, tool, formattedPrompt, addr) |
| 76 | + if err != nil { |
| 77 | + u.LogErrorAndExit(err) |
| 78 | + } |
| 79 | + |
| 80 | + internal.PrettyPrint(recommendations) |
| 81 | +} |
0 commit comments