Skip to content

Commit

Permalink
feat(errors): management
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Sanvoisin committed Jun 30, 2023
1 parent 330fe2a commit 356c8eb
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
9 changes: 8 additions & 1 deletion checks/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Checks() {
// Displaying the results
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Check", "Status", "Related guidelines"})
t.AppendHeader(table.Row{"#", "Check", "Status", "Related guideline", "Errors"})
for i, check := range listOfChecks {
c := check()
if c.Status == "✅" {
Expand All @@ -32,6 +32,13 @@ func Checks() {
t.AppendRows([]table.Row{
{i + 1, c.Name, c.Status, c.RelatedGuidelines},
})
if len(c.Errors) > 0 {
for _, err := range c.Errors {
t.AppendRows([]table.Row{
{"", "", "", err},
})
}
}
}
score := strconv.Itoa(totalChecksOk*100/totalChecks) + "%"
if score == "100%" {
Expand Down
9 changes: 4 additions & 5 deletions checks/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (
func ProviderInModule() data.Check {
name := "No provider in module"
relatedGuidelines := "https://padok-team.github.io/docs-terraform-guidelines/terraform/donts.html#using-provider-block-in-modules"
fmt.Println("Checking none prescence provider in module...")
// Find recusively all the modules in the current directory
modules, err := helpers.GetModules()
if err != nil {
fmt.Println("Error:", err)
}
modulesInError := []data.Module{}
modulesInError := []string{}
// For each module, check if the provider is defined
for _, module := range modules {
moduleConf, diags := tfconfig.LoadModule(module.FullPath)
Expand All @@ -26,9 +25,7 @@ func ProviderInModule() data.Check {
}
//If the module has no provider, display an error
if len(moduleConf.ProviderConfigs) > 0 {
fmt.Println("Error: provider found")
fmt.Println("Module:", module.FullPath)
modulesInError = append(modulesInError, module)
modulesInError = append(modulesInError, module.FullPath)
}

}
Expand All @@ -37,11 +34,13 @@ func ProviderInModule() data.Check {
Name: name,
RelatedGuidelines: relatedGuidelines,
Status: "❌",
Errors: modulesInError,
}
}
return data.Check{
Name: name,
RelatedGuidelines: relatedGuidelines,
Status: "✅",
Errors: modulesInError,
}
}
7 changes: 3 additions & 4 deletions checks/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
func Naming() data.Check {
name := "Stuttering in the naming of the resources"
relatedGuidelines := "https://padok-team.github.io/docs-terraform-guidelines/terraform/terraform_naming.html#resource-andor-data-source-naming"
fmt.Println("Checking naming convention...")
modules, err := helpers.GetModules()
if err != nil {
fmt.Println("Error:", err)
Expand All @@ -27,9 +26,7 @@ func Naming() data.Check {
//Check if the name of the resource is not a duplicate of its type
for _, resource := range moduleConf.ManagedResources {
if strings.Contains(resource.Type, resource.Name) {
namesInError = append(namesInError, resource.Name)
fmt.Println("Error: The name of the resource '" + resource.Name + "' is contained in its type '" + resource.Type + "'")
fmt.Println(resource.Pos.Filename)
namesInError = append(namesInError, resource.Pos.Filename+" --> "+resource.MapKey())
}
}

Expand All @@ -39,11 +36,13 @@ func Naming() data.Check {
Name: name,
RelatedGuidelines: relatedGuidelines,
Status: "❌",
Errors: namesInError,
}
}
return data.Check{
Name: name,
RelatedGuidelines: relatedGuidelines,
Status: "✅",
Errors: namesInError,
}
}
1 change: 1 addition & 0 deletions data/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Check struct {
Name string
Status string
RelatedGuidelines string
Errors []string
}

type Module struct {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/savioxavier/termlink v1.3.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/savioxavier/termlink v1.3.0 h1:3Gl4FzQjUyiHzmoEDfmWEhgIwDiJY4poOQHP+k8ReA4=
github.com/savioxavier/termlink v1.3.0/go.mod h1:5T5ePUlWbxCHIwyF8/Ez1qufOoGM89RCg9NvG+3G3gc=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
Expand Down

0 comments on commit 356c8eb

Please sign in to comment.