Skip to content

Commit

Permalink
add linter and fix faults
Browse files Browse the repository at this point in the history
  • Loading branch information
psycofdj committed Apr 25, 2023
1 parent 8d6f7c2 commit ca4e7d7
Show file tree
Hide file tree
Showing 25 changed files with 305 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: set up go
uses: actions/setup-go@v4
with:
go-version: 1.18
go-version: ">=1.20"

- name: tests modules
run: |
Expand Down
22 changes: 22 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: linter

on: push

jobs:
linter:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: set up go
uses: actions/setup-go@v4
with:
go-version: ">=1.20"

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
args: --config .golangci.yml
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: set up go
uses: actions/setup-go@v4
with:
go-version: "1.18"
go-version: ">=1.20"

- name: running unit-tests
run: |
Expand Down
35 changes: 35 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
run:
timeout: 30m
output:
format: line-number
linters:
disable-all: false
enable:
- bodyclose
- depguard
- dogsled
#- dupl
- errcheck
- exportloopref
#- funlen
- gocognit
- goconst
- gocritic
- godox
- gofmt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace
- reassign
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
all: test

test:
@go test -v ./...

check:
@golangci-lint run --config .golangci.yml

coverage:
@go test -cover -coverprofile cover.out -v ./...
@go tool cover -func=cover.out
@rm -f cover.out
32 changes: 20 additions & 12 deletions bosh_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"os"
"strings"
"time"

"github.com/cloudfoundry/bosh-cli/director"
"github.com/cloudfoundry/bosh-cli/uaa"
Expand Down Expand Up @@ -126,12 +127,11 @@ func (h *basicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
h.handler(w, r)
return
}

type boshConfigUpdater struct{}

