Skip to content

Commit

Permalink
Use defer to unlock mutex (#1186)
Browse files Browse the repository at this point in the history
Also
- remove some unused code
- run `npm install` as part of `build/do.rq pr`
- add `build/node_modules` to `.gitignore`

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Oct 8, 2024
1 parent 0af7c91 commit 24f0fd7
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ dist/
# ends up unless the client supports presenting it in a different way.
input.json
output.json

build/node_modules/
1 change: 1 addition & 0 deletions build/do.rq
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ job contains "tasks" if {
# - https://github.com/open-policy-agent/opa
job contains "pr" if {
run("go mod tidy")
run("npm install --silent --prefix build")

build(true)

Expand Down
3 changes: 2 additions & 1 deletion pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,8 @@ func (l Linter) lintWithRegoRules(ctx context.Context, input rules.Input) (repor
}

mu.Lock()
defer mu.Unlock()

regoReport.Violations = append(regoReport.Violations, result.Violations...)
regoReport.Notices = append(regoReport.Notices, result.Notices...)

Expand All @@ -897,7 +899,6 @@ func (l Linter) lintWithRegoRules(ctx context.Context, input rules.Input) (repor
if l.profiling {
regoReport.AddProfileEntries(result.AggregateProfile)
}
mu.Unlock()
}(name)
}

Expand Down
54 changes: 0 additions & 54 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ package report

import (
"fmt"
"io"
"sort"
"strconv"
"strings"
"time"

"github.com/olekukonko/tablewriter"
)

// RelatedResource provides documentation on a violation.
Expand Down Expand Up @@ -167,54 +161,6 @@ func (r *Report) AggregateProfileToSortedProfile(numResults int) {
r.Profile = r.Profile[:numResults]
}

// TODO: This does not belong here and is only for internal testing purposes at this point in time. Profile reports are
// currently only publicly available for the JSON reporter. Some variation of this will eventually be moved to the table
// reporter. (this code borrowed from OPA).
func (r Report) printProfile(w io.Writer) { //nolint:unused
tableProfile := generateTableProfile(w)

for i, rs := range r.Profile {
timeNs := time.Duration(rs.TotalTimeNs) * time.Nanosecond
line := []string{
timeNs.String(),
strconv.Itoa(rs.NumEval),
strconv.Itoa(rs.NumRedo),
strconv.Itoa(rs.NumGenExpr),
rs.Location,
}
tableProfile.Append(line)

if i == 0 {
tableProfile.SetFooter([]string{"", "", "", "", ""})
}
}

if tableProfile.NumLines() > 0 {
tableProfile.Render()
}
}

func generateTableWithKeys(writer io.Writer, keys ...string) *tablewriter.Table { //nolint:unused
table := tablewriter.NewWriter(writer)
aligns := make([]int, 0, len(keys))
hdrs := make([]string, 0, len(keys))

for _, k := range keys {
hdrs = append(hdrs, strings.Title(k)) //nolint:staticcheck // SA1019, no unicode here
aligns = append(aligns, tablewriter.ALIGN_LEFT)
}

table.SetHeader(hdrs)
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.SetColumnAlignment(aligns)

return table
}

func generateTableProfile(writer io.Writer) *tablewriter.Table { //nolint:unused
return generateTableWithKeys(writer, "Time", "Num Eval", "Num Redo", "Num Gen Expr", "Location")
}

// ViolationsFileCount returns the number of files containing violations.
func (r Report) ViolationsFileCount() map[string]int {
fc := map[string]int{}
Expand Down

0 comments on commit 24f0fd7

Please sign in to comment.