Skip to content

Commit

Permalink
Merge pull request #22 from RoseSecurity/add-fix-functionality
Browse files Browse the repository at this point in the history
Add `fix` functionality
  • Loading branch information
RoseSecurity authored Nov 3, 2024
2 parents 9bb0918 + bedf334 commit 7f665a4
Show file tree
Hide file tree
Showing 20 changed files with 249 additions and 60 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,23 @@ The following configuration options are available:
██  ██  ██████  ███████  ██████  ██████


Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations for boosting efficiency, security, and performance.
Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations and fixes for boosting efficiency, security, and performance.

Usage:
kuzco [flags]
kuzco [command]

Available Commands:
completion Generate the autocompletion script for the specified shell
fix Diagnose configuration errors
help Help about any command
recommend Intelligently analyze your Terraform and OpenTofu configurations
version Print the CLI version

Flags:
-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 kuzco
-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")
-h, --help help for kuzco

Use "kuzco [command] --help" for more information about a command.
```

## Contributing
Expand Down
6 changes: 1 addition & 5 deletions cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra/doc"
)

// Generate documentation for Terramaid commands and output to docs directory
// Generate documentation for Kuzco commands and output to docs directory
var docsCmd = &cobra.Command{
Use: "docs",
Short: "Generate documentation for the CLI",
Expand All @@ -20,7 +20,3 @@ var docsCmd = &cobra.Command{
return nil
},
}

func init() {
rootCmd.AddCommand(docsCmd)
}
81 changes: 81 additions & 0 deletions cmd/fix.go
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)
}
34 changes: 34 additions & 0 deletions cmd/recommend.go
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)
}
}
24 changes: 7 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"os"

"github.com/RoseSecurity/kuzco/internal"
tuiUtils "github.com/RoseSecurity/kuzco/internal/tui/utils"
Expand All @@ -21,17 +20,15 @@ var (
var rootCmd = &cobra.Command{
Use: "kuzco",
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.`,
Long: `Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations and fixes for boosting efficiency, security, and performance.`,
Run: runAnalyzer,
}

func init() {
rootCmd.AddCommand(docsCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the Terraform and OpenTofu file (required)")
rootCmd.Flags().StringVarP(&tool, "tool", "t", "terraform", "Specifies the configuration tooling for configurations. Valid values include: `terraform` and `opentofu`")
rootCmd.Flags().StringVarP(&model, "model", "m", "llama3.2", "LLM model to use for generating recommendations")
rootCmd.Flags().StringVarP(&prompt, "prompt", "p", "", "User prompt for guiding the response format of the LLM model")
rootCmd.Flags().StringVarP(&addr, "address", "a", "http://localhost:11434", "IP Address and port to use for the LLM model (ex: http://localhost:11434)")
rootCmd.AddCommand(recommendCmd)
rootCmd.AddCommand(fixCmd)
}

func runAnalyzer(cmd *cobra.Command, args []string) {
Expand All @@ -50,20 +47,13 @@ func runAnalyzer(cmd *cobra.Command, args []string) {

// 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)
}

// Proceed with the main logic if all required flags are set
if err := internal.Run(filePath, tool, model, prompt, addr); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
u.LogErrorAndExit(err)
}
cmd.Help()
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
u.LogErrorAndExit(err)
}
}
12 changes: 7 additions & 5 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (

"github.com/fatih/color"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)

var Version = "0.2.0"
// Placeholder for builds
var Version = "1.0.0"