func (cu boshConfigUpdater) UpdateConfigWithToken(environment string, token uaa.AccessToken) error {
func (cu boshConfigUpdater) UpdateConfigWithToken(_ string, _ uaa.AccessToken) error {
return nil
}
func (cu boshConfigUpdater) Save() error {
Expand All @@ -152,11 +152,11 @@ func prometheusHandler() http.Handler {
return handler
}

func readCACert(CACertFile string, logger logger.Logger) (string, error) {
if CACertFile != "" {
func readCaCert(caCertFile string, logger logger.Logger) (string, error) {
if caCertFile != "" {
fs := system.NewOsFileSystem(logger)

CACertFileFullPath, err := fs.ExpandPath(CACertFile)
CACertFileFullPath, err := fs.ExpandPath(caCertFile)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func buildBOSHClient() (director.Director, error) {
return nil, err
}

boshCACert, err := readCACert(*boshCACertFile, logger)
boshCACert, err := readCaCert(*boshCACertFile, logger)
if err != nil {
return nil, err
}
Expand All @@ -208,7 +208,7 @@ func buildBOSHClient() (director.Director, error) {
uaaURL := boshInfo.Auth.Options["url"]
uaaURLStr, ok := uaaURL.(string)
if !ok {
return nil, fmt.Errorf("Expected UAA URL '%s' to be a string", uaaURL)
return nil, fmt.Errorf("expected UAA URL '%s' to be a string", uaaURL)
}

uaaConfig, err := uaa.NewConfigFromURL(uaaURLStr)
Expand All @@ -235,11 +235,11 @@ func buildBOSHClient() (director.Director, error) {
directorConfig.TokenFunc = uaa.NewClientTokenSession(uaaClient).TokenFunc
} else {
answers := []uaa.PromptAnswer{
uaa.PromptAnswer{
{
Key: "username",
Value: *boshUsername,
},
uaa.PromptAnswer{
{
Key: "password",
Value: *boshPassword,
},
Expand Down Expand Up @@ -349,7 +349,7 @@ func main() {

http.Handle(*metricsPath, prometheusHandler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
_, _ = w.Write([]byte(`<html>
<head><title>BOSH Exporter</title></head>
<body>
<h1>BOSH Exporter</h1>
Expand All @@ -358,11 +358,19 @@ func main() {
</html>`))
})

server := &http.Server{
Addr: *listenAddress,
ReadTimeout: time.Second * 5,
ReadHeaderTimeout: time.Second * 10,
}

if *tlsCertFile != "" && *tlsKeyFile != "" {
log.Infoln("Listening TLS on", *listenAddress)
log.Fatal(http.ListenAndServeTLS(*listenAddress, *tlsCertFile, *tlsKeyFile, nil))
err = server.ListenAndServeTLS(*tlsCertFile, *tlsKeyFile)
} else {
log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
err = server.ListenAndServe()
}

log.Fatal(err)
}
2 changes: 1 addition & 1 deletion collectors/bosh_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewBoshCollector(
Namespace: namespace,
Subsystem: "",
Name: "scrape_errors_total",
Help: "Total number of times an error occured scraping BOSH.",
Help: "Total number of times an error occurred scraping BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
Expand Down
14 changes: 7 additions & 7 deletions collectors/bosh_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"github.com/bosh-prometheus/bosh_exporter/filters"

. "github.com/bosh-prometheus/bosh_exporter/collectors"
. "github.com/bosh-prometheus/bosh_exporter/utils/test_matchers"
. "github.com/bosh-prometheus/bosh_exporter/utils/matchers"
)

func init() {
log.Base().SetLevel("fatal")
_ = log.Base().SetLevel("fatal")
}

var _ = Describe("BoshCollector", func() {
Expand Down Expand Up @@ -51,10 +51,10 @@ var _ = Describe("BoshCollector", func() {
)

BeforeEach(func() {
namespace = "test_exporter"
environment = "test_environment"
boshName = "test_bosh_name"
boshUUID = "test_bosh_uuid"
namespace = testNamespace
environment = testEnvironment
boshName = testBoshName
boshUUID = testBoshUUID
tmpfile, err = os.CreateTemp("", "service_discovery_collector_test_")
Expect(err).ToNot(HaveOccurred())
serviceDiscoveryFilename = tmpfile.Name()
Expand Down Expand Up @@ -92,7 +92,7 @@ var _ = Describe("BoshCollector", func() {
Namespace: namespace,
Subsystem: "",
Name: "scrape_errors_total",
Help: "Total number of times an error occured scraping BOSH.",
Help: "Total number of times an error occurred scraping BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
Expand Down
7 changes: 7 additions & 0 deletions collectors/collectors_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"testing"
)

const (
testNamespace = "test_exporter"
testEnvironment = "test_environment"
testBoshName = "test_bosh_name"
testBoshUUID = "test_bosh_uuid"
)

func TestCollectors(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Collectors Suite")
Expand Down
9 changes: 3 additions & 6 deletions collectors/deployments_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ func (c *DeploymentsCollector) Collect(deployments []deployments.DeploymentInfo,
c.deploymentInstancesMetric.Reset()

for _, deployment := range deployments {
c.reportDeploymentReleaseInfoMetrics(deployment, ch)
c.reportDeploymentStemcellInfoMetrics(deployment, ch)
c.reportDeploymentInstancesMetrics(deployment, ch)
c.reportDeploymentReleaseInfoMetrics(deployment)
c.reportDeploymentStemcellInfoMetrics(deployment)
c.reportDeploymentInstancesMetrics(deployment)
}

c.deploymentReleaseInfoMetric.Collect(ch)
Expand All @@ -141,7 +141,6 @@ func (c *DeploymentsCollector) Describe(ch chan<- *prometheus.Desc) {

func (c *DeploymentsCollector) reportDeploymentReleaseInfoMetrics(
deployment deployments.DeploymentInfo,
ch chan<- prometheus.Metric,
) {
for _, release := range deployment.Releases {
c.deploymentReleaseInfoMetric.WithLabelValues(
Expand All @@ -154,7 +153,6 @@ func (c *DeploymentsCollector) reportDeploymentReleaseInfoMetrics(

func (c *DeploymentsCollector) reportDeploymentStemcellInfoMetrics(
deployment deployments.DeploymentInfo,
ch chan<- prometheus.Metric,
) {
for _, stemcell := range deployment.Stemcells {
c.deploymentStemcellInfoMetric.WithLabelValues(
Expand All @@ -168,7 +166,6 @@ func (c *DeploymentsCollector) reportDeploymentStemcellInfoMetrics(

func (c *DeploymentsCollector) reportDeploymentInstancesMetrics(
deployment deployments.DeploymentInfo,
ch chan<- prometheus.Metric,
) {
for _, instance := range deployment.Instances {
c.deploymentInstancesMetric.WithLabelValues(
Expand Down
10 changes: 5 additions & 5 deletions collectors/deployments_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/bosh-prometheus/bosh_exporter/deployments"

. "github.com/bosh-prometheus/bosh_exporter/collectors"
. "github.com/bosh-prometheus/bosh_exporter/utils/test_matchers"
. "github.com/bosh-prometheus/bosh_exporter/utils/matchers"
)

func init() {
Expand Down Expand Up @@ -43,10 +43,10 @@ var _ = Describe("DeploymentsCollector", func() {
)

BeforeEach(func() {
namespace = "test_exporter"
environment = "test_environment"
boshName = "test_bosh_name"
boshUUID = "test_bosh_uuid"
namespace = testNamespace
environment = testEnvironment
boshName = testBoshName
boshUUID = testBoshUUID

deploymentReleaseInfoMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down
Loading

0 comments on commit ca4e7d7

Please sign in to comment.