Skip to content

Commit

Permalink
feat: not display module with --unused-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sylwit committed Mar 11, 2023
1 parent ba33679 commit c2445e6
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,46 @@ func rootCmdExec(cmd *cobra.Command, args []string) error {
return err
}

for path, _ := range modules {
for path := range modules {
stats, err := findVariablesUsage(path)
if err != nil {
return err
}
fmt.Printf("Module: %s (%d variables found)\n", path, len(stats.variables))

for name, count := range stats.variables {
if fUnusedOnly && count > 0 {
if fUnusedOnly {
for name, count := range stats.variables {
if count > 0 {
delete(stats.variables, name)
}
}
if len(stats.variables) == 0 {
continue
}
fmt.Printf("%s : %d\n", name, count)
}
fmt.Println("")

err = displayModule(path, &stats)
if err != nil {
return err
}

}

fmt.Printf("%d modules processed", len(modules))

return nil
}

func displayModule(path string, stats *VariablesUsage) error {
fmt.Printf("Module: %s (%d variables found)\n", path, len(stats.variables))

for name, count := range stats.variables {
fmt.Printf("%s : %d\n", name, count)
}
fmt.Println("")

return nil
}

type VariablesUsage struct {
variables map[string]int
}
Expand Down Expand Up @@ -91,7 +110,7 @@ func countVariables(path string, tfconfig *tfconfig.Module) (map[string]int, err

content := string(data)

for variable, _ := range tfconfig.Variables {
for variable := range tfconfig.Variables {
regex := regexp.MustCompile(fmt.Sprintf(`var\.%s\W`, variable))
matches := regex.FindAllStringIndex(content, -1)

Expand Down

0 comments on commit c2445e6

Please # to comment.