type Release struct {
TagName string `json:"tag_name"`
Expand All @@ -28,8 +30,8 @@ var versionCmd = &cobra.Command{
if err == nil && latestReleaseTag != "" {
latestRelease := strings.TrimPrefix(latestReleaseTag, "v")
currentRelease := strings.TrimPrefix(Version, "v")
if latestRelease != currentRelease {
updateTerramaid(latestRelease)
if semver.Compare(latestRelease, currentRelease) > 0 {
updateKuzco(latestRelease)
}
}
},
Expand Down Expand Up @@ -57,8 +59,8 @@ func latestRelease() (string, error) {
}

// Display out of date warning
func updateTerramaid(latestVersion string) {
func updateKuzco(latestVersion string) {
c1 := color.New(color.FgCyan)

c1.Println(fmt.Sprintf("\nYour version of Terramaid is out of date. The latest version is %s\n\n", latestVersion))
c1.Println(fmt.Sprintf("\nYour version of Kuzco is out of date. The latest version is %s\n\n", latestVersion))
}
13 changes: 6 additions & 7 deletions docs/kuzco.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## kuzco

Intelligently analyze your Terraform configurations
Intelligently analyze your Terraform and OpenTofu configurations

### Synopsis

Intelligently analyze your Terraform configurations to receive personalized recommendations for boosting efficiency, security, and performance.
Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations and fixes for boosting efficiency, security, and performance.

```
kuzco [flags]
Expand All @@ -13,15 +13,14 @@ kuzco [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 file (required)
-h, --help help for kuzco
-m, --model string LLM model to use for generating recommendations (default "llama3.1")
-h, --help help for kuzco
```

### SEE ALSO

* [kuzco completion](kuzco_completion.md) - Generate the autocompletion script for the specified shell
* [kuzco fix](kuzco_fix.md) - Diagnose configuration errors
* [kuzco recommend](kuzco_recommend.md) - Intelligently analyze your Terraform and OpenTofu configurations
* [kuzco version](kuzco_version.md) - Print the CLI version

###### Auto generated by spf13/cobra on 27-Sep-2024
###### Auto generated by spf13/cobra on 2-Nov-2024
4 changes: 2 additions & 2 deletions docs/kuzco_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ See each sub-command's help for details on how to use the generated script.

### SEE ALSO

* [kuzco](kuzco.md) - Intelligently analyze your Terraform configurations
* [kuzco](kuzco.md) - Intelligently analyze your Terraform and OpenTofu configurations
* [kuzco completion bash](kuzco_completion_bash.md) - Generate the autocompletion script for bash
* [kuzco completion fish](kuzco_completion_fish.md) - Generate the autocompletion script for fish
* [kuzco completion powershell](kuzco_completion_powershell.md) - Generate the autocompletion script for powershell
* [kuzco completion zsh](kuzco_completion_zsh.md) - Generate the autocompletion script for zsh

###### Auto generated by spf13/cobra on 27-Sep-2024
###### Auto generated by spf13/cobra on 2-Nov-2024
2 changes: 1 addition & 1 deletion docs/kuzco_completion_bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ kuzco completion bash

* [kuzco completion](kuzco_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 27-Sep-2024
###### Auto generated by spf13/cobra on 2-Nov-2024
2 changes: 1 addition & 1 deletion docs/kuzco_completion_fish.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ kuzco completion fish [flags]

* [kuzco completion](kuzco_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 27-Sep-2024
###### Auto generated by spf13/cobra on 2-Nov-2024
2 changes: 1 addition & 1 deletion docs/kuzco_completion_powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ kuzco completion powershell [flags]

* [kuzco completion](kuzco_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 27-Sep-2024
###### Auto generated by spf13/cobra on 2-Nov-2024
2 changes: 1 addition & 1 deletion docs/kuzco_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ kuzco completion zsh [flags]

* [kuzco completion](kuzco_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 27-Sep-2024
###### Auto generated by spf13/cobra on 2-Nov-2024
33 changes: 33 additions & 0 deletions docs/kuzco_fix.md
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
28 changes: 28 additions & 0 deletions docs/kuzco_recommend.md
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
4 changes: 2 additions & 2 deletions docs/kuzco_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ kuzco version

### SEE ALSO

* [kuzco](kuzco.md) - Intelligently analyze your Terraform configurations
* [kuzco](kuzco.md) - Intelligently analyze your Terraform and OpenTofu configurations

###### Auto generated by spf13/cobra on 27-Sep-2024
###### Auto generated by spf13/cobra on 2-Nov-2024
Loading

0 comments on commit 7f665a4

Please sign in to comment.