Skip to content

Commit

Permalink
Merge pull request #15 from padok-team/feat/prepare-release
Browse files Browse the repository at this point in the history
ci: try fixing goreleaser
  • Loading branch information
cterence authored Sep 29, 2023
2 parents 870cc58 + 5d562ec commit 38ceb89
Show file tree
Hide file tree
Showing 20 changed files with 409 additions and 286 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ name: goreleaser

on:
push:
branches:
- main
tags-ignore:
- "*" # ignore all tags
tags:
- "*"

permissions:
contents: write
Expand All @@ -23,11 +21,8 @@ jobs:
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --clean --nightly
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
5 changes: 0 additions & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ checksum:
name_template: "checksums.txt"
snapshot:
name_template: "{{ .Tag }}-next"
nightly:
name_template: "{{ .ShortCommit }}"
tag_name: dev
publish_release: true
keep_single_release: true
changelog:
sort: asc
use: github
Expand Down
2 changes: 1 addition & 1 deletion checks/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"
)

func All(layers []data.Layer) []data.Check {
func All(layers []*data.Layer) []data.Check {
// List of checks to perform

var checkResults []data.Check
Expand Down
6 changes: 3 additions & 3 deletions checks/iterate_no_use_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ type ResourceChanges struct {
ResourceChanges []ResourceChange `json:"resource_changes"`
}

func IterateNoUseCount(layers []data.Layer) (data.Check, error) {
func IterateNoUseCount(layers []*data.Layer) (data.Check, error) {
name := "Don't use count to create multiple resources"
// relatedGuidelines := "https://padok-team.github.io/docs-terraform-guidelines/terraform/iterate_on_your_resources.html#list-iteration-count"
relatedGuidelines := "http://bitly.ws/K5WA"
relatedGuidelines := "https://t.ly/_P8pN"
status := "✅"
errors := []string{}

Expand All @@ -42,7 +42,7 @@ func IterateNoUseCount(layers []data.Layer) (data.Check, error) {
go func(layer *data.Layer) {
defer wg.Done()
c <- checkLayer(layer)
}(&layers[i])
}(layers[i])
}

wg.Wait()
Expand Down
6 changes: 3 additions & 3 deletions checks/plan_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"sync"
)

func PlanChecks(layers []data.Layer) []data.Check {
func PlanChecks(layers []*data.Layer) []data.Check {
// Add plan checks here
checks := map[string]func([]data.Layer) (data.Check, error){
checks := map[string]func([]*data.Layer) (data.Check, error){
"IterateNoUseCount": IterateNoUseCount,
"RefreshTime": RefreshTime,
}
Expand All @@ -21,7 +21,7 @@ func PlanChecks(layers []data.Layer) []data.Check {
defer close(c)

for _, checkFunction := range checks {
go func(checkFunction func([]data.Layer) (data.Check, error)) {
go func(checkFunction func([]*data.Layer) (data.Check, error)) {
defer wg.Done()

check, err := checkFunction(layers)
Expand Down
183 changes: 56 additions & 127 deletions checks/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,28 @@ package checks
import (
"fmt"
"guacamole/data"
"math"
"sort"
"strconv"
"strings"
"sync"

"github.com/fatih/color"
"golang.org/x/exp/slices"
)

func Profile(layers []data.Layer, verbose bool) {
wg := new(sync.WaitGroup)

wg.Add(len(layers))

for i := range layers {
go func(layer *data.Layer) {
defer wg.Done()
layer.BuildRootModule()
}(&layers[i])
func Profile(codebase data.Codebase, verbose bool) {
codebase.BuildLayers()
codebase.ComputeStats()
// Take the length of the longest number of objects in a layer for padding
if len(codebase.Layers) == 0 {
fmt.Println("No layers found")
return
}

wg.Wait()

padding := len(strconv.Itoa(layers[0].RootModule.CumulatedSize.Resources + layers[0].RootModule.CumulatedSize.Datasources))
padding := len(strconv.Itoa(codebase.Layers[0].RootModule.Stats.CumulatedSize.Resources + codebase.Layers[0].RootModule.Stats.CumulatedSize.Datasources))
if padding%2 == 0 {
padding += 4
} else {
padding += 3
}

codebaseSizes := data.Size{
Resources: 0,
Datasources: 0,
Modules: 0,
}

codebaseStats := data.Stats{
DistinctResourceTypes: make(map[string]int),
DistinctDatasourceTypes: make(map[string]int),
Depth: 0,
}

var biggestChild data.Module

// Display a legend
fmt.Println("Legend:")
c := color.New(color.FgWhite).Add(color.Bold)
Expand All @@ -60,150 +37,102 @@ func Profile(layers []data.Layer, verbose bool) {
c.Printf(" Datasource / Resource name\n\n")
c = color.New(color.FgYellow).Add(color.Bold)
c.Println("Profiling by layer:")
for _, layer := range layers {
for _, layer := range codebase.Layers {
fmt.Println(strings.Repeat("-", 50))
c = color.New(color.FgYellow).Add(color.Bold)
c.Printf("%s\n", layer.Name)

if layer.RootModule.CumulatedSize.Resources+layer.RootModule.CumulatedSize.Datasources > 0 {
// If the layer has no object in state, we don't want to display it
if layer.RootModule.Stats.CumulatedSize.Resources+layer.RootModule.Stats.CumulatedSize.Datasources > 0 {
printTree(layer.RootModule, padding, verbose)
} else {
fmt.Println(" -- Layer has no object in state --")
}

codebaseSizes.Resources += layer.RootModule.CumulatedSize.Resources
codebaseSizes.Datasources += layer.RootModule.CumulatedSize.Datasources
codebaseSizes.Modules += layer.RootModule.CumulatedSize.Modules

stats := layer.RootModule.ComputeStats()

// Update codebase stats
for k, v := range stats.DistinctDatasourceTypes {
codebaseStats.DistinctDatasourceTypes[k] += v
}
for k, v := range stats.DistinctResourceTypes {
codebaseStats.DistinctResourceTypes[k] += v
}

for _, c := range layer.RootModule.Children {
if c.CumulatedSize.Resources+c.CumulatedSize.Datasources > biggestChild.CumulatedSize.Resources+biggestChild.CumulatedSize.Datasources {
biggestChild = c
}
}

codebaseStats.Depth = int(math.Max(float64(codebaseStats.Depth), float64(stats.Depth)))

dKeys := make([]string, 0, len(stats.DistinctDatasourceTypes))
for k := range stats.DistinctDatasourceTypes {
dKeys = append(dKeys, k)
}

sort.SliceStable(dKeys, func(i, j int) bool {
if stats.DistinctDatasourceTypes[dKeys[i]] == stats.DistinctDatasourceTypes[dKeys[j]] {
return strings.Compare(dKeys[i], dKeys[j]) < 0
}
return stats.DistinctDatasourceTypes[dKeys[i]] > stats.DistinctDatasourceTypes[dKeys[j]]
})

rKeys := make([]string, 0, len(stats.DistinctResourceTypes))
for k := range stats.DistinctResourceTypes {
rKeys = append(rKeys, k)
}

sort.SliceStable(rKeys, func(i, j int) bool {
if stats.DistinctResourceTypes[rKeys[i]] == stats.DistinctResourceTypes[rKeys[j]] {
return strings.Compare(rKeys[i], rKeys[j]) < 0
}
return stats.DistinctResourceTypes[rKeys[i]] > stats.DistinctResourceTypes[rKeys[j]]
})

c = color.New(color.FgWhite).Add(color.Bold)
c.Printf("\nStats:\n")
c = color.New(color.FgBlue).Add(color.Bold)
c.Printf(" %s %d\n", "Datasources:", layer.RootModule.CumulatedSize.Datasources)
c.Printf(" %s %d\n", "Datasources:", layer.RootModule.Stats.CumulatedSize.Datasources)
c = color.New(color.FgBlue)
c.Printf(" %-14s %d\n", "Distinct types:", len(stats.DistinctDatasourceTypes))
c.Printf(" %-14s %d\n", "Distinct types:", len(layer.RootModule.Stats.DistinctDatasourceTypes))
if verbose {
for _, k := range dKeys {
c.Printf(" [%d] %s\n", stats.DistinctDatasourceTypes[k], k)
for k, v := range layer.RootModule.Stats.DistinctDatasourceTypes {
c.Printf(" [%d] %s\n", v, k)
}
}
c = color.New(color.FgGreen).Add(color.Bold)
c.Printf(" %s %d\n", "Resources:", layer.RootModule.CumulatedSize.Resources)
c.Printf(" %s %d\n", "Resources:", layer.RootModule.Stats.CumulatedSize.Resources)
c = color.New(color.FgGreen)
c.Printf(" %-14s %d\n", "Distinct types:", len(stats.DistinctResourceTypes))
c.Printf(" %-14s %d\n", "Distinct types:", len(layer.RootModule.Stats.DistinctResourceTypes))
if verbose {
for _, k := range rKeys {
c.Printf(" [%d] %s\n", stats.DistinctResourceTypes[k], k)
for k, v := range layer.RootModule.Stats.DistinctResourceTypes {
c.Printf(" [%d] %s\n", v, k)
}
}
c = color.New(color.FgWhite).Add(color.Bold)
c.Printf(" %s\n", "Modules:")
c = color.New(color.FgWhite)
c.Printf(" %-12s %d\n", "Module depth:", stats.Depth)
}

dgKeys := make([]string, 0, len(codebaseStats.DistinctDatasourceTypes))
for k := range codebaseStats.DistinctDatasourceTypes {
dgKeys = append(dgKeys, k)
}

sort.SliceStable(dgKeys, func(i, j int) bool {
if codebaseStats.DistinctDatasourceTypes[dgKeys[i]] == codebaseStats.DistinctDatasourceTypes[dgKeys[j]] {
return strings.Compare(dgKeys[i], dgKeys[j]) < 0
}
return codebaseStats.DistinctDatasourceTypes[dgKeys[i]] > codebaseStats.DistinctDatasourceTypes[dgKeys[j]]
})

rgKeys := make([]string, 0, len(codebaseStats.DistinctResourceTypes))
for k := range codebaseStats.DistinctResourceTypes {
rgKeys = append(rgKeys, k)
c.Printf(" %-12s %d\n", "Module depth:", layer.RootModule.Stats.Depth)
}

sort.SliceStable(rgKeys, func(i, j int) bool {
if codebaseStats.DistinctResourceTypes[rgKeys[i]] == codebaseStats.DistinctResourceTypes[rgKeys[j]] {
return strings.Compare(rgKeys[i], rgKeys[j]) < 0
}
return codebaseStats.DistinctResourceTypes[rgKeys[i]] > codebaseStats.DistinctResourceTypes[rgKeys[j]]
})

fmt.Println(strings.Repeat("-", 50))
c = color.New(color.FgWhite).Add(color.Bold)
c.Println("Codebase stats:")
c = color.New(color.FgBlue).Add(color.Bold)
c.Printf(" %-12s %d\n", "Datasources:", codebaseSizes.Datasources)
c.Printf(" %-12s %d\n", "Datasources:", codebase.Stats.Size.Datasources)
c = color.New(color.FgBlue)
c.Printf(" %-12s %d\n", "Distinct types:", len(codebaseStats.DistinctDatasourceTypes))
c.Printf(" %-12s %d\n", "Distinct types:", len(codebase.Stats.DistinctDatasourceTypes))

if verbose {
for _, k := range dgKeys {
numberString := "[" + strconv.Itoa(codebaseStats.DistinctDatasourceTypes[k]) + "]"
for k, v := range codebase.Stats.DistinctDatasourceTypes {
numberString := "[" + strconv.Itoa(v) + "]"
c.Printf("%10s %s\n", numberString, k)
}
}
c = color.New(color.FgGreen).Add(color.Bold)
c.Printf(" %-12s %d\n", "Resources:", codebaseSizes.Resources)
c.Printf(" %-12s %d\n", "Resources:", codebase.Stats.Size.Resources)
c = color.New(color.FgGreen)
c.Printf(" %-12s %d\n", "Distinct types:", len(codebaseStats.DistinctResourceTypes))
c.Printf(" %-12s %d\n", "Distinct types:", len(codebase.Stats.DistinctResourceTypes))
if verbose {
for _, k := range rgKeys {
numberString := "[" + strconv.Itoa(codebaseStats.DistinctResourceTypes[k]) + "]"
for k, v := range codebase.Stats.DistinctResourceTypes {
numberString := "[" + strconv.Itoa(v) + "]"
c.Printf("%10s %s\n", numberString, k)
}
}
c = color.New(color.FgWhite).Add(color.Bold)
c.Printf(" %-12s %d\n", "Modules:", codebaseSizes.Modules)
c.Printf(" %-12s %d\n", "Modules:", codebase.Stats.Size.Modules)
c = color.New(color.FgWhite)
c.Printf(" %-12s %d\n", "Max module depth:", codebaseStats.Depth)
c.Printf(" %-12s %d\n", "Max module depth:", codebase.Stats.Depth)
// Show biggest layer
c.Printf(" %s [%d] %s\n", "Biggest layer:", codebase.Stats.BiggestLayer.RootModule.Stats.CumulatedSize.Resources+codebase.Stats.BiggestLayer.RootModule.Stats.CumulatedSize.Datasources, codebase.Stats.BiggestLayer.Name)
// Show biggest children
c.Printf(" %s [%d] %s\n", "Biggest child module:", biggestChild.CumulatedSize.Resources+biggestChild.CumulatedSize.Datasources, biggestChild.Address)
c.Printf(" %s [%d] %s\n", "Biggest child module:", codebase.Stats.BiggestChildModule.Stats.CumulatedSize.Resources+codebase.Stats.BiggestChildModule.Stats.CumulatedSize.Datasources, codebase.Stats.BiggestChildModule.Address)
// Show warnings
c = color.New(color.FgYellow).Add(color.Bold)
c.Printf(" %s\n", "Warnings:")
for _, l := range codebase.Layers {
l.ComputeWarnings()
if len(l.Warnings.DatasourceInModuleWarning) > 0 {
c = color.New(color.FgYellow)
c.Printf(" Datasource(s) in module(s) in layer : %s\n", l.Name)
for _, w := range l.Warnings.DatasourceInModuleWarning {
c = color.New(color.FgWhite)
c.Printf(" %s\n", w.Module.Address)
for _, d := range w.Datasource {
c = color.New(color.FgBlue)
c.Printf(" %s\n", d.Type)
}
}
}
}
}

func printTree(m data.Module, padding int, verbose bool) {
func printTree(m *data.Module, padding int, verbose bool) {
c := color.New(color.FgWhite).Add(color.Bold)
totalSizeString := "[" + strconv.Itoa(m.CumulatedSize.Resources+m.CumulatedSize.Datasources) + "]"
c.Printf("%*s %s\n", padding, totalSizeString, m.Address)
totalSizeString := "[" + strconv.Itoa(m.Stats.CumulatedSize.Resources+m.Stats.CumulatedSize.Datasources) + "]"
c.Printf("%*s %s\n", padding, totalSizeString, m.Name)
// Sort objects first by kind, then by name
slices.SortFunc(m.ObjectTypes, func(i, j data.ObjectType) int {
slices.SortFunc(m.ObjectTypes, func(i, j *data.ObjectType) int {
if i.Kind == j.Kind {
return strings.Compare(i.Type, j.Type)
}
Expand Down
2 changes: 1 addition & 1 deletion checks/provider_in_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func ProviderInModule() (data.Check, error) {
name := "No provider in module"
// relatedGuidelines := "https://padok-team.github.io/docs-terraform-guidelines/terraform/donts.html#using-provider-block-in-modules"
relatedGuidelines := "http://bitly.ws/K5Wa"
relatedGuidelines := "https://t.ly/7KW3N"
// Find recusively all the modules in the current directory
modules, err := helpers.GetModules()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions checks/refresh_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"guacamole/data"
)

func RefreshTime(layers []data.Layer) (data.Check, error) {
func RefreshTime(layers []*data.Layer) (data.Check, error) {
checkResult := data.Check{
Name: "Layers' refresh time",
Status: "✅",
// RelatedGuidelines: "https://github.com/padok-team/docs-terraform-guidelines/blob/main/terraform/wysiwg_patterns.md",
RelatedGuidelines: "http://bitly.ws/K5VV",
RelatedGuidelines: "https://t.ly/8d1lc",
Errors: []string{},
}

Expand Down
Loading

0 comments on commit 38ceb89

Please sign in to comment.