Skip to content

Commit 8dd1571

Browse files
timvaillancourtdm-2
authored andcommitted
Add golangci-lint CI action, fix gosimple, govet + unused lint errors (#1127)
* Add `golangci-lint`, fix `gosimple`, `govet` and `unused` linter complaints * Go 1.16 * Update copyright dates
1 parent 63c171d commit 8dd1571

File tree

14 files changed

+156
-74
lines changed

14 files changed

+156
-74
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: golangci-lint
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
permissions:
8+
contents: read
9+
# Optional: allow read access to pull request. Use with `only-new-issues` option.
10+
# pull-requests: read
11+
jobs:
12+
golangci:
13+
name: lint
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/setup-go@v3
17+
with:
18+
go-version: 1.16
19+
- uses: actions/checkout@v3
20+
- name: golangci-lint
21+
uses: golangci/golangci-lint-action@v3

.golangci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
run:
2+
timeout: 5m
3+
modules-download-mode: readonly
4+
5+
linters:
6+
disable:
7+
- errcheck
8+
- staticcheck
9+
enable:
10+
- gosimple
11+
- govet
12+
- unused

go/base/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 GitHub Inc.
2+
Copyright 2022 GitHub Inc.
33
See https://github.com/github/gh-ost/blob/master/LICENSE
44
*/
55

@@ -15,7 +15,7 @@ import (
1515
"sync/atomic"
1616
"time"
1717

18-
"github.com/satori/go.uuid"
18+
uuid "github.com/satori/go.uuid"
1919

2020
"github.com/github/gh-ost/go/mysql"
2121
"github.com/github/gh-ost/go/sql"

go/base/context_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
2-
Copyright 2016 GitHub Inc.
2+
Copyright 2022 GitHub Inc.
33
See https://github.com/github/gh-ost/blob/master/LICENSE
44
*/
55

66
package base
77

88
import (
9+
"io/ioutil"
10+
"os"
911
"testing"
1012
"time"
1113

@@ -56,3 +58,65 @@ func TestGetTableNames(t *testing.T) {
5658
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_tmp_ghc")
5759
}
5860
}
61+
62+
func TestReadConfigFile(t *testing.T) {
63+
{
64+
context := NewMigrationContext()
65+
context.ConfigFile = "/does/not/exist"
66+
if err := context.ReadConfigFile(); err == nil {
67+
t.Fatal("Expected .ReadConfigFile() to return an error, got nil")
68+
}
69+
}
70+
{
71+
f, err := ioutil.TempFile("", t.Name())
72+
if err != nil {
73+
t.Fatalf("Failed to create tmp file: %v", err)
74+
}
75+
defer os.Remove(f.Name())
76+
77+
f.Write([]byte("[client]"))
78+
context := NewMigrationContext()
79+
context.ConfigFile = f.Name()
80+
if err := context.ReadConfigFile(); err != nil {
81+
t.Fatalf(".ReadConfigFile() failed: %v", err)
82+
}
83+
}
84+
{
85+
f, err := ioutil.TempFile("", t.Name())
86+
if err != nil {
87+
t.Fatalf("Failed to create tmp file: %v", err)
88+
}
89+
defer os.Remove(f.Name())
90+
91+
f.Write([]byte("[client]\nuser=test\npassword=123456"))
92+
context := NewMigrationContext()
93+
context.ConfigFile = f.Name()
94+
if err := context.ReadConfigFile(); err != nil {
95+
t.Fatalf(".ReadConfigFile() failed: %v", err)
96+
}
97+
98+
if context.config.Client.User != "test" {
99+
t.Fatalf("Expected client user %q, got %q", "test", context.config.Client.User)
100+
} else if context.config.Client.Password != "123456" {
101+
t.Fatalf("Expected client password %q, got %q", "123456", context.config.Client.Password)
102+
}
103+
}
104+
{
105+
f, err := ioutil.TempFile("", t.Name())
106+
if err != nil {
107+
t.Fatalf("Failed to create tmp file: %v", err)
108+
}
109+
defer os.Remove(f.Name())
110+
111+
f.Write([]byte("[osc]\nmax_load=10"))
112+
context := NewMigrationContext()
113+
context.ConfigFile = f.Name()
114+
if err := context.ReadConfigFile(); err != nil {
115+
t.Fatalf(".ReadConfigFile() failed: %v", err)
116+
}
117+
118+
if context.config.Osc.Max_Load != "10" {
119+
t.Fatalf("Expected osc 'max_load' %q, got %q", "10", context.config.Osc.Max_Load)
120+
}
121+
}
122+
}

go/base/default_logger.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
Copyright 2022 GitHub Inc.
3+
See https://github.com/github/gh-ost/blob/master/LICENSE
4+
*/
5+
16
package base
27

38
import (
@@ -12,22 +17,18 @@ func NewDefaultLogger() *simpleLogger {
1217

1318
func (*simpleLogger) Debug(args ...interface{}) {
1419
log.Debug(args[0].(string), args[1:])
15-
return
1620
}
1721

1822
func (*simpleLogger) Debugf(format string, args ...interface{}) {
1923
log.Debugf(format, args...)
20-
return
2124
}
2225

2326
func (*simpleLogger) Info(args ...interface{}) {
2427
log.Info(args[0].(string), args[1:])
25-
return
2628
}
2729

2830
func (*simpleLogger) Infof(format string, args ...interface{}) {
2931
log.Infof(format, args...)
30-
return
3132
}
3233

3334
func (*simpleLogger) Warning(args ...interface{}) error {
@@ -64,10 +65,8 @@ func (*simpleLogger) Fatale(err error) error {
6465

6566
func (*simpleLogger) SetLevel(level log.LogLevel) {
6667
log.SetLevel(level)
67-
return
6868
}
6969

7070
func (*simpleLogger) SetPrintStackTrace(printStackTraceFlag bool) {
7171
log.SetPrintStackTrace(printStackTraceFlag)
72-
return
7372
}

go/base/utils.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 GitHub Inc.
2+
Copyright 2022 GitHub Inc.
33
See https://github.com/github/gh-ost/blob/master/LICENSE
44
*/
55

@@ -25,9 +25,7 @@ func PrettifyDurationOutput(d time.Duration) string {
2525
if d < time.Second {
2626
return "0s"
2727
}
28-
result := fmt.Sprintf("%s", d)
29-
result = prettifyDurationRegexp.ReplaceAllString(result, "")
30-
return result
28+
return prettifyDurationRegexp.ReplaceAllString(d.String(), "")
3129
}
3230

3331
func FileExists(fileName string) bool {

go/binlog/gomysql_reader.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 GitHub Inc.
2+
Copyright 2022 GitHub Inc.
33
See https://github.com/github/gh-ost/blob/master/LICENSE
44
*/
55

@@ -64,7 +64,10 @@ func (this *GoMySQLReader) ConnectBinlogStreamer(coordinates mysql.BinlogCoordin
6464
this.currentCoordinates = coordinates
6565
this.migrationContext.Log.Infof("Connecting binlog streamer at %+v", this.currentCoordinates)
6666
// Start sync with specified binlog file and position
67-
this.binlogStreamer, err = this.binlogSyncer.StartSync(gomysql.Position{this.currentCoordinates.LogFile, uint32(this.currentCoordinates.LogPos)})
67+
this.binlogStreamer, err = this.binlogSyncer.StartSync(gomysql.Position{
68+
Name: this.currentCoordinates.LogFile,
69+
Pos: uint32(this.currentCoordinates.LogPos),
70+
})
6871

6972
return err
7073
}

go/logic/hooks.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
/*
3-
Copyright 2016 GitHub Inc.
2+
Copyright 2022 GitHub Inc.
43
See https://github.com/github/gh-ost/blob/master/LICENSE
54
*/
65

@@ -72,9 +71,7 @@ func (this *HooksExecutor) applyEnvironmentVariables(extraVariables ...string) [
7271
env = append(env, fmt.Sprintf("GH_OST_HOOKS_HINT_TOKEN=%s", this.migrationContext.HooksHintToken))
7372
env = append(env, fmt.Sprintf("GH_OST_DRY_RUN=%t", this.migrationContext.Noop))
7473

75-
for _, variable := range extraVariables {
76-
env = append(env, variable)
77-
}
74+
env = append(env, extraVariables...)
7875
return env
7976
}
8077

go/logic/inspect.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,5 +805,4 @@ func (this *Inspector) getReplicationLag() (replicationLag time.Duration, err er
805805
func (this *Inspector) Teardown() {
806806
this.db.Close()
807807
this.informationSchemaDb.Close()
808-
return
809808
}

go/logic/migrator.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 GitHub Inc.
2+
Copyright 2022 GitHub Inc.
33
See https://github.com/github/gh-ost/blob/master/LICENSE
44
*/
55

@@ -176,16 +176,6 @@ func (this *Migrator) retryOperationWithExponentialBackoff(operation func() erro
176176
return err
177177
}
178178

179-
// executeAndThrottleOnError executes a given function. If it errors, it
180-
// throttles.
181-
func (this *Migrator) executeAndThrottleOnError(operation func() error) (err error) {
182-
if err := operation(); err != nil {
183-
this.throttler.throttle(nil)
184-
return err
185-
}
186-
return nil
187-
}
188-
189179
// consumeRowCopyComplete blocks on the rowCopyComplete channel once, and then
190180
// consumes and drops any further incoming events that may be left hanging.
191181
func (this *Migrator) consumeRowCopyComplete() {
@@ -823,78 +813,78 @@ func (this *Migrator) initiateStatus() error {
823813
// migration, and as response to the "status" interactive command.
824814
func (this *Migrator) printMigrationStatusHint(writers ...io.Writer) {
825815
w := io.MultiWriter(writers...)
826-
fmt.Fprintln(w, fmt.Sprintf("# Migrating %s.%s; Ghost table is %s.%s",
816+
fmt.Fprintf(w, "# Migrating %s.%s; Ghost table is %s.%s\n",
827817
sql.EscapeName(this.migrationContext.DatabaseName),
828818
sql.EscapeName(this.migrationContext.OriginalTableName),
829819
sql.EscapeName(this.migrationContext.DatabaseName),
830820
sql.EscapeName(this.migrationContext.GetGhostTableName()),
831-
))
832-
fmt.Fprintln(w, fmt.Sprintf("# Migrating %+v; inspecting %+v; executing on %+v",
821+
)
822+
fmt.Fprintf(w, "# Migrating %+v; inspecting %+v; executing on %+v\n",
833823
*this.applier.connectionConfig.ImpliedKey,
834824
*this.inspector.connectionConfig.ImpliedKey,
835825
this.migrationContext.Hostname,
836-
))
837-
fmt.Fprintln(w, fmt.Sprintf("# Migration started at %+v",
826+
)
827+
fmt.Fprintf(w, "# Migration started at %+v\n",
838828
this.migrationContext.StartTime.Format(time.RubyDate),
839-
))
829+
)
840830
maxLoad := this.migrationContext.GetMaxLoad()
841831
criticalLoad := this.migrationContext.GetCriticalLoad()
842-
fmt.Fprintln(w, fmt.Sprintf("# chunk-size: %+v; max-lag-millis: %+vms; dml-batch-size: %+v; max-load: %s; critical-load: %s; nice-ratio: %f",
832+
fmt.Fprintf(w, "# chunk-size: %+v; max-lag-millis: %+vms; dml-batch-size: %+v; max-load: %s; critical-load: %s; nice-ratio: %f\n",
843833
atomic.LoadInt64(&this.migrationContext.ChunkSize),
844834
atomic.LoadInt64(&this.migrationContext.MaxLagMillisecondsThrottleThreshold),
845835
atomic.LoadInt64(&this.migrationContext.DMLBatchSize),
846836
maxLoad.String(),
847837
criticalLoad.String(),
848838
this.migrationContext.GetNiceRatio(),
849-
))
839+
)
850840
if this.migrationContext.ThrottleFlagFile != "" {
851841
setIndicator := ""
852842
if base.FileExists(this.migrationContext.ThrottleFlagFile) {
853843
setIndicator = "[set]"
854844
}
855-
fmt.Fprintln(w, fmt.Sprintf("# throttle-flag-file: %+v %+v",
845+
fmt.Fprintf(w, "# throttle-flag-file: %+v %+v\n",
856846
this.migrationContext.ThrottleFlagFile, setIndicator,
857-
))
847+
)
858848
}
859849
if this.migrationContext.ThrottleAdditionalFlagFile != "" {
860850
setIndicator := ""
861851
if base.FileExists(this.migrationContext.ThrottleAdditionalFlagFile) {
862852
setIndicator = "[set]"
863853
}
864-
fmt.Fprintln(w, fmt.Sprintf("# throttle-additional-flag-file: %+v %+v",
854+
fmt.Fprintf(w, "# throttle-additional-flag-file: %+v %+v\n",
865855
this.migrationContext.ThrottleAdditionalFlagFile, setIndicator,
866-
))
856+
)
867857
}
868858
if throttleQuery := this.migrationContext.GetThrottleQuery(); throttleQuery != "" {
869-
fmt.Fprintln(w, fmt.Sprintf("# throttle-query: %+v",
859+
fmt.Fprintf(w, "# throttle-query: %+v\n",
870860
throttleQuery,
871-
))
861+
)
872862
}
873863
if throttleControlReplicaKeys := this.migrationContext.GetThrottleControlReplicaKeys(); throttleControlReplicaKeys.Len() > 0 {
874-
fmt.Fprintln(w, fmt.Sprintf("# throttle-control-replicas count: %+v",
864+
fmt.Fprintf(w, "# throttle-control-replicas count: %+v\n",
875865
throttleControlReplicaKeys.Len(),
876-
))
866+
)
877867
}
878868

879869
if this.migrationContext.PostponeCutOverFlagFile != "" {
880870
setIndicator := ""
881871
if base.FileExists(this.migrationContext.PostponeCutOverFlagFile) {
882872
setIndicator = "[set]"
883873
}
884-
fmt.Fprintln(w, fmt.Sprintf("# postpone-cut-over-flag-file: %+v %+v",
874+
fmt.Fprintf(w, "# postpone-cut-over-flag-file: %+v %+v\n",
885875
this.migrationContext.PostponeCutOverFlagFile, setIndicator,
886-
))
876+
)
887877
}
888878
if this.migrationContext.PanicFlagFile != "" {
889-
fmt.Fprintln(w, fmt.Sprintf("# panic-flag-file: %+v",
879+
fmt.Fprintf(w, "# panic-flag-file: %+v\n",
890880
this.migrationContext.PanicFlagFile,
891-
))
881+
)
892882
}
893-
fmt.Fprintln(w, fmt.Sprintf("# Serving on unix socket: %+v",
883+
fmt.Fprintf(w, "# Serving on unix socket: %+v\n",
894884
this.migrationContext.ServeSocketFile,
895-
))
885+
)
896886
if this.migrationContext.ServeTCPPort != 0 {
897-
fmt.Fprintln(w, fmt.Sprintf("# Serving on TCP port: %+v", this.migrationContext.ServeTCPPort))
887+
fmt.Fprintf(w, "# Serving on TCP port: %+v\n", this.migrationContext.ServeTCPPort)
898888
}
899889
}
900890

@@ -1195,7 +1185,6 @@ func (this *Migrator) iterateChunks() error {
11951185
// Enqueue copy operation; to be executed by executeWriteFuncs()
11961186
this.copyRowsQueue <- copyRowsFunc
11971187
}
1198-
return nil
11991188
}
12001189

12011190
func (this *Migrator) onApplyEventStruct(eventStruct *applyEventStruct) error {
@@ -1301,7 +1290,6 @@ func (this *Migrator) executeWriteFuncs() error {
13011290
}
13021291
}
13031292
}
1304-
return nil
13051293
}
13061294

13071295
// finalCleanup takes actions at very end of migration, dropping tables etc.

0 commit comments

Comments
 (0)