Skip to content

Commit

Permalink
Merge pull request #636 from tailwarden/chore/go-lint
Browse files Browse the repository at this point in the history
chore: add golangci-lint configuration
  • Loading branch information
mlabouardy committed Mar 17, 2023
2 parents 49f3a2f + 5858d84 commit 5526131
Show file tree
Hide file tree
Showing 36 changed files with 301 additions and 121 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/build_test_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ jobs:
with:
go-version: 1.20.2

# FIXME: https://github.com/golangci/golangci-lint-action/issues/677
# - name: golangci-lint
# uses: golangci/golangci-lint-action@v3
# with:
# skip-pkg-cache: true

- name: Install dependencies
run: go mod download

- name: golangci-lint
uses: golangci/golangci-lint-action@v3

- name: Test
run: make test

Expand Down
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
run:
timeout: 5m
skip-dirs:
- dashboard
10 changes: 8 additions & 2 deletions handlers/csv_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func (handler *ApiHandler) DownloadInventoryCSVForView(w http.ResponseWriter, r
resources := make([]models.Resource, 0)

if len(view.Filters) == 0 {
handler.db.NewRaw("SELECT * FROM resources").Scan(handler.ctx, &resources)
err := handler.db.NewRaw("SELECT * FROM resources").Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Errorf("select failed")
}
respondWithCSVDownload(resources, w, r)
return
}
Expand Down Expand Up @@ -208,7 +211,10 @@ func (handler *ApiHandler) DownloadInventoryCSVForView(w http.ResponseWriter, r
}
}

