Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

NOISSUE - Add Linters #79

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ issues:
- "string `For example:\n` has (\\d+) occurrences, make it a constant"

linters-settings:
importas:
no-unaliased: true
no-extra-aliases: false
alias:
- pkg: github.com/mainflux/callhome/pkg/client
alias: chclient
- pkg: github.com/absmach/magistrala/logger
SammyOina marked this conversation as resolved.
Show resolved Hide resolved
alias: mglog
gocritic:
enabled-checks:
- captLocal
- singleCaseSwitch
- switchTrue
- importShadow
- httpNoBody
- paramTypeCombine
- emptyStringTest
- builtinShadow
- exposedSyncMutex
disabled-checks:
- appendAssign
enabled-tags:
Expand Down
60 changes: 30 additions & 30 deletions auth/api/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ func NewClient(conn *grpc.ClientConn, timeout time.Duration) magistrala.AuthServ
}

func (client grpcClient) Issue(ctx context.Context, req *magistrala.IssueReq, _ ...grpc.CallOption) (*magistrala.Token, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.issue(ctx, issueReq{userID: req.GetUserId(), domainID: req.GetDomainId(), keyType: auth.KeyType(req.Type)})
if err != nil {
Expand All @@ -186,8 +186,8 @@ func decodeIssueResponse(_ context.Context, grpcRes interface{}) (interface{}, e
}

func (client grpcClient) Refresh(ctx context.Context, req *magistrala.RefreshReq, _ ...grpc.CallOption) (*magistrala.Token, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.refresh(ctx, refreshReq{refreshToken: req.GetRefreshToken(), domainID: req.GetDomainId()})
if err != nil {
Expand All @@ -206,8 +206,8 @@ func decodeRefreshResponse(_ context.Context, grpcRes interface{}) (interface{},
}

func (client grpcClient) Identify(ctx context.Context, token *magistrala.IdentityReq, _ ...grpc.CallOption) (*magistrala.IdentityRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.identify(ctx, identityReq{token: token.GetToken()})
if err != nil {
Expand All @@ -229,8 +229,8 @@ func decodeIdentifyResponse(_ context.Context, grpcRes interface{}) (interface{}
}

func (client grpcClient) Authorize(ctx context.Context, req *magistrala.AuthorizeReq, _ ...grpc.CallOption) (r *magistrala.AuthorizeRes, err error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.authorize(ctx, authReq{
Domain: req.GetDomain(),
Expand Down Expand Up @@ -270,8 +270,8 @@ func encodeAuthorizeRequest(_ context.Context, grpcReq interface{}) (interface{}
}

func (client grpcClient) AddPolicy(ctx context.Context, in *magistrala.AddPolicyReq, opts ...grpc.CallOption) (*magistrala.AddPolicyRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.addPolicy(ctx, policyReq{
Domain: in.GetDomain(),
Expand Down Expand Up @@ -313,8 +313,8 @@ func encodeAddPolicyRequest(_ context.Context, grpcReq interface{}) (interface{}
}

func (client grpcClient) AddPolicies(ctx context.Context, in *magistrala.AddPoliciesReq, opts ...grpc.CallOption) (*magistrala.AddPoliciesRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
r := policiesReq{}
if in.GetAddPoliciesReq() != nil {
for _, mgApr := range in.GetAddPoliciesReq() {
Expand Down Expand Up @@ -368,8 +368,8 @@ func encodeAddPoliciesRequest(_ context.Context, grpcReq interface{}) (interface
}

func (client grpcClient) DeletePolicy(ctx context.Context, in *magistrala.DeletePolicyReq, opts ...grpc.CallOption) (*magistrala.DeletePolicyRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.deletePolicy(ctx, policyReq{
Domain: in.GetDomain(),
Expand Down Expand Up @@ -411,8 +411,8 @@ func encodeDeletePolicyRequest(_ context.Context, grpcReq interface{}) (interfac
}

func (client grpcClient) DeletePolicies(ctx context.Context, in *magistrala.DeletePoliciesReq, opts ...grpc.CallOption) (*magistrala.DeletePoliciesRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
r := policiesReq{}

if in.GetDeletePoliciesReq() != nil {
Expand Down Expand Up @@ -466,8 +466,8 @@ func encodeDeletePoliciesRequest(_ context.Context, grpcReq interface{}) (interf
}

func (client grpcClient) ListObjects(ctx context.Context, in *magistrala.ListObjectsReq, opts ...grpc.CallOption) (*magistrala.ListObjectsRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.listObjects(ctx, listObjectsReq{
Domain: in.GetDomain(),
Expand Down Expand Up @@ -505,8 +505,8 @@ func encodeListObjectsRequest(_ context.Context, grpcReq interface{}) (interface
}

func (client grpcClient) ListAllObjects(ctx context.Context, in *magistrala.ListObjectsReq, opts ...grpc.CallOption) (*magistrala.ListObjectsRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.listAllObjects(ctx, listObjectsReq{
Domain: in.GetDomain(),
Expand All @@ -526,8 +526,8 @@ func (client grpcClient) ListAllObjects(ctx context.Context, in *magistrala.List
}

func (client grpcClient) CountObjects(ctx context.Context, in *magistrala.CountObjectsReq, opts ...grpc.CallOption) (*magistrala.CountObjectsRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.countObjects(ctx, listObjectsReq{
Domain: in.GetDomain(),
Expand Down Expand Up @@ -565,8 +565,8 @@ func encodeCountObjectsRequest(_ context.Context, grpcReq interface{}) (interfac
}

func (client grpcClient) ListSubjects(ctx context.Context, in *magistrala.ListSubjectsReq, opts ...grpc.CallOption) (*magistrala.ListSubjectsRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.listSubjects(ctx, listSubjectsReq{
Domain: in.GetDomain(),
Expand Down Expand Up @@ -605,8 +605,8 @@ func encodeListSubjectsRequest(_ context.Context, grpcReq interface{}) (interfac
}

func (client grpcClient) ListAllSubjects(ctx context.Context, in *magistrala.ListSubjectsReq, opts ...grpc.CallOption) (*magistrala.ListSubjectsRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.listAllSubjects(ctx, listSubjectsReq{
Domain: in.GetDomain(),
Expand All @@ -626,8 +626,8 @@ func (client grpcClient) ListAllSubjects(ctx context.Context, in *magistrala.Lis
}

func (client grpcClient) CountSubjects(ctx context.Context, in *magistrala.CountSubjectsReq, opts ...grpc.CallOption) (*magistrala.CountSubjectsRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.countSubjects(ctx, countSubjectsReq{
Domain: in.GetDomain(),
Expand Down Expand Up @@ -665,8 +665,8 @@ func encodeCountSubjectsRequest(_ context.Context, grpcReq interface{}) (interfa
}

func (client grpcClient) ListPermissions(ctx context.Context, in *magistrala.ListPermissionsReq, opts ...grpc.CallOption) (*magistrala.ListPermissionsRes, error) {
ctx, close := context.WithTimeout(ctx, client.timeout)
defer close()
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.listPermissions(ctx, listPermissionsReq{
Domain: in.GetDomain(),
Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/domains/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/internal/api"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/logger"
mglog "github.com/absmach/magistrala/logger"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

func MakeHandler(svc auth.Service, mux *chi.Mux, logger logger.Logger) *chi.Mux {
func MakeHandler(svc auth.Service, mux *chi.Mux, logger mglog.Logger) *chi.Mux {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, api.EncodeError)),
}
Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/keys/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/absmach/magistrala/auth/jwt"
"github.com/absmach/magistrala/auth/mocks"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/logger"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -76,7 +76,7 @@ func newService() auth.Service {
}

func newServer(svc auth.Service) *httptest.Server {
logger := logger.NewMock()
logger := mglog.NewMock()
mux := httpapi.MakeHandler(svc, logger, "")
return httptest.NewServer(mux)
}
Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/keys/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/logger"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/errors"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
Expand All @@ -21,7 +21,7 @@ import (
const contentType = "application/json"

// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc auth.Service, mux *chi.Mux, logger logger.Logger) *chi.Mux {
func MakeHandler(svc auth.Service, mux *chi.Mux, logger mglog.Logger) *chi.Mux {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, encodeError)),
}
Expand Down
4 changes: 2 additions & 2 deletions auth/api/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/auth/api/http/domains"
"github.com/absmach/magistrala/auth/api/http/keys"
"github.com/absmach/magistrala/logger"
mglog "github.com/absmach/magistrala/logger"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc auth.Service, logger logger.Logger, instanceID string) http.Handler {
func MakeHandler(svc auth.Service, logger mglog.Logger, instanceID string) http.Handler {
mux := chi.NewRouter()

mux = keys.MakeHandler(svc, mux, logger)
Expand Down
18 changes: 9 additions & 9 deletions auth/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (
"time"

"github.com/absmach/magistrala/auth"
log "github.com/absmach/magistrala/logger"
mglog "github.com/absmach/magistrala/logger"
)

var _ auth.Service = (*loggingMiddleware)(nil)

type loggingMiddleware struct {
logger log.Logger
logger mglog.Logger
svc auth.Service
}

// LoggingMiddleware adds logging facilities to the core service.
func LoggingMiddleware(svc auth.Service, logger log.Logger) auth.Service {
func LoggingMiddleware(svc auth.Service, logger mglog.Logger) auth.Service {
return &loggingMiddleware{logger, svc}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ func (lm *loggingMiddleware) CreateDomain(ctx context.Context, token string, d a
return lm.svc.CreateDomain(ctx, token, d)
}

func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token string, id string) (do auth.Domain, err error) {
func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token, id string) (do auth.Domain, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method retrieve_domain for domain id %s took %s to complete", id, time.Since(begin))
if err != nil {
Expand All @@ -256,7 +256,7 @@ func (lm *loggingMiddleware) RetrieveDomain(ctx context.Context, token string, i
return lm.svc.RetrieveDomain(ctx, token, id)
}

func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token string, id string, d auth.DomainReq) (do auth.Domain, err error) {
func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method update_domain for domain id %s took %s to complete", id, time.Since(begin))
if err != nil {
Expand All @@ -268,7 +268,7 @@ func (lm *loggingMiddleware) UpdateDomain(ctx context.Context, token string, id
return lm.svc.UpdateDomain(ctx, token, id, d)
}

func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token string, id string, d auth.DomainReq) (do auth.Domain, err error) {
func (lm *loggingMiddleware) ChangeDomainStatus(ctx context.Context, token, id string, d auth.DomainReq) (do auth.Domain, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method change_domain_status for domain id %s took %s to complete", id, time.Since(begin))
if err != nil {
Expand All @@ -292,7 +292,7 @@ func (lm *loggingMiddleware) ListDomains(ctx context.Context, token string, page
return lm.svc.ListDomains(ctx, token, page)
}

func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token string, id string, userIds []string, relation string) (err error) {
func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method assign_users took %s to complete", time.Since(begin))
if err != nil {
Expand All @@ -304,7 +304,7 @@ func (lm *loggingMiddleware) AssignUsers(ctx context.Context, token string, id s
return lm.svc.AssignUsers(ctx, token, id, userIds, relation)
}

func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token string, id string, userIds []string, relation string) (err error) {
func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token, id string, userIds []string, relation string) (err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method unassign_users took %s to complete", time.Since(begin))
if err != nil {
Expand All @@ -316,7 +316,7 @@ func (lm *loggingMiddleware) UnassignUsers(ctx context.Context, token string, id
return lm.svc.UnassignUsers(ctx, token, id, userIds, relation)
}

func (lm *loggingMiddleware) ListUserDomains(ctx context.Context, token string, userID string, page auth.Page) (do auth.DomainsPage, err error) {
func (lm *loggingMiddleware) ListUserDomains(ctx context.Context, token, userID string, page auth.Page) (do auth.DomainsPage, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method list_user_domains took %s to complete", time.Since(begin))
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions auth/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,23 @@ func (ms *metricsMiddleware) CreateDomain(ctx context.Context, token string, d a
return ms.svc.CreateDomain(ctx, token, d)
}

func (ms *metricsMiddleware) RetrieveDomain(ctx context.Context, token string, id string) (auth.Domain, error) {
func (ms *metricsMiddleware) RetrieveDomain(ctx context.Context, token, id string) (auth.Domain, error) {
defer func(begin time.Time) {
ms.counter.With("method", "retrieve_domain").Add(1)
ms.latency.With("method", "retrieve_domain").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.RetrieveDomain(ctx, token, id)
}

func (ms *metricsMiddleware) UpdateDomain(ctx context.Context, token string, id string, d auth.DomainReq) (auth.Domain, error) {
func (ms *metricsMiddleware) UpdateDomain(ctx context.Context, token, id string, d auth.DomainReq) (auth.Domain, error) {
defer func(begin time.Time) {
ms.counter.With("method", "update_domain").Add(1)
ms.latency.With("method", "update_domain").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UpdateDomain(ctx, token, id, d)
}

func (ms *metricsMiddleware) ChangeDomainStatus(ctx context.Context, token string, id string, d auth.DomainReq) (auth.Domain, error) {
func (ms *metricsMiddleware) ChangeDomainStatus(ctx context.Context, token, id string, d auth.DomainReq) (auth.Domain, error) {
defer func(begin time.Time) {
ms.counter.With("method", "change_domain_status").Add(1)
ms.latency.With("method", "change_domain_status").Observe(time.Since(begin).Seconds())
Expand All @@ -208,23 +208,23 @@ func (ms *metricsMiddleware) ListDomains(ctx context.Context, token string, page
return ms.svc.ListDomains(ctx, token, page)
}

func (ms *metricsMiddleware) AssignUsers(ctx context.Context, token string, id string, userIds []string, relation string) error {
func (ms *metricsMiddleware) AssignUsers(ctx context.Context, token, id string, userIds []string, relation string) error {
defer func(begin time.Time) {
ms.counter.With("method", "assign_users").Add(1)
ms.latency.With("method", "assign_users").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.AssignUsers(ctx, token, id, userIds, relation)
}

func (ms *metricsMiddleware) UnassignUsers(ctx context.Context, token string, id string, userIds []string, relation string) error {
func (ms *metricsMiddleware) UnassignUsers(ctx context.Context, token, id string, userIds []string, relation string) error {
defer func(begin time.Time) {
ms.counter.With("method", "unassign_users").Add(1)
ms.latency.With("method", "unassign_users").Observe(time.Since(begin).Seconds())
}(time.Now())
return ms.svc.UnassignUsers(ctx, token, id, userIds, relation)
}

func (ms *metricsMiddleware) ListUserDomains(ctx context.Context, token string, userID string, page auth.Page) (auth.DomainsPage, error) {
func (ms *metricsMiddleware) ListUserDomains(ctx context.Context, token, userID string, page auth.Page) (auth.DomainsPage, error) {
defer func(begin time.Time) {
ms.counter.With("method", "list_user_domains").Add(1)
ms.latency.With("method", "list_user_domains").Observe(time.Since(begin).Seconds())
Expand Down
2 changes: 1 addition & 1 deletion auth/mocks/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (m *DomainsRepo) ListDomains(ctx context.Context, pm auth.Page) (auth.Domai
return ret.Get(0).(auth.DomainsPage), ret.Error(1)
}

func (m *DomainsRepo) Update(ctx context.Context, id string, userID string, d auth.DomainReq) (auth.Domain, error) {
func (m *DomainsRepo) Update(ctx context.Context, id, userID string, d auth.DomainReq) (auth.Domain, error) {
ret := m.Called(ctx, d, id, userID)

return ret.Get(0).(auth.Domain), ret.Error(1)
Expand Down
Loading
Loading