Skip to content

Commit 1aed383

Browse files
committed
Merge branch '38-linter-warnings' into 'master'
chore: check and fix suppressed linter warnings (#38) Closes #38 See merge request postgres-ai/database-lab!381
2 parents ffae354 + 67e61eb commit 1aed383

File tree

14 files changed

+80
-118
lines changed

14 files changed

+80
-118
lines changed

.golangci.yml

+15-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ linters-settings:
1313
errcheck:
1414
check-type-assertions: false
1515
check-blank: false
16-
exclude: ./errcheck_excludes.txt
16+
exclude-functions:
17+
- (*os.File).Close
18+
errorlint:
19+
errorf: true
20+
asserts: true
21+
comparison: true
1722
gofmt:
1823
simplify: true
24+
gofumpt:
25+
lang-version: "1.17"
26+
extra-rules: false
27+
gosimple:
28+
go: "1.17"
29+
checks: [ "all" ]
1930
goimports:
2031
local-prefixes: gitlab.com/postgres-ai/database-lab
2132
dupl:
@@ -29,7 +40,7 @@ linters-settings:
2940
gomnd:
3041
settings:
3142
mnd:
32-
ignored-functions: strconv.Format*,os.*
43+
ignored-functions: strconv.Format*,os.*,strconv.Parse*,strings.SplitN,bytes.SplitN
3344
revive:
3445
min-confidence: 0.8
3546
unused:
@@ -50,6 +61,8 @@ linters-settings:
5061
- hugeParam
5162
enabled-tags:
5263
- performance
64+
disabled-tags:
65+
- experimental
5366

5467
linters:
5568
enable:
@@ -99,5 +112,3 @@ issues:
99112
exclude-use-default: false
100113
max-issues-per-linter: 0
101114
max-same-issues: 0
102-
103-
new-from-rev: 9e951ebe

cmd/database-lab/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ func main() {
7575

7676
pm := pool.NewPoolManager(&cfg.PoolManager, runner)
7777
if err := pm.ReloadPools(); err != nil {
78-
log.Fatal(err.Error())
78+
log.Err(err.Error())
79+
return
7980
}
8081

8182
internalNetworkID, err := networks.Setup(ctx, dockerCLI, engProps.InstanceID, engProps.ContainerName)

errcheck_excludes.txt

-1
This file was deleted.

internal/estimator/profile.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ func (p *Profiler) RenderStat() string {
293293
return p.out.String()
294294
}
295295

296+
const (
297+
percentColumnSize = 6
298+
timingColumnSize = 12
299+
)
300+
296301
// printHeader prints stats header.
297302
func (p *Profiler) printHeader() {
298303
p.out.WriteString(fmt.Sprintf("%% time seconds wait_event\n"))
@@ -320,14 +325,16 @@ func (p *Profiler) printStat() {
320325

321326
// Print stats and calculating totals.
322327
for _, e := range eventsList {
323-
p.out.WriteString(fmt.Sprintf("%-*.2f %*.6f %s\n", 6, p.waitEventPercents[e.waitEventName], 12, e.waitEventValue, e.waitEventName))
328+
p.out.WriteString(fmt.Sprintf("%-*.2f %*.6f %s\n", percentColumnSize, p.waitEventPercents[e.waitEventName],
329+
timingColumnSize, e.waitEventValue, e.waitEventName))
330+
324331
totalPct += p.waitEventPercents[e.waitEventName]
325332
totalTime += e.waitEventValue
326333
}
327334

328335
// Print totals.
329336
p.out.WriteString("------ ------------ -----------------------------\n")
330-
p.out.WriteString(fmt.Sprintf("%-*.2f %*.6f\n", 6, totalPct, 12, totalTime))
337+
p.out.WriteString(fmt.Sprintf("%-*.2f %*.6f\n", percentColumnSize, totalPct, timingColumnSize, totalTime))
331338
}
332339

333340
// EstimateTime estimates time.

internal/provision/databases/postgres/postgres.go

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ func Stop(r runners.Runner, p *resources.Pool, name string) error {
131131
}
132132

133133
log.Msg("docker container was not found, ignore", err)
134-
135134
}
136135

137136
if _, err := r.Run("rm -rf " + p.SocketCloneDir(name) + "/*"); err != nil {

internal/provision/pool/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func NewManager(runner runners.Runner, config ManagerConfig) (FSManager, error)
8585
}
8686

8787
default:
88-
return nil, errors.New(fmt.Sprintf(`unsupported thin-clone manager specified: "%s"`, config.Pool.Mode))
88+
return nil, fmt.Errorf(`unsupported thin-clone manager specified: "%s"`, config.Pool.Mode)
8989
}
9090

9191
log.Dbg(fmt.Sprintf(`Using "%s" thin-clone manager.`, config.Pool.Mode))

internal/provision/runners/runners.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ const (
3030
sudoParams = "--non-interactive"
3131
)
3232

33+
// Runner runs commands.
3334
type Runner interface {
3435
Run(string, ...bool) (string, error)
3536
}
3637

38+
// RunnerError represents a runner error.
3739
type RunnerError struct {
3840
Msg string
3941
ExitStatus int
@@ -51,12 +53,10 @@ func NewRunnerError(command string, stderr string, e error) error {
5153
case (*exec.ExitError):
5254
// SO: https://stackoverflow.com/questions/10385551/get-exit-code-go.
5355
// The program has exited with an exit code != 0
54-
5556
// This works on both Unix and Windows. Although package
5657
// syscall is generally platform dependent, WaitStatus is
5758
// defined for both Unix and Windows and in both cases has
5859
// an ExitStatus() method with the same signature.
59-
6060
if status, ok := err.Sys().(syscall.WaitStatus); ok {
6161
exitStatus = status.ExitStatus()
6262
}
@@ -72,11 +72,12 @@ func NewRunnerError(command string, stderr string, e error) error {
7272
}
7373
}
7474

75+
// Error returns runner error.
7576
func (e RunnerError) Error() string {
7677
return e.Msg
7778
}
7879

79-
// Local.
80+
// LocalRunner represents implementation of a local runner.
8081
type LocalRunner struct {
8182
UseSudo bool
8283
}
@@ -90,6 +91,7 @@ func NewLocalRunner(useSudo bool) *LocalRunner {
9091
return r
9192
}
9293

94+
// Run executes command.
9395
func (r *LocalRunner) Run(command string, options ...bool) (string, error) {
9496
command = strings.Trim(command, " \n")
9597
if len(command) == 0 {
@@ -104,9 +106,6 @@ func (r *LocalRunner) Run(command string, options ...bool) (string, error) {
104106
log.Dbg(fmt.Sprintf(`Run(Local): "%s"`, logCommand))
105107
}
106108

107-
var out bytes.Buffer
108-
var stderr bytes.Buffer
109-
110109
if runtime.GOOS == "windows" {
111110
return "", errors.New("Windows is not supported")
112111
}
@@ -117,6 +116,11 @@ func (r *LocalRunner) Run(command string, options ...bool) (string, error) {
117116

118117
cmd := exec.Command("/bin/bash", "-c", command)
119118

119+
var (
120+
out bytes.Buffer
121+
stderr bytes.Buffer
122+
)
123+
120124
cmd.Stdout = &out
121125
cmd.Stderr = &stderr
122126

internal/provision/thinclones/lvm/lvmanager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (m *LVManager) GetSessionState(_ string) (*resources.SessionState, error) {
125125
return &resources.SessionState{}, nil
126126
}
127127

128-
// GetDiskState is not implemented.
128+
// GetFilesystemState is not implemented.
129129
func (m *LVManager) GetFilesystemState() (models.FileSystem, error) {
130130
// TODO(anatoly): Implement.
131131
return models.FileSystem{Mode: PoolMode}, nil

pkg/client/dblabapi/client.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"time"
2020

2121
"github.com/pkg/errors"
22+
2223
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
2324
"gitlab.com/postgres-ai/database-lab/v3/pkg/models"
2425
)

pkg/client/platform/client.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"strings"
1818

1919
"github.com/pkg/errors"
20+
2021
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
2122
)
2223

pkg/log/log.go

+33-29
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,21 @@
22
2019 © Postgres.ai
33
*/
44

5+
// Package log formats and prints log messages.
56
package log
67

78
import (
89
"encoding/json"
910
"fmt"
1011
"log"
1112
"os"
13+
"strings"
1214
)
1315

1416
var debugMode = true
1517

1618
var std = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)
1719

18-
const (
19-
WHITE = "\x1b[1;37m"
20-
RED = "\x1b[1;31m"
21-
GREEN = "\x1b[1;32m"
22-
YELLOW = "\x1b[1;33m"
23-
END = "\x1b[0m"
24-
OK = GREEN + "OK" + END
25-
FAIL = RED + "Fail" + END
26-
)
27-
2820
const (
2921
calldepth = 3
3022
)
@@ -33,47 +25,58 @@ func toString(i1 interface{}) string {
3325
if i1 == nil {
3426
return ""
3527
}
28+
3629
switch i2 := i1.(type) {
37-
default:
38-
return fmt.Sprint(i2)
3930
case bool:
4031
if i2 {
4132
return "true"
42-
} else {
43-
return "false"
4433
}
34+
35+
return "false"
36+
4537
case string:
4638
return i2
39+
4740
case *bool:
4841
if i2 == nil {
4942
return ""
5043
}
44+
5145
if *i2 {
5246
return "true"
53-
} else {
54-
return "false"
5547
}
48+
49+
return "false"
50+
5651
case *string:
5752
if i2 == nil {
5853
return ""
5954
}
55+
6056
return *i2
57+
6158
case *json.Number:
6259
return i2.String()
60+
6361
case json.Number:
6462
return i2.String()
63+
64+
default:
65+
return fmt.Sprint(i2)
6566
}
6667
}
6768

6869
func prepareMessage(v ...interface{}) string {
69-
message := ""
70+
builder := strings.Builder{}
71+
7072
for _, value := range v {
71-
message = message + " " + toString(value)
73+
builder.WriteString(" " + toString(value))
7274
}
73-
return message
75+
76+
return builder.String()
7477
}
7578

76-
func println(v ...interface{}) {
79+
func printLine(v ...interface{}) {
7780
_ = std.Output(calldepth, fmt.Sprintln(v...))
7881
}
7982

@@ -92,38 +95,39 @@ func SetDebug(enable bool) {
9295
}
9396
}
9497

95-
// Output message.
98+
// Msg outputs message.
9699
func Msg(v ...interface{}) {
97-
println("[INFO] " + prepareMessage(v...))
100+
printLine("[INFO] " + prepareMessage(v...))
98101
}
99102

100103
// Warn outputs a warning message.
101104
func Warn(v ...interface{}) {
102-
println("[WARNING] " + prepareMessage(v...))
105+
printLine("[WARNING] " + prepareMessage(v...))
103106
}
104107

105-
// Output debug message.
108+
// Dbg outputs debug message.
106109
func Dbg(v ...interface{}) {
107110
if debugMode {
108-
println("[DEBUG] " + prepareMessage(v...))
111+
printLine("[DEBUG] " + prepareMessage(v...))
109112
}
110113
}
111114

112-
// Output error message.
115+
// Err outputs error message.
113116
func Err(v ...interface{}) {
114-
println("[ERROR] " + prepareMessage(v...))
117+
printLine("[ERROR] " + prepareMessage(v...))
115118
}
116119

117120
// Errf outputs formatted log.
118121
func Errf(format string, v ...interface{}) {
119122
printf("[ERROR] "+format, v...)
120123
}
121124

122-
// Messages for security audit.
125+
// Audit outputs messages for security audit.
123126
func Audit(v ...interface{}) {
124-
println("[AUDIT] " + prepareMessage(v...))
127+
printLine("[AUDIT] " + prepareMessage(v...))
125128
}
126129

130+
// Fatal prints fatal message and exits.
127131
func Fatal(v ...interface{}) {
128132
log.Fatal("[FATAL] " + prepareMessage(v...))
129133
}

0 commit comments

Comments
 (0)