Skip to content

Commit

Permalink
Add more linters and address identified issues (#314)
Browse files Browse the repository at this point in the history
* add godot linter

* add errorlint linter

* capital letters

* add nlreturn linter

* set limit 15

* set limit 12

* set limit 10

* delete @description from models

* delete @description from models

* sort linters
  • Loading branch information
korotkov-aerospike authored Jan 27, 2025
1 parent aff2ffe commit 71474cb
Show file tree
Hide file tree
Showing 57 changed files with 100 additions and 78 deletions.
14 changes: 11 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ linters:
enable:
- gci
- bodyclose
- unused
- dogsled
- dupl
- errcheck
- errorlint
- funlen
- gci
- goconst
- gocritic
- gocyclo
- godot
- gofmt
- goimports
- gocyclo
Expand All @@ -22,12 +23,14 @@ linters:
- govet
- ineffassign
- misspell
- prealloc
- promlinter
- nolintlint
- nakedret
- nestif
- nlreturn
- prealloc # pre-allocate slices with define size if the slice size is known in advance
- predeclared
- promlinter
- reassign
- revive
- sloglint
- staticcheck
Expand All @@ -46,6 +49,10 @@ linters-settings:
gosec:
excludes:
- G115
nlreturn:
block-size: 10
godot:
exclude: [ "@(Success|Failure|Router).*" ]
tagliatelle:
case:
rules:
Expand All @@ -61,6 +68,7 @@ issues:
- prealloc
- funlen
- goconst
- nlreturn

exclude-dirs:
- examples
Expand Down
1 change: 1 addition & 0 deletions build/readme/readme_example_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func main() {
buffer.WriteString(fmt.Sprintf("<!-- %s -->\n\n```%s\n", name, format))
buffer.Write(formattedExample)
buffer.WriteString("\n```")

return buffer.Bytes()
})
err = os.WriteFile("README.md", updatedReadme, 0600)
Expand Down
1 change: 1 addition & 0 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func runHTTPServer(ctx context.Context, serverConfig *model.HTTPServerConfig, h
}

slog.Info("HTTP server shutdown gracefully")

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/server/configuration/storage_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type storageManager struct {
nsValidator aerospike.NamespaceValidator
}

// newStorageManager returns new instance of storageManager
// newStorageManager returns new instance of storageManager.
func newStorageManager(configStorage model.Storage, nsValidator aerospike.NamespaceValidator) Manager {
return &storageManager{
storage: configStorage,
Expand Down
1 change: 1 addition & 0 deletions internal/server/handlers/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func readBackupsLogic(ctx context.Context,
}
result[routine] = list
}

return result, nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ func logWriter(config *model.LoggerConfig) io.Writer {
if config.GetStdoutWriterOrDefault() {
return io.MultiWriter(fileWriter, os.Stdout)
}

return fileWriter
} else if config.GetStdoutWriterOrDefault() {
return os.Stdout
}

return &ignoreWriter{}
}

Expand Down
1 change: 1 addition & 0 deletions internal/util/white_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewIPWhiteList(ipList []string) *IPWhiteList {
networks = append(networks, &network)
}
}

return &IPWhiteList{
addresses: addresses,
networks: networks,
Expand Down
4 changes: 2 additions & 2 deletions pkg/dto/aerospike_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a *AerospikeCluster) Validate() error {
return nil
}

