1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-02 22:17:10 +08:00
b1ackd0t 5e060d5620
NOISSUE - Add More Linters (#1924)
* Fix linting errors

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* feat(linters): add ineffassign linter

This commit adds the `ineffassign` linter to the project's `.golangci.yml` configuration file. The `ineffassign` linter helps identify and flag assignments to variables that are never used, helping to improve code quality and maintainability.

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* Add extra linters

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* feat(golangci): Add header check

- Added goheader check to ensure all files have license headers
- Added build tags for "nats" in the .golangci.yml file to include the necessary dependencies for the "nats" package during the build process.
- Also, increased the maximum number of issues per linter and the maximum number of same issues reported by the linter to improve the code quality analysis.

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* feat(.golangci.yml): Add new linters

Add the following new linters to the .golangci.yml configuration file:
- asasalint
- asciicheck
- bidichk
- contextcheck
- decorder
- dogsled
- errchkjson
- errname
- execinquery
- exportloopref
- ginkgolinter
- gocheckcompilerdirectives

These linters will help improve code quality and catch potential issues during the code review process.

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

---------

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
2023-10-16 11:43:33 +02:00

58 lines
2.5 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package influxdb
import (
"context"
"time"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/mainflux/mainflux/internal/env"
"github.com/mainflux/mainflux/pkg/errors"
)
var (
errConnect = errors.New("failed to create InfluxDB client")
errConfig = errors.New("failed to load InfluxDB client configuration from environment variable")
)
type Config struct {
Protocol string `env:"PROTOCOL" envDefault:"http"`
Host string `env:"HOST" envDefault:"localhost"`
Port string `env:"PORT" envDefault:"8086"`
Username string `env:"ADMIN_USER" envDefault:"mainflux"`
Password string `env:"ADMIN_PASSWORD" envDefault:"mainflux"`
DBName string `env:"NAME" envDefault:"mainflux"`
Bucket string `env:"BUCKET" envDefault:"mainflux-bucket"`
Org string `env:"ORG" envDefault:"mainflux"`
Token string `env:"TOKEN" envDefault:"mainflux-token"`
DBUrl string `env:"DBURL" envDefault:""`
UserAgent string `env:"USER_AGENT" envDefault:"InfluxDBClient"`
Timeout time.Duration `env:"TIMEOUT"` // Influxdb client configuration by default has no timeout duration , this field will not have a fallback default timeout duration. Reference: https://pkg.go.dev/github.com/influxdata/influxdb@v1.10.0/client/v2#HTTPConfig
InsecureSkipVerify bool `env:"INSECURE_SKIP_VERIFY" envDefault:"false"`
}
// Setup load configuration from environment variable, create InfluxDB client and connect to InfluxDB server.
func Setup(ctx context.Context, envPrefix string) (influxdb2.Client, error) {
config := Config{}
if err := env.Parse(&config, env.Options{Prefix: envPrefix}); err != nil {
return nil, errors.Wrap(errConfig, err)
}
return Connect(ctx, config)
}
// Connect create InfluxDB client and connect to InfluxDB server.
func Connect(ctx context.Context, config Config) (influxdb2.Client, error) {
client := influxdb2.NewClientWithOptions(config.DBUrl, config.Token,
influxdb2.DefaultOptions().
SetUseGZip(true).
SetFlushInterval(100))
ctx, cancel := context.WithTimeout(ctx, config.Timeout)
defer cancel()
if _, err := client.Ready(ctx); err != nil {
return nil, errors.Wrap(errConnect, err)
}
return client, nil
}