Skip to content

Commit 7f665a4

Browse files
authored
Merge pull request #22 from RoseSecurity/add-fix-functionality
Add `fix` functionality
2 parents 9bb0918 + bedf334 commit 7f665a4

20 files changed

+249
-60
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,23 @@ The following configuration options are available:
6969
██  ██  ██████  ███████  ██████  ██████
7070

7171

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

7474
Usage:
7575
kuzco [flags]
7676
kuzco [command]
7777

7878
Available Commands:
7979
completion Generate the autocompletion script for the specified shell
80+
fix Diagnose configuration errors
8081
help Help about any command
82+
recommend Intelligently analyze your Terraform and OpenTofu configurations
8183
version Print the CLI version
8284

8385
Flags:
84-
-a, --address string IP Address and port to use for the LLM model (ex: http://localhost:11434) (default "http://localhost:11434")
85-
-f, --file string Path to the Terraform and OpenTofu file (required)
86-
-h, --help help for kuzco
87-
-m, --model string LLM model to use for generating recommendations (default "llama3.2")
88-
-p, --prompt string User prompt for guiding the response format of the LLM model
89-
-t, --tool terraform Specifies the configuration tooling for configurations. Valid values include: terraform and opentofu (default "terraform")
86+
-h, --help help for kuzco
87+
88+
Use "kuzco [command] --help" for more information about a command.
9089
```
9190

9291
## Contributing

cmd/docs.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/spf13/cobra/doc"
66
)
77

8-
// Generate documentation for Terramaid commands and output to docs directory
8+
// Generate documentation for Kuzco commands and output to docs directory
99
var docsCmd = &cobra.Command{
1010
Use: "docs",
1111
Short: "Generate documentation for the CLI",
@@ -20,7 +20,3 @@ var docsCmd = &cobra.Command{
2020
return nil
2121
},
2222
}
23-
24-
func init() {
25-
rootCmd.AddCommand(docsCmd)
26-
}

cmd/fix.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

cmd/recommend.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cmd
2+
3+
import (
4+
"github.com/RoseSecurity/kuzco/internal"
5+
u "github.com/RoseSecurity/kuzco/pkg/utils"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var recommendCmd = &cobra.Command{
10+
Use: "recommend",
11+
Short: "Intelligently analyze your Terraform and OpenTofu configurations",
12+
Long: `Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations for boosting efficiency, security, and performance.`,
13+
Run: Analyze,
14+
}
15+
16+
func init() {
17+
recommendCmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the Terraform and OpenTofu file (required)")
18+
recommendCmd.Flags().StringVarP(&tool, "tool", "t", "terraform", "Specifies the configuration tooling for configurations. Valid values include: `terraform` and `opentofu`")
19+
recommendCmd.Flags().StringVarP(&model, "model", "m", "llama3.2", "LLM model to use for generating recommendations")
20+
recommendCmd.Flags().StringVarP(&prompt, "prompt", "p", "", "User prompt for guiding the response format of the LLM model")
21+
recommendCmd.Flags().StringVarP(&addr, "address", "a", "http://localhost:11434", "IP Address and port to use for the LLM model (ex: http://localhost:11434)")
22+
}
23+
24+
func Analyze(cmd *cobra.Command, args []string) {
25+
// Validate that the specified model exists in Ollama
26+
if err := internal.ValidateModel(model, addr); err != nil {
27+
u.LogErrorAndExit(err)
28+
}
29+
30+
// Proceed with the main logic if all required flags are set
31+
if err := internal.Run(filePath, tool, model, prompt, addr); err != nil {
32+
u.LogErrorAndExit(err)
33+
}
34+
}

cmd/root.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/RoseSecurity/kuzco/internal"
87
tuiUtils "github.com/RoseSecurity/kuzco/internal/tui/utils"
@@ -21,17 +20,15 @@ var (
2120
var rootCmd = &cobra.Command{
2221
Use: "kuzco",
2322
Short: "Intelligently analyze your Terraform and OpenTofu configurations",
24-
Long: `Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations for boosting efficiency, security, and performance.`,
23+
Long: `Intelligently analyze your Terraform and OpenTofu configurations to receive personalized recommendations and fixes for boosting efficiency, security, and performance.`,
2524
Run: runAnalyzer,
2625
}
2726

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

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

5148
// Validate that the specified model exists in Ollama
5249
if err := internal.ValidateModel(model, addr); err != nil {
53-
fmt.Fprintf(os.Stderr, "Model validation error: %v\n", err)
54-
os.Exit(1)
55-
}
56-
57-
// Proceed with the main logic if all required flags are set
58-
if err := internal.Run(filePath, tool, model, prompt, addr); err != nil {
59-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
60-
os.Exit(1)
50+
u.LogErrorAndExit(err)
6151
}
52+
cmd.Help()
6253
}
6354

6455
func Execute() {
6556
if err := rootCmd.Execute(); err != nil {
66-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
67-
os.Exit(1)
57+
u.LogErrorAndExit(err)
6858
}
6959
}

cmd/version.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99

1010
"github.com/fatih/color"
1111
"github.com/spf13/cobra"
12+
"golang.org/x/mod/semver"
1213
)
1314

14-
var Version = "0.2.0"
15+
// Placeholder for builds
16+
var Version = "1.0.0"
1517

1618
type Release struct {
1719
TagName string `json:"tag_name"`
@@ -28,8 +30,8 @@ var versionCmd = &cobra.Command{
2830
if err == nil && latestReleaseTag != "" {
2931
latestRelease := strings.TrimPrefix(latestReleaseTag, "v")
3032
currentRelease := strings.TrimPrefix(Version, "v")
31-
if latestRelease != currentRelease {
32-
updateTerramaid(latestRelease)
33+
if semver.Compare(latestRelease, currentRelease) > 0 {
34+
updateKuzco(latestRelease)
3335
}
3436
}
3537
},
@@ -57,8 +59,8 @@ func latestRelease() (string, error) {
5759
}
5860

5961
// Display out of date warning
60-
func updateTerramaid(latestVersion string) {
62+
func updateKuzco(latestVersion string) {
6163
c1 := color.New(color.FgCyan)
6264

63-
c1.Println(fmt.Sprintf("\nYour version of Terramaid is out of date. The latest version is %s\n\n", latestVersion))
65+
c1.Println(fmt.Sprintf("\nYour version of Kuzco is out of date. The latest version is %s\n\n", latestVersion))
6466
}

docs/kuzco.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## kuzco
22

3-
Intelligently analyze your Terraform configurations
3+
Intelligently analyze your Terraform and OpenTofu configurations
44

55
### Synopsis
66

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

99
```
1010
kuzco [flags]
@@ -13,15 +13,14 @@ kuzco [flags]
1313
### Options
1414

1515
```
16-
-a, --address string IP Address and port to use for the LLM model (ex: http://localhost:11434) (default "http://localhost:11434")
17-
-f, --file string Path to the Terraform file (required)
18-
-h, --help help for kuzco
19-
-m, --model string LLM model to use for generating recommendations (default "llama3.1")
16+
-h, --help help for kuzco
2017
```
2118

2219
### SEE ALSO
2320

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

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

docs/kuzco_completion.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ See each sub-command's help for details on how to use the generated script.
1616

1717
### SEE ALSO
1818

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

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

docs/kuzco_completion_bash.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ kuzco completion bash
4141

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

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

docs/kuzco_completion_fish.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ kuzco completion fish [flags]
3232

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

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

0 commit comments

Comments
 (0)