Skip to content

Instantly share code, notes, and snippets.

@nicolai86
Created December 22, 2021 17:05
Show Gist options
  • Save nicolai86/c76215e28153a79e53d49e26d6879a0f to your computer and use it in GitHub Desktop.
Save nicolai86/c76215e28153a79e53d49e26d6879a0f to your computer and use it in GitHub Desktop.
linter ensuring resources are snake_cased
package main
import (
"fmt"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"log"
"os"
"strings"
)
func checkForViolations(dir string) ([]string, error) {
module, diags := tfconfig.LoadModule(dir)
if diags.HasErrors() {
return nil, diags.Err()
}
violations := []string{}
for name, module := range module.ModuleCalls {
nested, err := checkForViolations(dir + "/" + module.Source)
if err != nil {
return nil, fmt.Errorf("failed checking module %s: %w", name, err)
}
for _, violation := range nested {
violations = append(violations, fmt.Sprintf("module.%s.%s", name, violation))
}
}
for name, resource := range module.ManagedResources {
if strings.Contains(name, "-") {
violations = append(violations, fmt.Sprintf("%s.%s", resource.Type, resource.Name))
}
}
return violations, nil
}
func main() {
dir := os.Args[1]
violations, err := checkForViolations(dir)
if err != nil {
log.Fatalf("failed to check for violations: %s\n", err)
}
for _, violation := range violations {
fmt.Printf("%s is not snake_cased\n", violation)
}
if len(violations) > 0 {
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment