Skip to content

Commit

Permalink
fix: remove deprecated ioutil and rand calls
Browse files Browse the repository at this point in the history
- Change ioutil to io and os calls per Go 1.16 deprecation
- Move global random to localized random source
  • Loading branch information
tydavis authored and benbjohnson committed Mar 12, 2023
1 parent 6bbced3 commit 91e57a6
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 19 deletions.
3 changes: 1 addition & 2 deletions cmd/litestream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
Expand Down Expand Up @@ -253,7 +252,7 @@ func readConfigFile(filename string, expandEnv bool) (_ Config, err error) {

// Read configuration.
// Do not return an error if using default path and file is missing.
buf, err := ioutil.ReadFile(filename)
buf, err := os.ReadFile(filename)
if err != nil {
return config, err
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/litestream/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main_test
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -23,7 +22,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 @@ -55,7 +54,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 @@ -82,7 +81,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
5 changes: 2 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -292,7 +291,7 @@ func (db *DB) invalidatePos(ctx context.Context) error {
}
defer rd.Close()

n, err := io.Copy(ioutil.Discard, lz4.NewReader(rd))
n, err := io.Copy(io.Discard, lz4.NewReader(rd))
if err != nil {
return err
}
Expand Down Expand Up @@ -671,7 +670,7 @@ func (db *DB) cleanGenerations(ctx context.Context) 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
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 @@ -111,7 +110,7 @@ func (c *FileReplicaClient) 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
13 changes: 7 additions & 6 deletions integration/replica_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"io"
"math/rand"
"os"
"path"
Expand All @@ -22,12 +22,13 @@ import (
)

func init() {
rand.Seed(time.Now().UnixNano())
localRand = rand.New(rand.NewSource(time.Now().UnixNano()))
}

var (
// Enables integration tests.
replicaType = flag.String("replica-type", "file", "")
localRand *rand.Rand
)

// S3 settings
Expand Down Expand Up @@ -193,7 +194,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 +225,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 +379,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 +410,7 @@ func TestReplicaClient_WALSegmentReader(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
3 changes: 1 addition & 2 deletions replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"sort"
Expand Down Expand Up @@ -375,7 +374,7 @@ func (r *Replica) calcPos(ctx context.Context, generation string) (pos Pos, err
}
defer rd.Close()

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

0 comments on commit 91e57a6

Please sign in to comment.