Skip to content

Commit

Permalink
Enable staticcheck, fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hifi committed Dec 15, 2023
1 parent 1a96ad4 commit 38a5a22
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ repos:
- "github.com/benbjohnson/litestrem"
- "-w"
- id: go-vet-repo-mod
# - id: go-staticcheck-repo-mod
- id: go-staticcheck-repo-mod
3 changes: 1 addition & 2 deletions cmd/litestream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log/slog"
"net/url"
"os"
Expand Down Expand Up @@ -223,7 +222,7 @@ func ReadConfigFile(filename string, expandEnv bool) (_ Config, err error) {
}

// Read configuration.
buf, err := ioutil.ReadFile(filename)
buf, err := os.ReadFile(filename)
if os.IsNotExist(err) {
return config, fmt.Errorf("config file not found: %s", filename)
} else if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions cmd/litestream/main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -16,7 +15,7 @@ func TestReadConfigFile(t *testing.T) {
// Ensure global AWS settings are propagated down to replica configurations.
t.Run("PropagateGlobalSettings", func(t *testing.T) {
filename := filepath.Join(t.TempDir(), "litestream.yml")
if err := ioutil.WriteFile(filename, []byte(`
if err := os.WriteFile(filename, []byte(`
access-key-id: XXX
secret-access-key: YYY
Expand Down Expand Up @@ -48,7 +47,7 @@ dbs:
os.Setenv("LITESTREAM_TEST_1872363", "s3://foo/bar")

filename := filepath.Join(t.TempDir(), "litestream.yml")
if err := ioutil.WriteFile(filename, []byte(`
if err := os.WriteFile(filename, []byte(`
dbs:
- path: $LITESTREAM_TEST_0129380
replicas:
Expand All @@ -75,7 +74,7 @@ dbs:
os.Setenv("LITESTREAM_TEST_9847533", "s3://foo/bar")

filename := filepath.Join(t.TempDir(), "litestream.yml")
if err := ioutil.WriteFile(filename, []byte(`
if err := os.WriteFile(filename, []byte(`
dbs:
- path: /path/to/db
replicas:
Expand Down
20 changes: 12 additions & 8 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"hash/crc64"
"io"
"io/ioutil"
"log/slog"
"math"
"math/rand"
Expand Down Expand Up @@ -207,15 +206,20 @@ func (db *DB) CurrentShadowWALPath(generation string) (string, error) {

// CurrentShadowWALIndex returns the current WAL index & total size.
func (db *DB) CurrentShadowWALIndex(generation string) (index int, size int64, err error) {
fis, err := ioutil.ReadDir(filepath.Join(db.GenerationPath(generation), "wal"))
des, err := os.ReadDir(filepath.Join(db.GenerationPath(generation), "wal"))
if os.IsNotExist(err) {
return 0, 0, nil // no wal files written for generation
} else if err != nil {
return 0, 0, err
}

// Find highest wal index.
for _, fi := range fis {
for _, de := range des {
fi, err := de.Info()
if err != nil {
return 0, 0, err
}

if v, err := ParseWALPath(fi.Name()); err != nil {
continue // invalid wal filename
} else if v > index {
Expand Down Expand Up @@ -546,7 +550,7 @@ func (db *DB) cleanGenerations() error {
}

dir := filepath.Join(db.metaPath, "generations")
fis, err := ioutil.ReadDir(dir)
fis, err := os.ReadDir(dir)
if os.IsNotExist(err) {
return nil
} else if err != nil {
Expand Down Expand Up @@ -593,7 +597,7 @@ func (db *DB) cleanWAL() error {

// Remove all WAL files for the generation before the lowest index.
dir := db.ShadowWALDir(generation)
fis, err := ioutil.ReadDir(dir)
fis, err := os.ReadDir(dir)
if os.IsNotExist(err) {
return nil
} else if err != nil {
Expand Down Expand Up @@ -649,7 +653,7 @@ func (db *DB) releaseReadLock() error {
// CurrentGeneration returns the name of the generation saved to the "generation"
// file in the meta data directory. Returns empty string if none exists.
func (db *DB) CurrentGeneration() (string, error) {
buf, err := ioutil.ReadFile(db.GenerationNamePath())
buf, err := os.ReadFile(db.GenerationNamePath())
if os.IsNotExist(err) {
return "", nil
} else if err != nil {
Expand Down Expand Up @@ -691,7 +695,7 @@ func (db *DB) createGeneration() (string, error) {
if db.fileInfo != nil {
mode = db.fileInfo.Mode()
}
if err := ioutil.WriteFile(generationNamePath+".tmp", []byte(generation+"\n"), mode); err != nil {
if err := os.WriteFile(generationNamePath+".tmp", []byte(generation+"\n"), mode); err != nil {
return "", fmt.Errorf("write generation temp file: %w", err)
}
uid, gid := internal.Fileinfo(db.fileInfo)
Expand Down Expand Up @@ -986,7 +990,7 @@ func (db *DB) initShadowWALFile(filename string) (int64, error) {
}
if err := internal.MkdirAll(filepath.Dir(filename), db.dirInfo); err != nil {
return 0, err
} else if err := ioutil.WriteFile(filename, hdr, mode); err != nil {
} else if err := os.WriteFile(filename, hdr, mode); err != nil {
return 0, err
}
uid, gid := internal.Fileinfo(db.fileInfo)
Expand Down
5 changes: 2 additions & 3 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package litestream_test
import (
"context"
"database/sql"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -374,9 +373,9 @@ func TestDB_Sync(t *testing.T) {
// Read existing file, update header checksum, and write back only header
// to simulate a header with a mismatched checksum.
shadowWALPath := db.ShadowWALPath(pos0.Generation, pos0.Index)
if buf, err := ioutil.ReadFile(shadowWALPath); err != nil {
if buf, err := os.ReadFile(shadowWALPath); err != nil {
t.Fatal(err)
} else if err := ioutil.WriteFile(shadowWALPath, append(buf[:litestream.WALHeaderSize-8], 0, 0, 0, 0, 0, 0, 0, 0), 0600); err != nil {
} else if err := os.WriteFile(shadowWALPath, append(buf[:litestream.WALHeaderSize-8], 0, 0, 0, 0, 0, 0, 0, 0), 0600); err != nil {
t.Fatal(err)
}

Expand Down
3 changes: 1 addition & 2 deletions file/replica_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -112,7 +111,7 @@ func (c *ReplicaClient) Generations(ctx context.Context) ([]string, error) {
return nil, fmt.Errorf("cannot determine generations path: %w", err)
}

fis, err := ioutil.ReadDir(root)
fis, err := os.ReadDir(root)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"hash/crc64"
"io"
"io/ioutil"
"log/slog"
"math"
"os"
Expand Down Expand Up @@ -380,7 +379,7 @@ func (r *Replica) calcPos(ctx context.Context, generation string) (pos Pos, err
rd = io.NopCloser(drd)
}

n, err := io.Copy(ioutil.Discard, lz4.NewReader(rd))
n, err := io.Copy(io.Discard, lz4.NewReader(rd))
if err != nil {
return pos, err
}
Expand Down Expand Up @@ -812,7 +811,7 @@ func (r *Replica) Validate(ctx context.Context) error {
db := r.DB()

// Restore replica to a temporary directory.
tmpdir, err := ioutil.TempDir("", "*-litestream")
tmpdir, err := os.MkdirTemp("", "*-litestream")
if err != nil {
return err
}
Expand Down Expand Up @@ -1086,7 +1085,7 @@ func (r *Replica) Restore(ctx context.Context, opt RestoreOptions) (err error) {
}

// Ensure that we found the specific index, if one was specified.
if opt.Index != math.MaxInt32 && opt.Index != opt.Index {
if opt.Index != math.MaxInt32 && opt.Index != maxWALIndex {
return fmt.Errorf("unable to locate index %d in generation %q, highest index was %d", opt.Index, opt.Generation, maxWALIndex)
}

Expand Down Expand Up @@ -1266,7 +1265,7 @@ func (r *Replica) SnapshotIndexByIndex(ctx context.Context, generation string, i
}

// Use snapshot if it newer.
if snapshotIndex == -1 || snapshotIndex >= snapshotIndex {
if snapshotIndex == -1 || snapshot.Index >= snapshotIndex {
snapshotIndex = snapshot.Index
}
}
Expand Down
15 changes: 5 additions & 10 deletions replica_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"io"
"math/rand"
"os"
"path"
"reflect"
"sort"
"strings"
"testing"
"time"

"github.com/benbjohnson/litestream"
"github.com/benbjohnson/litestream/abs"
Expand All @@ -22,10 +21,6 @@ import (
"github.com/benbjohnson/litestream/sftp"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

var (
// Enables integration tests.
integration = flag.String("integration", "file", "")
Expand Down Expand Up @@ -193,7 +188,7 @@ func TestReplicaClient_WriteSnapshot(t *testing.T) {

if r, err := c.SnapshotReader(context.Background(), "b16ddcf5c697540f", 1000); err != nil {
t.Fatal(err)
} else if buf, err := ioutil.ReadAll(r); err != nil {
} else if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err)
} else if err := r.Close(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -224,7 +219,7 @@ func TestReplicaClient_SnapshotReader(t *testing.T) {
}
defer r.Close()

if buf, err := ioutil.ReadAll(r); err != nil {
if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err)
} else if got, want := string(buf), "foo"; got != want {
t.Fatalf("ReadAll=%v, want %v", got, want)
Expand Down Expand Up @@ -378,7 +373,7 @@ func TestReplicaClient_WriteWALSegment(t *testing.T) {

if r, err := c.WALSegmentReader(context.Background(), litestream.Pos{Generation: "b16ddcf5c697540f", Index: 1000, Offset: 2000}); err != nil {
t.Fatal(err)
} else if buf, err := ioutil.ReadAll(r); err != nil {
} else if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err)
} else if err := r.Close(); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -409,7 +404,7 @@ func TestReplicaClient_WALReader(t *testing.T) {
}
defer r.Close()

if buf, err := ioutil.ReadAll(r); err != nil {
if buf, err := io.ReadAll(r); err != nil {
t.Fatal(err)
} else if got, want := string(buf), "foobar"; got != want {
t.Fatalf("ReadAll=%v, want %v", got, want)
Expand Down

0 comments on commit 38a5a22

Please sign in to comment.