// NewClusterFromReader creates a new Storage object from a given reader
// NewClusterFromReader creates a new Storage object from a given reader.
func NewClusterFromReader(r io.Reader, format SerializationFormat) (*AerospikeCluster, error) {
a := &AerospikeCluster{}
if err := Deserialize(a, r, format); err != nil {
Expand Down Expand Up @@ -190,7 +190,7 @@ func (c *Credentials) fromModel(m *model.Credentials, config *model.BackupConfig
c.SecretAgentConfig = ResolveSecretAgentFromModel(m.SecretAgent, config)
}

// Validate validates the credentials configuration
// Validate validates the credentials configuration.
func (c *Credentials) Validate() error {
if c == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/dto/backup_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type BackupMetadata struct {
UDFCount uint64 `yaml:"udf-count" json:"udf-count" format:"int64" example:"2"`
}

// NewBackupDetailsFromModel creates a new BackupDetails from a model.BackupDetails
// NewBackupDetailsFromModel creates a new BackupDetails from a model.BackupDetails.
func NewBackupDetailsFromModel(m *model.BackupDetails, config *model.BackupConfig) *BackupDetails {
if m == nil {
return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/dto/backup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type BackupPolicy struct {
Sealed *bool `yaml:"sealed,omitempty" json:"sealed,omitempty"`
}

// NewBackupPolicyFromReader creates a new BackupPolicy object from a given reader
// NewBackupPolicyFromReader creates a new BackupPolicy object from a given reader.
func NewBackupPolicyFromReader(r io.Reader, format SerializationFormat) (*BackupPolicy, error) {
b := &BackupPolicy{}
if err := Deserialize(b, r, format); err != nil {
Expand Down Expand Up @@ -98,6 +98,7 @@ func (p *BackupPolicy) Validate() error {
if err := p.CompressionPolicy.Validate(); err != nil {
return err
}

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/dto/backup_routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (r *BackupRoutine) Validate() error {
return emptyFieldValidationError("secret-agent")
}
}

return nil
}

Expand Down Expand Up @@ -138,7 +139,7 @@ func (r *BackupRoutine) ToModel(
}, nil
}

// NewRoutineFromReader creates a new BackupRoutine object from a given reader
// NewRoutineFromReader creates a new BackupRoutine object from a given reader.
func NewRoutineFromReader(r io.Reader, format SerializationFormat) (*BackupRoutine, error) {
b := &BackupRoutine{}
if err := Deserialize(b, r, format); err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/dto/base_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"gopkg.in/yaml.v3"
)

// SerializationFormat represents the format for serialization/deserialization
// SerializationFormat represents the format for serialization/deserialization.
type SerializationFormat int

const (
Expand All @@ -22,7 +22,7 @@ type Validator interface {
Validate() error
}

// Serialize handles serialization
// Serialize handles serialization.
func Serialize(v any, format SerializationFormat) ([]byte, error) {
var (
data []byte
Expand All @@ -45,7 +45,7 @@ func Serialize(v any, format SerializationFormat) ([]byte, error) {
return data, nil
}

// Deserialize handles deserialization
// Deserialize handles deserialization.
func Deserialize(v any, r io.Reader, format SerializationFormat) error {
var err error

Expand All @@ -65,7 +65,7 @@ func Deserialize(v any, r io.Reader, format SerializationFormat) error {
return nil
}

// ConvertModelsToDTO converts an array of models to an array of DTOs
// ConvertModelsToDTO converts an array of models to an array of DTOs.
func ConvertModelsToDTO[M any, D any](models []M, dtoConstructor func(*M) D) []D {
result := make([]D, len(models))
for i := range models {
Expand All @@ -74,7 +74,7 @@ func ConvertModelsToDTO[M any, D any](models []M, dtoConstructor func(*M) D) []D
return result
}

// ConvertModelMapToDTO converts a map of models to a map of DTOs
// ConvertModelMapToDTO converts a map of models to a map of DTOs.
func ConvertModelMapToDTO[M any, D any](modelMap map[string]*M, dtoConstructor func(*M) *D) map[string]*D {
result := make(map[string]*D, len(modelMap))
for key, m := range modelMap {
Expand All @@ -83,7 +83,7 @@ func ConvertModelMapToDTO[M any, D any](modelMap map[string]*M, dtoConstructor f
return result
}

// ConvertStorageMapToDTO converts a map of models to a map of DTOs
// ConvertStorageMapToDTO converts a map of models to a map of DTOs.
func ConvertStorageMapToDTO(modelMap map[string]model.Storage, config *model.BackupConfig) map[string]*Storage {
result := make(map[string]*Storage, len(modelMap))
for key, s := range modelMap {
Expand Down
3 changes: 2 additions & 1 deletion pkg/dto/compression_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/aerospike/aerospike-backup-service/v3/pkg/model"
)

// Compression modes
// Compression modes.
const (
CompressNone = "NONE"
CompressZSTD = "ZSTD"
Expand Down Expand Up @@ -35,6 +35,7 @@ func (p *CompressionPolicy) Validate() error {
if p.Level < -1 {
return fmt.Errorf("invalid compression level: %d", p.Level)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/dto/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (c *Config) fromModel(m *model.Config) {
}
}

// NewConfigFromReader creates a new Config object from a given reader
// NewConfigFromReader creates a new Config object from a given reader.
func NewConfigFromReader(r io.Reader, format SerializationFormat) (*Config, error) {
c := &Config{}
if err := Deserialize(c, r, format); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/dto/encryption_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/aerospike/aerospike-backup-service/v3/pkg/model"
)

// Encryption modes
// Encryption modes.
const (
EncryptNone = "NONE"
EncryptAES128 = "AES128"
Expand Down
1 change: 1 addition & 0 deletions pkg/dto/http_server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (s *HTTPServerConfig) Validate() error {
if s.Timeout != nil && *s.Timeout < 0 {
return fmt.Errorf("timeout cannot be negative: %d", *s.Timeout)
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/dto/logger_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (l *LoggerConfig) Validate() error {
if err := l.FileWriter.Validate(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -129,6 +130,7 @@ func (f *FileLoggerConfig) Validate() error {
if f.MaxBackups < 0 {
return fmt.Errorf("negative logger MaxBackups: %d", f.MaxBackups)
}

return nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/dto/restore_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
// operation, where Source is the original namespace name and Destination is
// the namespace name to which the backup data is to be restored.
//
// @Description RestoreNamespace specifies an alternative namespace name for the restore
// @Description operation.
// @Description RestoreNamespace specifies an alternative namespace name for the restore operation.
type RestoreNamespace struct {
// Original namespace name.
Source *string `json:"source,omitempty" example:"source-ns" validate:"required"`
Expand Down
1 change: 1 addition & 0 deletions pkg/dto/restore_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (p *RestorePolicy) Validate() error {
if p.ExtraTTL != nil && *p.ExtraTTL <= 0 {
return fmt.Errorf("extraTTL %d invalid, should be positive number", *p.ExtraTTL)
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/dto/restore_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (r *RestoreRequest) Validate() error {
if err := r.Policy.Validate(); err != nil {
return err
}

return nil
}

Expand All @@ -68,6 +69,7 @@ func (r *RestoreTimestampRequest) Validate() error {
if r.Routine == "" {
return emptyFieldValidationError(r.Routine)
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/dto/routine_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// RoutineState represent the current state of backups (full and incremental)
// @Description RoutineState represent the current state of backups (full and incremental)
// @Description RoutineState represent the current state of backups (full and incremental).
type RoutineState struct {
// Full represents the state of a full backup. Nil if no full backup is running.
Full *RunningJob `json:"full,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/dto/storage_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (s *S3Storage) toModel(config *model.Config) (*model.S3Storage, error) {
SecretAgent: agent,
}
}

return &model.S3Storage{
Path: s.Path,
Bucket: s.Bucket,
Expand Down Expand Up @@ -99,5 +100,6 @@ func newS3StorageFromModel(s *model.S3Storage, config *model.BackupConfig) *S3St
result.AccessKeyID = &s.Auth.KeyIDSecret
result.SecretAccessKey = &s.Auth.AccessKeySecret
}

return result
}
4 changes: 2 additions & 2 deletions pkg/dto/time_bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/aerospike/aerospike-backup-service/v3/pkg/model"
"github.com/aerospike/aerospike-backup-service/v3/pkg/util"
)

// TimeBounds represents a period of time between two timestamps.
Expand Down Expand Up @@ -60,6 +61,5 @@ func parseTimestamp(value string) (*time.Time, error) {
return nil, fmt.Errorf("timestamp should be positive or zero, got %d", intValue)
}

result := time.UnixMilli(intValue)
return &result, nil
return util.Ptr(time.UnixMilli(intValue)), nil
}
Loading

0 comments on commit 71474cb

Please sign in to comment.