Skip to content

Commit 7cb5b07

Browse files
committed
fix linter warnings
1 parent 5881256 commit 7cb5b07

File tree

8 files changed

+19
-89
lines changed

8 files changed

+19
-89
lines changed

cmd/etcd_fdw/cli_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package main
33

44
import (
5-
"os"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
@@ -125,12 +124,8 @@ func TestCLIParsing(t *testing.T) {
125124
// TestCLIEnvironmentVariables tests that CLI can read from environment variables
126125
func TestCLIEnvironmentVariables(t *testing.T) {
127126
// Set environment variables
128-
os.Setenv("ETCD_FDW_POSTGRES_DSN", "postgres://env:pass@localhost:5432/envdb")
129-
os.Setenv("ETCD_FDW_ETCD_DSN", "etcd://localhost:2379,localhost:2380/")
130-
defer func() {
131-
os.Unsetenv("ETCD_FDW_POSTGRES_DSN")
132-
os.Unsetenv("ETCD_FDW_ETCD_DSN")
133-
}()
127+
t.Setenv("ETCD_FDW_POSTGRES_DSN", "postgres://env:pass@localhost:5432/envdb")
128+
t.Setenv("ETCD_FDW_ETCD_DSN", "etcd://localhost:2379,localhost:2380/")
134129

135130
// This will fail because ParseCLI function doesn't exist yet
136131
config, err := ParseCLI([]string{})
@@ -144,12 +139,8 @@ func TestCLIEnvironmentVariables(t *testing.T) {
144139
// TestCLIFlagPrecedence tests that command-line flags override environment variables
145140
func TestCLIFlagPrecedence(t *testing.T) {
146141
// Set environment variables
147-
os.Setenv("ETCD_FDW_POSTGRES_DSN", "postgres://env:pass@localhost:5432/envdb")
148-
os.Setenv("ETCD_FDW_ETCD_DSN", "etcd://localhost:2379/")
149-
defer func() {
150-
os.Unsetenv("ETCD_FDW_POSTGRES_DSN")
151-
os.Unsetenv("ETCD_FDW_ETCD_DSN")
152-
}()
142+
t.Setenv("ETCD_FDW_POSTGRES_DSN", "postgres://env:pass@localhost:5432/envdb")
143+
t.Setenv("ETCD_FDW_ETCD_DSN", "etcd://localhost:2379/")
153144

154145
// Command-line flags should override environment
155146
args := []string{

cmd/etcd_fdw/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func main() {
144144
if err != nil {
145145
logrus.WithError(err).Fatal("Failed to connect to etcd after retries")
146146
}
147-
defer etcdClient.Close()
147+
defer func() { _ = etcdClient.Close() }()
148148

149149
// Parse polling interval
150150
pollingInterval, err := time.ParseDuration(config.PollingInterval)

internal/log/formatter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package log provides a custom logrus formatter
12
package log
23

34
import (
@@ -11,6 +12,7 @@ import (
1112
"github.com/sirupsen/logrus"
1213
)
1314

15+
// NewFormatter creates a new custom logrus formatter
1416
func NewFormatter(noColors bool) *Formatter {
1517
return &Formatter{
1618
HideKeys: false,

internal/migrations/migrations_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestMigrationWithRealDatabase(t *testing.T) {
6969
ctx := context.Background()
7070
conn, err := pgx.Connect(ctx, dsn)
7171
require.NoError(t, err, "Should connect to test database")
72-
defer conn.Close(ctx)
72+
defer func() { _ = conn.Close(ctx) }()
7373

7474
// Apply migrations
7575
err = Apply(ctx, conn) // Use the Apply function instead of migrator method
@@ -102,7 +102,7 @@ func TestMigrationFunctions(t *testing.T) {
102102
ctx := context.Background()
103103
conn, err := pgx.Connect(ctx, dsn)
104104
require.NoError(t, err, "Should connect to test database")
105-
defer conn.Close(ctx)
105+
defer func() { _ = conn.Close(ctx) }()
106106

107107
// Apply migrations first
108108
err = Apply(ctx, conn)
@@ -163,7 +163,7 @@ func getTestDSN(t *testing.T) string {
163163

164164
// Cleanup container when test ends
165165
t.Cleanup(func() {
166-
pgContainer.Terminate(ctx)
166+
_ = pgContainer.Terminate(ctx)
167167
})
168168

169169
// Get connection string

internal/sync/etcd.go

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (c *EtcdClient) WatchPrefix(ctx context.Context, startRevision int64) clien
5454
opts = append(opts, clientv3.WithRev(startRevision+1))
5555
}
5656

57-
watchChan := c.Client.Watch(ctx, c.prefix, opts...)
57+
watchChan := c.Watch(ctx, c.prefix, opts...)
5858
logrus.WithFields(logrus.Fields{
5959
"prefix": c.prefix,
6060
"revision": startRevision,
@@ -65,7 +65,7 @@ func (c *EtcdClient) WatchPrefix(ctx context.Context, startRevision int64) clien
6565

6666
// GetAllKeys retrieves all key-value pairs with the given prefix for initial sync
6767
func (c *EtcdClient) GetAllKeys(ctx context.Context, prefix string) ([]KeyValueRecord, error) {
68-
resp, err := c.Client.Get(ctx, prefix, clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
68+
resp, err := c.Get(ctx, prefix, clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
6969
if err != nil {
7070
return nil, fmt.Errorf("failed to get all keys: %w", err)
7171
}
@@ -90,59 +90,6 @@ func (c *EtcdClient) GetAllKeys(ctx context.Context, prefix string) ([]KeyValueR
9090
return pairs, nil
9191
}
9292

93-
// Put stores a key-value pair in etcd
94-
func (c *EtcdClient) Put(ctx context.Context, key, value string) (*clientv3.PutResponse, error) {
95-
resp, err := c.Client.Put(ctx, key, value)
96-
if err != nil {
97-
return nil, fmt.Errorf("failed to put key %s: %w", key, err)
98-
}
99-
100-
logrus.WithFields(logrus.Fields{
101-
"key": key,
102-
"revision": resp.Header.Revision,
103-
}).Debug("Put key to etcd")
104-
105-
return resp, nil
106-
}
107-
108-
// Delete removes a key from etcd
109-
func (c *EtcdClient) Delete(ctx context.Context, key string) (*clientv3.DeleteResponse, error) {
110-
resp, err := c.Client.Delete(ctx, key)
111-
if err != nil {
112-
return nil, fmt.Errorf("failed to delete key %s: %w", key, err)
113-
}
114-
115-
logrus.WithFields(logrus.Fields{
116-
"key": key,
117-
"revision": resp.Header.Revision,
118-
"deleted": resp.Deleted,
119-
}).Debug("Deleted key from etcd")
120-
121-
return resp, nil
122-
}
123-
124-
// Get retrieves a single key from etcd
125-
func (c *EtcdClient) Get(ctx context.Context, key string) (*KeyValueRecord, error) {
126-
resp, err := c.Client.Get(ctx, key)
127-
if err != nil {
128-
return nil, fmt.Errorf("failed to get key %s: %w", key, err)
129-
}
130-
131-
if len(resp.Kvs) == 0 {
132-
return nil, nil // Key not found
133-
}
134-
135-
kv := resp.Kvs[0]
136-
value := string(kv.Value)
137-
138-
return &KeyValueRecord{
139-
Key: string(kv.Key),
140-
Value: value,
141-
Revision: kv.ModRevision,
142-
Tombstone: false,
143-
}, nil
144-
}
145-
14693
// NewEtcdClientWithRetry creates a new etcd client with retry logic
14794
func NewEtcdClientWithRetry(ctx context.Context, dsn string) (*EtcdClient, error) {
14895
config := DefaultRetryConfig()
@@ -158,7 +105,7 @@ func NewEtcdClientWithRetry(ctx context.Context, dsn string) (*EtcdClient, error
158105
// Test the connection
159106
if _, testErr := client.Get(ctx, "healthcheck"); testErr != nil {
160107
if client != nil {
161-
client.Close()
108+
_ = client.Close()
162109
}
163110
return testErr
164111
}
@@ -243,7 +190,7 @@ func (c *EtcdClient) WatchWithRecovery(ctx context.Context, startRevision int64)
243190
}
244191

245192
// RetryEtcdOperation retries an etcd operation with exponential backoff
246-
func RetryEtcdOperation(ctx context.Context, operation func() error, operationName string) error {
193+
func RetryEtcdOperation(ctx context.Context, operation func() error) error {
247194
config := DefaultRetryConfig()
248195
return RetryWithBackoff(ctx, config, operation)
249196
}

internal/sync/integration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ func setupTestContainers(t *testing.T) (*pgxpool.Pool, *EtcdClient, func()) {
8787

8888
cleanup := func() {
8989
pool.Close()
90-
etcdClient.Close()
91-
pgContainer.Terminate(ctx)
92-
etcdContainer.Terminate(ctx)
90+
_ = etcdClient.Close()
91+
_ = pgContainer.Terminate(ctx)
92+
_ = etcdContainer.Terminate(ctx)
9393
}
9494

9595
return pool, etcdClient, cleanup

internal/sync/postgresql.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,6 @@ func NewWithRetry(ctx context.Context, databaseURL string, callbacks ...func(*pg
196196
return pool, nil
197197
}
198198

199-
// RetryOperation retries a database operation with exponential backoff
200-
func RetryOperation(ctx context.Context, operation func() error, operationName string) error {
201-
config := DefaultRetryConfig()
202-
return RetryWithBackoff(ctx, config, operation)
203-
}
204-
205199
// InsertPendingRecord inserts a new record with revision -1 (pending sync to etcd)
206200
func InsertPendingRecord(ctx context.Context, pool PgxIface, key string, value string, tombstone bool) error {
207201
query := `

internal/sync/sync.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
clientv3 "go.etcd.io/etcd/client/v3"
1111
)
1212

13-
const InvalidRevision = -1
14-
1513
// Service orchestrates bidirectional synchronization between etcd and PostgreSQL
1614
type Service struct {
1715
pgPool PgxIface
@@ -140,8 +138,6 @@ func (s *Service) syncEtcdToPostgreSQL(ctx context.Context) error {
140138
if err != nil {
141139
logrus.WithError(err).WithField("key", string(event.Kv.Key)).Error("Failed to process etcd event after retries")
142140
// Continue processing other events rather than failing entirely
143-
} else {
144-
latestRevision = event.Kv.ModRevision
145141
}
146142
}
147143
}
@@ -261,7 +257,7 @@ func (s *Service) processPendingRecord(ctx context.Context, record KeyValueRecor
261257
}
262258
newRevision = resp.Header.Revision
263259
return nil
264-
}, "etcd_delete")
260+
})
265261

266262
if err != nil {
267263
logrus.WithError(err).WithFields(logrus.Fields{
@@ -284,7 +280,7 @@ func (s *Service) processPendingRecord(ctx context.Context, record KeyValueRecor
284280
}
285281
newRevision = resp.Header.Revision
286282
return nil
287-
}, "etcd_put")
283+
})
288284

289285
if err != nil {
290286
logrus.WithError(err).WithFields(logrus.Fields{

0 commit comments

Comments
 (0)