handler.db.NewRaw(query).Scan(handler.ctx, &resources)
err = handler.db.NewRaw(query).Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Errorf("scan failed")
}
} else {
query := fmt.Sprintf("SELECT * FROM resources WHERE %s ORDER BY id", whereClause)
if len(view.Exclude) > 0 {
Expand Down
36 changes: 29 additions & 7 deletions handlers/dashboard_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/utils"
)
Expand All @@ -17,25 +18,37 @@ func (handler *ApiHandler) DashboardStatsHandler(w http.ResponseWriter, r *http.
Count int `bun:"count" json:"total"`
}{}

handler.db.NewRaw("SELECT COUNT(*) as count FROM (SELECT DISTINCT region FROM resources) AS temp").Scan(handler.ctx, &regions)
err := handler.db.NewRaw("SELECT COUNT(*) as count FROM (SELECT DISTINCT region FROM resources) AS temp").Scan(handler.ctx, &regions)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

resources := struct {
Count int `bun:"count" json:"total"`
}{}

handler.db.NewRaw("SELECT COUNT(*) as count FROM resources").Scan(handler.ctx, &resources)
err = handler.db.NewRaw("SELECT COUNT(*) as count FROM resources").Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

cost := struct {
Sum float64 `bun:"sum" json:"total"`
}{}

handler.db.NewRaw("SELECT SUM(cost) as sum FROM resources").Scan(handler.ctx, &cost)
err = handler.db.NewRaw("SELECT SUM(cost) as sum FROM resources").Scan(handler.ctx, &cost)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

accounts := struct {
Count int `bun:"count" json:"total"`
}{}

handler.db.NewRaw("SELECT COUNT(*) as count FROM (SELECT DISTINCT account FROM resources) AS temp").Scan(handler.ctx, &accounts)
err = handler.db.NewRaw("SELECT COUNT(*) as count FROM (SELECT DISTINCT account FROM resources) AS temp").Scan(handler.ctx, &accounts)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

output := struct {
Resources int `json:"resources"`
Expand Down Expand Up @@ -65,9 +78,15 @@ func (handler *ApiHandler) ResourcesBreakdownStatsHandler(w http.ResponseWriter,

if len(input.Exclude) > 0 {
s, _ := json.Marshal(input.Exclude)
handler.db.NewRaw(fmt.Sprintf("SELECT %s as label, COUNT(*) as total FROM resources WHERE %s NOT IN (%s) GROUP BY %s ORDER by total desc;", input.Filter, input.Filter, strings.Trim(string(s), "[]"), input.Filter)).Scan(handler.ctx, &groups)
err = handler.db.NewRaw(fmt.Sprintf("SELECT %s as label, COUNT(*) as total FROM resources WHERE %s NOT IN (%s) GROUP BY %s ORDER by total desc;", input.Filter, input.Filter, strings.Trim(string(s), "[]"), input.Filter)).Scan(handler.ctx, &groups)
if err != nil {
logrus.WithError(err).Error("scan failed")
}
} else {
handler.db.NewRaw(fmt.Sprintf("SELECT %s as label, COUNT(*) as total FROM resources GROUP BY %s ORDER by total desc;", input.Filter, input.Filter)).Scan(handler.ctx, &groups)
err = handler.db.NewRaw(fmt.Sprintf("SELECT %s as label, COUNT(*) as total FROM resources GROUP BY %s ORDER by total desc;", input.Filter, input.Filter)).Scan(handler.ctx, &groups)
if err != nil {
logrus.WithError(err).Error("scan failed")
}
}

segments := groups
Expand All @@ -92,7 +111,10 @@ func (handler *ApiHandler) ResourcesBreakdownStatsHandler(w http.ResponseWriter,
func (handler *ApiHandler) LocationBreakdownStatsHandler(w http.ResponseWriter, r *http.Request) {
groups := make([]models.OutputResources, 0)

handler.db.NewRaw("SELECT region as label, COUNT(*) as total FROM resources GROUP BY region ORDER by total desc;").Scan(handler.ctx, &groups)
err := handler.db.NewRaw("SELECT region as label, COUNT(*) as total FROM resources GROUP BY region ORDER by total desc;").Scan(handler.ctx, &groups)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

locations := make([]models.OutputLocations, 0)

Expand Down
23 changes: 17 additions & 6 deletions handlers/resources_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/utils"
Expand Down Expand Up @@ -53,8 +54,6 @@ func (handler *ApiHandler) FilterResourcesHandler(w http.ResponseWriter, r *http

var limit int64
var skip int64
limit = 0
skip = 0
l, err := strconv.ParseInt(limitRaw, 10, 64)
if err != nil {
limit = 0
Expand Down Expand Up @@ -224,9 +223,15 @@ func (handler *ApiHandler) FilterResourcesHandler(w http.ResponseWriter, r *http
if len(filters) == 0 {
if len(query) > 0 {
whereClause := fmt.Sprintf("(name ilike '%s' OR region ilike '%s' OR service ilike '%s' OR provider ilike '%s' OR account ilike '%s' OR tags @> '[{\"value\":\"%s\"}]' or tags @> '[{\"key\":\"%s\"}]')", query, query, query, query, query, query, query)
handler.db.NewRaw(fmt.Sprintf("SELECT * FROM resources WHERE %s ORDER BY id LIMIT %d OFFSET %d", whereClause, limit, skip)).Scan(handler.ctx, &resources)
err = handler.db.NewRaw(fmt.Sprintf("SELECT * FROM resources WHERE %s ORDER BY id LIMIT %d OFFSET %d", whereClause, limit, skip)).Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Error("scan failed")
}
} else {
handler.db.NewRaw(fmt.Sprintf("SELECT * FROM resources ORDER BY id LIMIT %d OFFSET %d", limit, skip)).Scan(handler.ctx, &resources)
err = handler.db.NewRaw(fmt.Sprintf("SELECT * FROM resources ORDER BY id LIMIT %d OFFSET %d", limit, skip)).Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Error("scan failed")
}
}
respondWithJSON(w, 200, resources)
return
Expand All @@ -246,7 +251,10 @@ func (handler *ApiHandler) FilterResourcesHandler(w http.ResponseWriter, r *http
}
}

handler.db.NewRaw(query).Scan(handler.ctx, &resources)
err = handler.db.NewRaw(query).Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Error("scan failed")
}
} else {
query := fmt.Sprintf("SELECT * FROM resources WHERE %s ORDER BY id LIMIT %d OFFSET %d", whereClause, limit, skip)
if len(view.Exclude) > 0 {
Expand All @@ -272,5 +280,8 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
_, err := w.Write(response)
if err != nil {
logrus.WithError(err).Error("write failed")
}
}
66 changes: 53 additions & 13 deletions handlers/stats_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/sirupsen/logrus"
. "github.com/tailwarden/komiser/models"
"github.com/uptrace/bun/dialect"
)
Expand All @@ -16,19 +17,28 @@ func (handler *ApiHandler) StatsHandler(w http.ResponseWriter, r *http.Request)
Count int `bun:"count" json:"total"`
}{}

handler.db.NewRaw("SELECT COUNT(*) as count FROM (SELECT DISTINCT region FROM resources) AS temp").Scan(handler.ctx, &regions)
err := handler.db.NewRaw("SELECT COUNT(*) as count FROM (SELECT DISTINCT region FROM resources) AS temp").Scan(handler.ctx, &regions)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

resources := struct {
Count int `bun:"count" json:"total"`
}{}

handler.db.NewRaw("SELECT COUNT(*) as count FROM resources").Scan(handler.ctx, &resources)
err = handler.db.NewRaw("SELECT COUNT(*) as count FROM resources").Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

cost := struct {
Sum float64 `bun:"sum" json:"total"`
}{}

handler.db.NewRaw("SELECT SUM(cost) as sum FROM resources").Scan(handler.ctx, &cost)
err = handler.db.NewRaw("SELECT SUM(cost) as sum FROM resources").Scan(handler.ctx, &cost)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

output := struct {
Resources int `json:"resources"`
Expand Down Expand Up @@ -201,19 +211,28 @@ func (handler *ApiHandler) FilterStatsHandler(w http.ResponseWriter, r *http.Req
query = fmt.Sprintf("FROM resources CROSS JOIN json_each(tags) WHERE type='object' AND %s", whereClause)
}

handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count FROM (SELECT DISTINCT region %s) AS temp", query)).Scan(handler.ctx, &regions)
err = handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count FROM (SELECT DISTINCT region %s) AS temp", query)).Scan(handler.ctx, &regions)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

resources := struct {
Count int `bun:"count" json:"total"`
}{}

handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count %s", query)).Scan(handler.ctx, &resources)
err = handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count %s", query)).Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

cost := struct {
Sum float64 `bun:"sum" json:"total"`
}{}

handler.db.NewRaw(fmt.Sprintf("SELECT SUM(cost) as sum %s", query)).Scan(handler.ctx, &cost)
err = handler.db.NewRaw(fmt.Sprintf("SELECT SUM(cost) as sum %s", query)).Scan(handler.ctx, &cost)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

output := struct {
Resources int `json:"resources"`
Expand All @@ -232,19 +251,28 @@ func (handler *ApiHandler) FilterStatsHandler(w http.ResponseWriter, r *http.Req
regions := struct {
Count int `bun:"count" json:"total"`
}{}
handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count FROM (SELECT DISTINCT region %s) AS temp", query)).Scan(handler.ctx, &regions)
err = handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count FROM (SELECT DISTINCT region %s) AS temp", query)).Scan(handler.ctx, &regions)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

resources := struct {
Count int `bun:"count" json:"total"`
}{}

handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count %s", query)).Scan(handler.ctx, &resources)
err = handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count %s", query)).Scan(handler.ctx, &resources)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

cost := struct {
Sum float64 `bun:"sum" json:"total"`
}{}

handler.db.NewRaw(fmt.Sprintf("SELECT SUM(cost) as sum %s", query)).Scan(handler.ctx, &cost)
err = handler.db.NewRaw(fmt.Sprintf("SELECT SUM(cost) as sum %s", query)).Scan(handler.ctx, &cost)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

output := struct {
Resources int `json:"resources"`
Expand All @@ -267,7 +295,10 @@ func (handler *ApiHandler) ListRegionsHandler(w http.ResponseWriter, r *http.Req

outputs := make([]Output, 0)

handler.db.NewRaw("SELECT DISTINCT(region) FROM resources").Scan(handler.ctx, &outputs)
err := handler.db.NewRaw("SELECT DISTINCT(region) FROM resources").Scan(handler.ctx, &outputs)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

regions := make([]string, 0)

Expand All @@ -285,7 +316,10 @@ func (handler *ApiHandler) ListProvidersHandler(w http.ResponseWriter, r *http.R

outputs := make([]Output, 0)

handler.db.NewRaw("SELECT DISTINCT(provider) FROM resources").Scan(handler.ctx, &outputs)
err := handler.db.NewRaw("SELECT DISTINCT(provider) FROM resources").Scan(handler.ctx, &outputs)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

providers := make([]string, 0)

Expand All @@ -303,7 +337,10 @@ func (handler *ApiHandler) ListServicesHandler(w http.ResponseWriter, r *http.Re

outputs := make([]Output, 0)

handler.db.NewRaw("SELECT DISTINCT(service) FROM resources").Scan(handler.ctx, &outputs)
err := handler.db.NewRaw("SELECT DISTINCT(service) FROM resources").Scan(handler.ctx, &outputs)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

services := make([]string, 0)

Expand All @@ -321,7 +358,10 @@ func (handler *ApiHandler) ListAccountsHandler(w http.ResponseWriter, r *http.Re

outputs := make([]Output, 0)

handler.db.NewRaw("SELECT DISTINCT(account) FROM resources").Scan(handler.ctx, &outputs)
err := handler.db.NewRaw("SELECT DISTINCT(account) FROM resources").Scan(handler.ctx, &outputs)
if err != nil {
logrus.WithError(err).Error("scan failed")
}

accounts := make([]string, 0)

Expand Down
Loading

0 comments on commit 5526131

Please sign in to comment.