Skip to content

Commit

Permalink
Replace deprecated ioutil with io and os
Browse files Browse the repository at this point in the history
https://pkg.go.dev/io/ioutil is deprecated since Go 1.16.
  • Loading branch information
alexandear authored and bep committed Mar 1, 2023
1 parent 97b010f commit d453c12
Show file tree
Hide file tree
Showing 36 changed files with 112 additions and 191 deletions.
7 changes: 3 additions & 4 deletions cache/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -207,7 +206,7 @@ func (c *Cache) GetOrCreateBytes(id string, create func() ([]byte, error)) (Item

if r := c.getOrRemove(id); r != nil {
defer r.Close()
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
return info, b, err
}

Expand Down Expand Up @@ -242,7 +241,7 @@ func (c *Cache) GetBytes(id string) (ItemInfo, []byte, error) {

if r := c.getOrRemove(id); r != nil {
defer r.Close()
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
return info, b, err
}

Expand Down Expand Up @@ -314,7 +313,7 @@ func (c *Cache) getString(id string) string {
}
defer f.Close()

b, _ := ioutil.ReadAll(f)
b, _ := io.ReadAll(f)
return string(b)
}

Expand Down
23 changes: 8 additions & 15 deletions cache/filecache/filecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
Expand All @@ -44,13 +42,8 @@ func TestFileCache(t *testing.T) {
t.Parallel()
c := qt.New(t)

tempWorkingDir, err := ioutil.TempDir("", "hugo_filecache_test_work")
c.Assert(err, qt.IsNil)
defer os.Remove(tempWorkingDir)

tempCacheDir, err := ioutil.TempDir("", "hugo_filecache_test_cache")
c.Assert(err, qt.IsNil)
defer os.Remove(tempCacheDir)
tempWorkingDir := t.TempDir()
tempCacheDir := t.TempDir()

osfs := afero.NewOsFs()

Expand Down Expand Up @@ -123,7 +116,7 @@ dir = ":cacheDir/c"
io.Closer
}{
strings.NewReader(s),
ioutil.NopCloser(nil),
io.NopCloser(nil),
}, nil
}
}
Expand All @@ -138,7 +131,7 @@ dir = ":cacheDir/c"
c.Assert(err, qt.IsNil)
c.Assert(r, qt.Not(qt.IsNil))
c.Assert(info.Name, qt.Equals, "a")
b, _ := ioutil.ReadAll(r)
b, _ := io.ReadAll(r)
r.Close()
c.Assert(string(b), qt.Equals, "abc")

Expand All @@ -154,7 +147,7 @@ dir = ":cacheDir/c"

_, r, err = ca.GetOrCreate("a", rf("bcd"))
c.Assert(err, qt.IsNil)
b, _ = ioutil.ReadAll(r)
b, _ = io.ReadAll(r)
r.Close()
c.Assert(string(b), qt.Equals, "abc")
}
Expand All @@ -173,7 +166,7 @@ dir = ":cacheDir/c"
c.Assert(err, qt.IsNil)
c.Assert(r, qt.Not(qt.IsNil))
c.Assert(info.Name, qt.Equals, "mykey")
b, _ := ioutil.ReadAll(r)
b, _ := io.ReadAll(r)
r.Close()
c.Assert(string(b), qt.Equals, "Hugo is great!")

Expand Down Expand Up @@ -233,7 +226,7 @@ dir = "/cache/c"
return hugio.ToReadCloser(strings.NewReader(data)), nil
})
c.Assert(err, qt.IsNil)
b, _ := ioutil.ReadAll(r)
b, _ := io.ReadAll(r)
r.Close()
c.Assert(string(b), qt.Equals, data)
// Trigger some expiration.
Expand All @@ -260,7 +253,7 @@ func TestFileCacheReadOrCreateErrorInRead(t *testing.T) {
return errors.New("fail")
}

b, _ := ioutil.ReadAll(r)
b, _ := io.ReadAll(r)
result = string(b)

