Skip to content

Commit

Permalink
feat: add 2 new checks
Browse files Browse the repository at this point in the history
  • Loading branch information
cterence committed Sep 29, 2023
1 parent e89d4e7 commit 96279c3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 3 deletions.
59 changes: 59 additions & 0 deletions checks/collection_var_name_plural.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package checks

import (
"fmt"
"guacamole/data"
"guacamole/helpers"
"strconv"
"strings"

pluralize "github.com/gertd/go-pluralize"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
)

func CollectionVarNamePlural() (data.Check, error) {
name := "Collection variable name must be plural"
relatedGuidelines := "https://t.ly/A7P5j"
modules, err := helpers.GetModules()
if err != nil {
return data.Check{}, err
}
variablesInError := []string{}

// For each module, check if the provider is defined
for _, module := range modules {
moduleConf, diags := tfconfig.LoadModule(module.FullPath)
if diags.HasErrors() {
return data.Check{}, diags.Err()
}

// Check if the name of the resource is not in snake case
for _, variable := range moduleConf.Variables {
// I want to check if the name of the resource contains any word (separated by a dash) of its type

// Check if prefix is "list"

if strings.HasPrefix(variable.Type, "list") || strings.HasPrefix(variable.Type, "set") || strings.HasPrefix(variable.Type, "map") {
// Check if the variable name is plural
pluralize := pluralize.NewClient()
fmt.Println(variable.Name, pluralize.IsPlural(variable.Name))
if !pluralize.IsPlural(variable.Name) {
variablesInError = append(variablesInError, variable.Pos.Filename+":"+strconv.Itoa(variable.Pos.Line)+" --> "+variable.Name)
}
}
}
}

dataCheck := data.Check{
Name: name,
RelatedGuidelines: relatedGuidelines,
Status: "✅",
Errors: variablesInError,
}

if len(variablesInError) > 0 {
dataCheck.Status = "❌"
}

return dataCheck, nil
}
50 changes: 50 additions & 0 deletions checks/missing_var_description.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package checks

import (
"guacamole/data"
"guacamole/helpers"
"strconv"

"github.com/hashicorp/terraform-config-inspect/tfconfig"
)

func MissingVarDescription() (data.Check, error) {
name := "Missing description for variables"
relatedGuidelines := "https://t.ly/vKG6L"
modules, err := helpers.GetModules()
if err != nil {
return data.Check{}, err
}
variablesInError := []string{}

// For each module, check if the provider is defined
for _, module := range modules {
moduleConf, diags := tfconfig.LoadModule(module.FullPath)

if diags.HasErrors() {
return data.Check{}, diags.Err()
}

// Check if the name of the resource is not in snake case
for _, variable := range moduleConf.Variables {
// I want to check if the name of the resource contains any word (separated by a dash) of its type

if variable.Description == "" {
variablesInError = append(variablesInError, variable.Pos.Filename+":"+strconv.Itoa(variable.Pos.Line)+" --> "+variable.Name)
}
}
}

dataCheck := data.Check{
Name: name,
RelatedGuidelines: relatedGuidelines,
Status: "✅",
Errors: variablesInError,
}

if len(variablesInError) > 0 {
dataCheck.Status = "❌"
}

return dataCheck, nil
}
8 changes: 5 additions & 3 deletions checks/static_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
func StaticChecks() []data.Check {
// Add static checks here
checks := map[string]func() (data.Check, error){
"ProviderInModule": ProviderInModule,
"Stuttering": Stuttering,
"SnakeCase": SnakeCase,
"ProviderInModule": ProviderInModule,
"Stuttering": Stuttering,
"SnakeCase": SnakeCase,
"MissingVarDescription": MissingVarDescription,
"CollectionVarNamePlural": CollectionVarNamePlural,
}

var checkResults []data.Check
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/fatih/color v1.15.0
github.com/gertd/go-pluralize v0.2.1
github.com/hashicorp/terraform-config-inspect v0.0.0-20230925220900-5a6f8d18746d
github.com/hashicorp/terraform-exec v0.19.0
github.com/hashicorp/terraform-json v0.17.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBD
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA=
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
github.com/go-git/go-billy/v5 v5.4.1 h1:Uwp5tDRkPr+l/TnbHOQzp+tmJfLceOlbVucgpTz8ix4=
github.com/go-git/go-git/v5 v5.8.1 h1:Zo79E4p7TRk0xoRgMq0RShiTHGKcKI4+DI6BfJc/Q+A=
Expand Down

0 comments on commit 96279c3

Please sign in to comment.