Skip to content

Commit

Permalink
cleanup deprecated package io/ioutil (coredns#4920)
Browse files Browse the repository at this point in the history
Signed-off-by: zounengren <[email protected]>
  • Loading branch information
zouyee authored Oct 13, 2021
1 parent 44fcca0 commit 5191959
Show file tree
Hide file tree
Showing 26 changed files with 53 additions and 72 deletions.
5 changes: 2 additions & 3 deletions coremain/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package coremain
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
Expand Down Expand Up @@ -96,7 +95,7 @@ func confLoader(serverType string) (caddy.Input, error) {
return caddy.CaddyfileFromPipe(os.Stdin, serverType)
}

contents, err := ioutil.ReadFile(conf)
contents, err := os.ReadFile(conf)
if err != nil {
return nil, err
}
Expand All @@ -109,7 +108,7 @@ func confLoader(serverType string) (caddy.Input, error) {

// defaultLoader loads the Corefile from the current working directory.
func defaultLoader(serverType string) (caddy.Input, error) {
contents, err := ioutil.ReadFile(caddy.DefaultConfigFile)
contents, err := os.ReadFile(caddy.DefaultConfigFile)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
Expand Down
3 changes: 1 addition & 2 deletions directives_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main
import (
"bufio"
"go/format"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -101,7 +100,7 @@ func formatAndWrite(file string, data string) error {
return err
}

if err = ioutil.WriteFile(file, res, 0644); err != nil {
if err = os.WriteFile(file, res, 0644); err != nil {
return err
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions owners_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
Expand Down Expand Up @@ -35,7 +34,7 @@ var Owners = []string{`
// to prevent `No newline at end of file` with gofmt
golist += "\n"

if err := ioutil.WriteFile("plugin/chaos/zowners.go", []byte(golist), 0644); err != nil {
if err := os.WriteFile("plugin/chaos/zowners.go", []byte(golist), 0644); err != nil {
log.Fatal(err)
}
return
Expand Down
5 changes: 2 additions & 3 deletions plugin/auto/walk_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package auto

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -67,13 +66,13 @@ func TestWalkNonExistent(t *testing.T) {
}

func createFiles() (string, error) {
dir, err := ioutil.TempDir(os.TempDir(), "coredns")
dir, err := os.MkdirTemp(os.TempDir(), "coredns")
if err != nil {
return dir, err
}

for _, name := range dbFiles {
if err := ioutil.WriteFile(filepath.Join(dir, name), []byte(zoneContent), 0644); err != nil {
if err := os.WriteFile(filepath.Join(dir, name), []byte(zoneContent), 0644); err != nil {
return dir, err
}
}
Expand Down
9 changes: 4 additions & 5 deletions plugin/dnssec/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dnssec

import (
"io/ioutil"
"os"
"strings"
"testing"
Expand All @@ -10,19 +9,19 @@ import (
)

func TestSetupDnssec(t *testing.T) {
if err := ioutil.WriteFile("Kcluster.local.key", []byte(keypub), 0644); err != nil {
if err := os.WriteFile("Kcluster.local.key", []byte(keypub), 0644); err != nil {
t.Fatalf("Failed to write pub key file: %s", err)
}
defer func() { os.Remove("Kcluster.local.key") }()
if err := ioutil.WriteFile("Kcluster.local.private", []byte(keypriv), 0644); err != nil {
if err := os.WriteFile("Kcluster.local.private", []byte(keypriv), 0644); err != nil {
t.Fatalf("Failed to write private key file: %s", err)
}
defer func() { os.Remove("Kcluster.local.private") }()
if err := ioutil.WriteFile("ksk_Kcluster.local.key", []byte(kskpub), 0644); err != nil {
if err := os.WriteFile("ksk_Kcluster.local.key", []byte(kskpub), 0644); err != nil {
t.Fatalf("Failed to write pub key file: %s", err)
}
defer func() { os.Remove("ksk_Kcluster.local.key") }()
if err := ioutil.WriteFile("ksk_Kcluster.local.private", []byte(kskpriv), 0644); err != nil {
if err := os.WriteFile("ksk_Kcluster.local.private", []byte(kskpriv), 0644); err != nil {
t.Fatalf("Failed to write private key file: %s", err)
}
defer func() { os.Remove("ksk_Kcluster.local.private") }()
Expand Down
3 changes: 1 addition & 2 deletions plugin/file/reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package file

import (
"context"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestZoneReload(t *testing.T) {
if len(rrs) != 5 {
t.Fatalf("Expected 5 RRs, got %d", len(rrs))
}
if err := ioutil.WriteFile(fileName, []byte(reloadZone2Test), 0644); err != nil {
if err := os.WriteFile(fileName, []byte(reloadZone2Test), 0644); err != nil {
t.Fatalf("Failed to write new zone data: %s", err)
}
// Could still be racy, but we need to wait a bit for the event to be seen
Expand Down
3 changes: 1 addition & 2 deletions plugin/forward/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package forward

import (
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -126,7 +125,7 @@ func TestSetupTLS(t *testing.T) {

func TestSetupResolvconf(t *testing.T) {
const resolv = "resolv.conf"
if err := ioutil.WriteFile(resolv,
if err := os.WriteFile(resolv,
[]byte(`nameserver 10.10.255.252
nameserver 10.10.255.253`), 0666); err != nil {
t.Fatalf("Failed to write resolv.conf file: %s", err)
Expand Down
3 changes: 1 addition & 2 deletions plugin/grpc/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package grpc

import (
"io/ioutil"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -105,7 +104,7 @@ tls

func TestSetupResolvconf(t *testing.T) {
const resolv = "resolv.conf"
if err := ioutil.WriteFile(resolv,
if err := os.WriteFile(resolv,
[]byte(`nameserver 10.10.255.252
nameserver 10.10.255.253`), 0666); err != nil {
t.Fatalf("Failed to write resolv.conf file: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions plugin/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package health

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand All @@ -25,7 +25,7 @@ func TestHealth(t *testing.T) {
if response.StatusCode != 200 {
t.Errorf("Invalid status code: expecting '200', got '%d'", response.StatusCode)
}
content, err := ioutil.ReadAll(response.Body)
content, err := io.ReadAll(response.Body)
if err != nil {
t.Fatalf("Unable to get response body from %s: %v", address, err)
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package log
import (
"bytes"
"context"
"io/ioutil"
"io"
"log"
"strings"
"testing"
Expand Down Expand Up @@ -240,7 +240,7 @@ func TestLogged(t *testing.T) {
}

func BenchmarkLogged(b *testing.B) {
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)

rule := Rule{
NameScope: ".",
Expand Down
3 changes: 1 addition & 2 deletions plugin/pkg/doh/doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/miekg/dns"
Expand Down Expand Up @@ -93,7 +92,7 @@ func requestToMsgGet(req *http.Request) (*dns.Msg, error) {
}

func toMsg(r io.ReadCloser) (*dns.Msg, error) {
buf, err := ioutil.ReadAll(r)
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package log

import (
"fmt"
"io/ioutil"
"io"
golog "log"
"os"
"sync"
Expand Down Expand Up @@ -102,7 +102,7 @@ func Fatal(v ...interface{}) { log(fatal, v...); os.Exit(1) }
func Fatalf(format string, v ...interface{}) { logf(fatal, format, v...); os.Exit(1) }

// Discard sets the log output to /dev/null.
func Discard() { golog.SetOutput(ioutil.Discard) }
func Discard() { golog.SetOutput(io.Discard) }

const (
debug = "[DEBUG] "
Expand Down
3 changes: 1 addition & 2 deletions plugin/pkg/parse/host_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package parse

import (
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -61,7 +60,7 @@ func TestHostPortOrFile(t *testing.T) {
},
}

err := ioutil.WriteFile("resolv.conf", []byte("nameserver 127.0.0.1\n"), 0600)
err := os.WriteFile("resolv.conf", []byte("nameserver 127.0.0.1\n"), 0600)
if err != nil {
t.Fatalf("Failed to write test resolv.conf")
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/pkg/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"time"
)

Expand Down Expand Up @@ -95,7 +95,7 @@ func loadRoots(caPath string) (*x509.CertPool, error) {
}

roots := x509.NewCertPool()
pem, err := ioutil.ReadFile(caPath)
pem, err := os.ReadFile(caPath)
if err != nil {
return nil, fmt.Errorf("error reading %s: %s", caPath, err)
}
Expand Down
3 changes: 1 addition & 2 deletions plugin/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package root

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -24,7 +23,7 @@ func TestRoot(t *testing.T) {

nonExistingDir := filepath.Join(existingDirPath, "highly_unlikely_to_exist_dir")

existingFile, err := ioutil.TempFile("", "root_test")
existingFile, err := os.CreateTemp("", "root_test")
if err != nil {
t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions plugin/sign/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sign
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -15,7 +14,7 @@ import (

// write writes out the zone file to a temporary file which is then moved into the correct place.
func (s *Signer) write(z *file.Zone) error {
f, err := ioutil.TempFile(s.directory, "signed-")
f, err := os.CreateTemp(s.directory, "signed-")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/sign/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/ecdsa"
"crypto/rsa"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -70,7 +70,7 @@ func readKeyPair(public, private string) (Pair, error) {
if err != nil {
return Pair{}, err
}
b, err := ioutil.ReadAll(rk)
b, err := io.ReadAll(rk)
if err != nil {
return Pair{}, err
}
Expand Down
3 changes: 1 addition & 2 deletions plugin/sign/signer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sign

import (
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -50,7 +49,7 @@ $ORIGIN example.org.
@ IN SOA linode miek.miek.nl. ( 1282630060 4H 1H 7D 4H )
IN NS linode
`
if err := ioutil.WriteFile("db.apex-test.example.org", []byte(apex), 0644); err != nil {
if err := os.WriteFile("db.apex-test.example.org", []byte(apex), 0644); err != nil {
t.Fatal(err)
}
defer os.Remove("db.apex-test.example.org")
Expand Down
Loading

0 comments on commit 5191959

Please sign in to comment.