return nil
Expand Down
6 changes: 3 additions & 3 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package commands
import (
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -201,7 +201,7 @@ func newCommandeer(mustHaveConfigFile, failOnInitErr, running bool, h *hugoBuild
rebuildDebouncer = debounce.New(4 * time.Second)
}

out := ioutil.Discard
out := io.Discard
if !h.quiet {
out = os.Stdout
}
Expand All @@ -221,7 +221,7 @@ func newCommandeer(mustHaveConfigFile, failOnInitErr, running bool, h *hugoBuild
running: running,

// This will be replaced later, but we need something to log to before the configuration is read.
logger: loggers.NewLogger(jww.LevelWarn, jww.LevelError, out, ioutil.Discard, running),
logger: loggers.NewLogger(jww.LevelWarn, jww.LevelError, out, io.Discard, running),
}

return c, c.loadConfig()
Expand Down
3 changes: 1 addition & 2 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package commands

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -402,7 +401,7 @@ PostProcess: {{ $foo.RelPermalink }}

func writeFile(t testing.TB, filename, content string) {
must(t, os.MkdirAll(filepath.Dir(filename), os.FileMode(0755)))
must(t, ioutil.WriteFile(filename, []byte(content), os.FileMode(0755)))
must(t, os.WriteFile(filename, []byte(content), os.FileMode(0755)))
}

func must(t testing.TB, err error) {
Expand Down
8 changes: 4 additions & 4 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package commands
import (
"context"
"fmt"
"io/ioutil"
"io"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -138,10 +138,10 @@ func initializeConfig(mustHaveConfigFile, failOnInitErr, running bool,

func (c *commandeer) createLogger(cfg config.Provider) (loggers.Logger, error) {
var (
logHandle = ioutil.Discard
logHandle = io.Discard
logThreshold = jww.LevelWarn
logFile = cfg.GetString("logFile")
outHandle = ioutil.Discard
outHandle = io.Discard
stdoutThreshold = jww.LevelWarn
)

Expand All @@ -157,7 +157,7 @@ func (c *commandeer) createLogger(cfg config.Provider) (loggers.Logger, error) {
return nil, newSystemError("Failed to open log file:", logFile, err)
}
} else {
logHandle, err = ioutil.TempFile("", "hugo")
logHandle, err = os.CreateTemp("", "hugo")
if err != nil {
return nil, newSystemError(err)
}
Expand Down
12 changes: 6 additions & 6 deletions commands/import_jekyll.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -164,7 +164,7 @@ func (i *importCmd) importFromJekyll(cmd *cobra.Command, args []string) error {
func (i *importCmd) getJekyllDirInfo(fs afero.Fs, jekyllRoot string) (map[string]bool, bool) {
postDirs := make(map[string]bool)
hasAnyPost := false
if entries, err := ioutil.ReadDir(jekyllRoot); err == nil {
if entries, err := os.ReadDir(jekyllRoot); err == nil {
for _, entry := range entries {
if entry.IsDir() {
subDir := filepath.Join(jekyllRoot, entry.Name())
Expand All @@ -186,7 +186,7 @@ func (i *importCmd) retrieveJekyllPostDir(fs afero.Fs, dir string) (bool, bool)
return true, !isEmpty
}

if entries, err := ioutil.ReadDir(dir); err == nil {
if entries, err := os.ReadDir(dir); err == nil {
for _, entry := range entries {
if entry.IsDir() {
subDir := filepath.Join(dir, entry.Name())
Expand Down Expand Up @@ -247,7 +247,7 @@ func (i *importCmd) loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string]

defer f.Close()

b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
return nil
}
Expand Down Expand Up @@ -310,7 +310,7 @@ func (i *importCmd) copyJekyllFilesAndFolders(jekyllRoot, dest string, jekyllPos
if err != nil {
return err
}
entries, err := ioutil.ReadDir(jekyllRoot)
entries, err := os.ReadDir(jekyllRoot)
if err != nil {
return err
}
Expand Down Expand Up @@ -386,7 +386,7 @@ func convertJekyllPost(path, relPath, targetDir string, draft bool) error {
targetParentDir := filepath.Dir(targetFile)
os.MkdirAll(targetParentDir, 0777)

contentBytes, err := ioutil.ReadFile(path)
contentBytes, err := os.ReadFile(path)
if err != nil {
jww.ERROR.Println("Read file error:", path)
return err
Expand Down
3 changes: 1 addition & 2 deletions common/herrors/error_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package herrors

import (
"io"
"io/ioutil"
"path/filepath"
"strings"

Expand Down Expand Up @@ -114,7 +113,7 @@ func locateError(r io.Reader, le FileError, matches LineMatcherFn) *ErrorContext

ectx := &ErrorContext{LinesPos: -1, Position: text.Position{Offset: -1}}

b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
if err != nil {
return ectx
}
Expand Down
5 changes: 2 additions & 3 deletions common/hugio/writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package hugio

import (
"io"
"io/ioutil"
)

// As implemented by strings.Builder.
Expand Down Expand Up @@ -63,7 +62,7 @@ func ToWriteCloser(w io.Writer) io.WriteCloser {
io.Closer
}{
w,
ioutil.NopCloser(nil),
io.NopCloser(nil),
}
}

Expand All @@ -79,6 +78,6 @@ func ToReadCloser(r io.Reader) io.ReadCloser {
io.Closer
}{
r,
ioutil.NopCloser(nil),
io.NopCloser(nil),
}
}
9 changes: 4 additions & 5 deletions common/loggers/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
Expand Down Expand Up @@ -92,7 +91,7 @@ type logger struct {
*jww.Notepad

// The writer that represents stdout.
// Will be ioutil.Discard when in quiet mode.
// Will be io.Discard when in quiet mode.
out io.Writer

logCounters *LogCounters
Expand Down Expand Up @@ -232,12 +231,12 @@ func NewErrorLogger() Logger {

// NewBasicLogger creates a new basic logger writing to Stdout.
func NewBasicLogger(t jww.Threshold) Logger {
return newLogger(t, jww.LevelError, os.Stdout, ioutil.Discard, false)
return newLogger(t, jww.LevelError, os.Stdout, io.Discard, false)
}

// NewBasicLoggerForWriter creates a new basic logger writing to w.
func NewBasicLoggerForWriter(t jww.Threshold, w io.Writer) Logger {
return newLogger(t, jww.LevelError, w, ioutil.Discard, false)
return newLogger(t, jww.LevelError, w, io.Discard, false)
}

// RemoveANSIColours removes all ANSI colours from the given string.
Expand Down Expand Up @@ -291,7 +290,7 @@ func InitGlobalLogger(stdoutThreshold, logThreshold jww.Threshold, outHandle, lo

func getLogWriters(outHandle, logHandle io.Writer) (io.Writer, io.Writer) {
isTerm := terminal.PrintANSIColors(os.Stdout)
if logHandle != ioutil.Discard && isTerm {
if logHandle != io.Discard && isTerm {
// Remove any Ansi coloring from log output
logHandle = ansiCleaner{w: logHandle}
}
Expand Down
3 changes: 1 addition & 2 deletions deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"mime"
"os"
"path/filepath"
Expand Down Expand Up @@ -403,7 +402,7 @@ func (lf *localFile) Reader() (io.ReadCloser, error) {
// We've got the gzipped contents cached in gzipped.
// Note: we can't use lf.gzipped directly as a Reader, since we it discards
// data after it is read, and we may read it more than once.
return ioutil.NopCloser(bytes.NewReader(lf.gzipped.Bytes())), nil
return io.NopCloser(bytes.NewReader(lf.gzipped.Bytes())), nil
}
// Not expected to fail since we did it successfully earlier in newLocalFile,
// but could happen due to changes in the underlying filesystem.
Expand Down
Loading

0 comments on commit d453c12

Please sign in to comment.