-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from RoseSecurity/add-fix-functionality
Add `fix` functionality
- Loading branch information
Showing
20 changed files
with
249 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/RoseSecurity/kuzco/internal" | ||
u "github.com/RoseSecurity/kuzco/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var fixCmd = &cobra.Command{ | ||
Use: "fix", | ||
Short: "Diagnose configuration errors", | ||
Long: `This command analyzes and diagnoses Terraform configuration errors`, | ||
Example: "kuzco fix -f path/to/config.tf -t terraform", | ||
Run: Diagnose, | ||
} | ||
|
||
func init() { | ||
fixCmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the Terraform and OpenTofu file (required)") | ||
fixCmd.Flags().StringVarP(&tool, "tool", "t", "terraform", "Specifies the configuration tooling for configurations. Valid values include: `terraform` and `opentofu`") | ||
fixCmd.Flags().StringVarP(&model, "model", "m", "llama3.2", "LLM model to use for generating recommendations") | ||
fixCmd.Flags().StringVarP(&addr, "address", "a", "http://localhost:11434", "IP Address and port to use for the LLM model (ex: http://localhost:11434)") | ||
} | ||
|
||
func Diagnose(cmd *cobra.Command, args []string) { | ||
// Ensure file path is provided | ||
if filePath == "" { | ||
fmt.Fprintf(os.Stderr, "Error: file path is required. Use the -f flag to specify the configuration file.\n") | ||
os.Exit(1) | ||
} | ||
|
||
// Validate that the specified model exists in Ollama | ||
if err := internal.ValidateModel(model, addr); err != nil { | ||
fmt.Fprintf(os.Stderr, "Model validation error: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Read the configuration file content | ||
config, err := os.ReadFile(filePath) | ||
if err != nil { | ||
u.LogErrorAndExit(err) | ||
} | ||
|
||
if len(config) == 0 { | ||
fmt.Fprintf(os.Stderr, "Error: configuration file is empty\n") | ||
os.Exit(1) | ||
} | ||
|
||
// Generate a formatted prompt for the recommendations function | ||
formattedPrompt := fmt.Sprintf(`Error detected in configuration file '%s': | ||
Error Details: | ||
%%v | ||
Resolution Steps: | ||
1. Identify the attribute(s) or syntax causing the error. | ||
2. Refer to the Terraform or OpenTofu documentation for valid syntax and attribute usage the resources. | ||
3. Correct the invalid attribute(s), fix the syntax, or remove the invalid attributes if they are unnecessary. | ||
4. Reformat the corrected resource block if needed. | ||
Example Corrected Configuration: | ||
resource "type" "name" { | ||
# Explanation of attribute1's purpose | ||
attribute1 = "value1" | ||
# Optional comment for attribute2 | ||
attribute2 = "value2" | ||
} | ||
Please review and update the configuration file as outlined above to resolve the issue.`, filePath) | ||
|
||
// Pass the prompt and file content to GetRecommendations | ||
recommendations, err := internal.GetRecommendations(string(config), nil, model, tool, formattedPrompt, addr) | ||
if err != nil { | ||
u.LogErrorAndExit(err) | ||
} | ||
|
||
internal.PrettyPrint(recommendations) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/RoseSecurity/kuzco/internal" | ||
u "github.com/RoseSecurity/kuzco/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var recommendCmd = &cobra.Command{ | ||
Use: "recommend", | ||
Short: "Intelligently analyze your Terraform and OpenTofu configurations", | ||
Long: `Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations for boosting efficiency, security, and performance.`, | ||
Run: Analyze, | ||
} | ||
|
||
func init() { | ||
recommendCmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the Terraform and OpenTofu file (required)") | ||
recommendCmd.Flags().StringVarP(&tool, "tool", "t", "terraform", "Specifies the configuration tooling for configurations. Valid values include: `terraform` and `opentofu`") | ||
recommendCmd.Flags().StringVarP(&model, "model", "m", "llama3.2", "LLM model to use for generating recommendations") | ||
recommendCmd.Flags().StringVarP(&prompt, "prompt", "p", "", "User prompt for guiding the response format of the LLM model") | ||
recommendCmd.Flags().StringVarP(&addr, "address", "a", "http://localhost:11434", "IP Address and port to use for the LLM model (ex: http://localhost:11434)") | ||
} | ||
|
||
func Analyze(cmd *cobra.Command, args []string) { | ||
// Validate that the specified model exists in Ollama | ||
if err := internal.ValidateModel(model, addr); err != nil { | ||
u.LogErrorAndExit(err) | ||
} | ||
|
||
// Proceed with the main logic if all required flags are set | ||
if err := internal.Run(filePath, tool, model, prompt, addr); err != nil { | ||
u.LogErrorAndExit(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
## kuzco fix | ||
|
||
Diagnose configuration errors | ||
|
||
### Synopsis | ||
|
||
This command analyzes and diagnoses Terraform configuration errors | ||
|
||
``` | ||
kuzco fix [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
kuzco fix -f path/to/config.tf -t terraform | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-a, --address string IP Address and port to use for the LLM model (ex: http://localhost:11434) (default "http://localhost:11434") | ||
-f, --file string Path to the Terraform and OpenTofu file (required) | ||
-h, --help help for fix | ||
-m, --model string LLM model to use for generating recommendations (default "llama3.2") | ||
-t, --tool terraform Specifies the configuration tooling for configurations. Valid values include: terraform and `opentofu` (default "terraform") | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [kuzco](kuzco.md) - Intelligently analyze your Terraform and OpenTofu configurations | ||
|
||
###### Auto generated by spf13/cobra on 2-Nov-2024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
## kuzco recommend | ||
|
||
Intelligently analyze your Terraform and OpenTofu configurations | ||
|
||
### Synopsis | ||
|
||
Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations for boosting efficiency, security, and performance. | ||
|
||
``` | ||
kuzco recommend [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-a, --address string IP Address and port to use for the LLM model (ex: http://localhost:11434) (default "http://localhost:11434") | ||
-f, --file string Path to the Terraform and OpenTofu file (required) | ||
-h, --help help for recommend | ||
-m, --model string LLM model to use for generating recommendations (default "llama3.2") | ||
-p, --prompt string User prompt for guiding the response format of the LLM model | ||
-t, --tool terraform Specifies the configuration tooling for configurations. Valid values include: terraform and `opentofu` (default "terraform") | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [kuzco](kuzco.md) - Intelligently analyze your Terraform and OpenTofu configurations | ||
|
||
###### Auto generated by spf13/cobra on 2-Nov-2024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.