refactor: move from io/ioutil to io and os package (#1772)

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2021-09-27 08:55:23 +08:00
committed by GitHub
parent a2cce0ba77
commit 62af723017
25 changed files with 45 additions and 57 deletions

View File

@@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"regexp" "regexp"
"runtime" "runtime"
@@ -129,7 +129,7 @@ func makeGithubRequest(url string, output interface{}) error {
defer response.Body.Close() defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body) data, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
//lint:ignore ST1005 Github is a proper capitalized noun //lint:ignore ST1005 Github is a proper capitalized noun
return fmt.Errorf("Github API read response failed: %s", err) return fmt.Errorf("Github API read response failed: %s", err)

View File

@@ -1,8 +1,8 @@
package api package api
import ( import (
"io"
"io/fs" "io/fs"
"io/ioutil"
"os" "os"
"strings" "strings"
@@ -92,5 +92,5 @@ func getRandomPerformerImageUsingName(name, gender, customPath string) ([]byte,
} }
defer img.Close() defer img.Close()
return ioutil.ReadAll(img) return io.ReadAll(img)
} }

View File

@@ -3,7 +3,7 @@ package api
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"sync" "sync"
@@ -109,7 +109,7 @@ func (r *mutationResolver) BackupDatabase(ctx context.Context, input models.Back
if err := utils.EnsureDir(mgr.Paths.Generated.Downloads); err != nil { if err := utils.EnsureDir(mgr.Paths.Generated.Downloads); err != nil {
return nil, fmt.Errorf("could not create backup directory %v: %w", mgr.Paths.Generated.Downloads, err) return nil, fmt.Errorf("could not create backup directory %v: %w", mgr.Paths.Generated.Downloads, err)
} }
f, err := ioutil.TempFile(mgr.Paths.Generated.Downloads, "backup*.sqlite") f, err := os.CreateTemp(mgr.Paths.Generated.Downloads, "backup*.sqlite")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -7,9 +7,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path" "path"
"runtime/debug" "runtime/debug"
"strconv" "strconv"
@@ -344,12 +344,12 @@ func makeTLSConfig(c *config.Instance) (*tls.Config, error) {
return nil, errors.New("SSL key file must be present if certificate file is present") return nil, errors.New("SSL key file must be present if certificate file is present")
} }
cert, err := ioutil.ReadFile(certFile) cert, err := os.ReadFile(certFile)
if err != nil { if err != nil {
return nil, fmt.Errorf("error reading SSL certificate file %s: %s", certFile, err.Error()) return nil, fmt.Errorf("error reading SSL certificate file %s: %s", certFile, err.Error())
} }
key, err := ioutil.ReadFile(keyFile) key, err := os.ReadFile(keyFile)
if err != nil { if err != nil {
return nil, fmt.Errorf("error reading SSL key file %s: %s", keyFile, err.Error()) return nil, fmt.Errorf("error reading SSL key file %s: %s", keyFile, err.Error())
} }

View File

@@ -7,7 +7,6 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"testing" "testing"
@@ -44,7 +43,7 @@ func testTeardown(databaseFile string) {
func runTests(m *testing.M) int { func runTests(m *testing.M) int {
// create the database file // create the database file
f, err := ioutil.TempFile("", "*.sqlite") f, err := os.CreateTemp("", "*.sqlite")
if err != nil { if err != nil {
panic(fmt.Sprintf("Could not create temporary file: %s", err.Error())) panic(fmt.Sprintf("Could not create temporary file: %s", err.Error()))
} }

View File

@@ -33,7 +33,6 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
@@ -483,7 +482,7 @@ func (me *Server) contentDirectoryInitialEvent(urls []*url.URL, sid string) {
logger.Errorf("Could not notify %s: %s", _url.String(), err) logger.Errorf("Could not notify %s: %s", _url.String(), err)
continue continue
} }
b, _ := ioutil.ReadAll(resp.Body) b, _ := io.ReadAll(resp.Body)
if len(b) > 0 { if len(b) > 0 {
logger.Debug(string(b)) logger.Debug(string(b))
} }

View File

@@ -2,7 +2,7 @@ package ffmpeg
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@@ -126,7 +126,7 @@ func (e *Encoder) runTranscode(probeResult VideoFile, args []string) (string, er
} }
} }
stdoutData, _ := ioutil.ReadAll(stdout) stdoutData, _ := io.ReadAll(stdout)
stdoutString := string(stdoutData) stdoutString := string(stdoutData)
registerRunningEncoder(probeResult.Path, cmd.Process) registerRunningEncoder(probeResult.Path, cmd.Process)

View File

@@ -2,7 +2,6 @@ package ffmpeg
import ( import (
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@@ -234,7 +233,7 @@ func (e *Encoder) stream(probeResult VideoFile, options TranscodeStreamOptions)
// stderr must be consumed or the process deadlocks // stderr must be consumed or the process deadlocks
go func() { go func() {
stderrData, _ := ioutil.ReadAll(stderr) stderrData, _ := io.ReadAll(stderr)
stderrString := string(stderrData) stderrString := string(stderrData)
if len(stderrString) > 0 { if len(stderrString) > 0 {
logger.Debugf("[stream] ffmpeg stderr: %s", stderrString) logger.Debugf("[stream] ffmpeg stderr: %s", stderrString)

View File

@@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"image" "image"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@@ -247,7 +246,7 @@ func Serve(w http.ResponseWriter, r *http.Request, path string) {
} }
defer rc.Close() defer rc.Close()
data, err := ioutil.ReadAll(rc) data, err := io.ReadAll(rc)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return

View File

@@ -3,7 +3,7 @@ package config
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime" "runtime"
@@ -800,7 +800,7 @@ func (i *Instance) GetCSS() string {
return "" return ""
} }
buf, err := ioutil.ReadFile(fn) buf, err := os.ReadFile(fn)
if err != nil { if err != nil {
return "" return ""
@@ -816,7 +816,7 @@ func (i *Instance) SetCSS(css string) {
buf := []byte(css) buf := []byte(css)
if err := ioutil.WriteFile(fn, buf, 0777); err != nil { if err := os.WriteFile(fn, buf, 0777); err != nil {
logger.Warnf("error while writing %v bytes to %v: %v", len(buf), fn, err) logger.Warnf("error while writing %v bytes to %v: %v", len(buf), fn, err)
} }
} }

View File

@@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
"io/ioutil"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
@@ -141,7 +140,7 @@ func (g *SpriteGenerator) generateSpriteVTT(encoder *ffmpeg.Encoder) error {
} }
vtt := strings.Join(vttLines, "\n") vtt := strings.Join(vttLines, "\n")
return ioutil.WriteFile(g.VTTOutputPath, []byte(vtt), 0644) return os.WriteFile(g.VTTOutputPath, []byte(vtt), 0644)
} }
func (g *SpriteGenerator) imageExists() bool { func (g *SpriteGenerator) imageExists() bool {

View File

@@ -2,8 +2,7 @@ package jsonschema
import ( import (
"bytes" "bytes"
"os"
"io/ioutil"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
) )
@@ -19,7 +18,7 @@ func marshalToFile(filePath string, j interface{}) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(filePath, data, 0644) return os.WriteFile(filePath, data, 0644)
} }
func encode(j interface{}) ([]byte, error) { func encode(j interface{}) ([]byte, error) {

View File

@@ -2,7 +2,7 @@ package paths
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"github.com/stashapp/stash/pkg/logger" "github.com/stashapp/stash/pkg/logger"
@@ -54,7 +54,7 @@ func (gp *generatedPaths) TempDir(pattern string) (string, error) {
if err := gp.EnsureTmpDir(); err != nil { if err := gp.EnsureTmpDir(); err != nil {
logger.Warnf("Could not ensure existence of a temporary directory: %v", err) logger.Warnf("Could not ensure existence of a temporary directory: %v", err)
} }
ret, err := ioutil.TempDir(gp.Tmp, pattern) ret, err := os.MkdirTemp(gp.Tmp, pattern)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@@ -5,7 +5,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@@ -177,7 +176,7 @@ func (t *ExportTask) generateDownload() error {
if err := utils.EnsureDir(instance.Paths.Generated.Downloads); err != nil { if err := utils.EnsureDir(instance.Paths.Generated.Downloads); err != nil {
return err return err
} }
z, err := ioutil.TempFile(instance.Paths.Generated.Downloads, "export*.zip") z, err := os.CreateTemp(instance.Paths.Generated.Downloads, "export*.zip")
if err != nil { if err != nil {
return err return err
} }

View File

@@ -3,7 +3,7 @@ package manager
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"time" "time"
@@ -52,7 +52,7 @@ func (t *GenerateScreenshotTask) Start() {
} }
defer f.Close() defer f.Close()
coverImageData, err := ioutil.ReadAll(f) coverImageData, err := io.ReadAll(f)
if err != nil { if err != nil {
logger.Errorf("Error reading screenshot: %s", err.Error()) logger.Errorf("Error reading screenshot: %s", err.Error())
return return

View File

@@ -5,7 +5,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io"
"os" "os"
"time" "time"
@@ -23,7 +23,7 @@ func main() {
input := common.PluginInput{} input := common.PluginInput{}
if len(os.Args) < 2 { if len(os.Args) < 2 {
inData, _ := ioutil.ReadAll(os.Stdin) inData, _ := io.ReadAll(os.Stdin)
log.Debugf("Raw input: %s", string(inData)) log.Debugf("Raw input: %s", string(inData))
decodeErr := json.Unmarshal(inData, &input) decodeErr := json.Unmarshal(inData, &input)

View File

@@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os/exec" "os/exec"
"sync" "sync"
@@ -79,7 +78,7 @@ func (t *rawPluginTask) Start() error {
go func() { go func() {
defer t.waitGroup.Done() defer t.waitGroup.Done()
defer close(t.done) defer close(t.done)
stdoutData, _ := ioutil.ReadAll(stdout) stdoutData, _ := io.ReadAll(stdout)
stdoutString := string(stdoutData) stdoutString := string(stdoutData)
output := t.getOutput(stdoutString) output := t.getOutput(stdoutString)

View File

@@ -3,7 +3,7 @@ package scraper
import ( import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@@ -121,7 +121,7 @@ func getImage(url string, globalConfig GlobalConfig) (*string, error) {
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -2,7 +2,7 @@ package scraper
import ( import (
"errors" "errors"
"io/ioutil" "io"
"net/url" "net/url"
"strings" "strings"
@@ -53,7 +53,7 @@ func (s *jsonScraper) loadURL(url string) (string, error) {
return "", err return "", err
} }
logger.Infof("loadURL (%s)\n", url) logger.Infof("loadURL (%s)\n", url)
doc, err := ioutil.ReadAll(r) doc, err := io.ReadAll(r)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@@ -3,7 +3,7 @@ package stashbox
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@@ -522,7 +522,7 @@ func fetchImage(url string) (*string, error) {
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -7,7 +7,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"os" "os"
@@ -92,7 +91,7 @@ func loadURL(url string, scraperConfig config, globalConfig GlobalConfig) (io.Re
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -140,7 +139,7 @@ func urlFromCDP(url string, driverOptions scraperDriverOptions, globalConfig Glo
act, cancelAct = chromedp.NewRemoteAllocator(context.Background(), remote) act, cancelAct = chromedp.NewRemoteAllocator(context.Background(), remote)
} else { } else {
// use a temporary user directory for chrome // use a temporary user directory for chrome
dir, err := ioutil.TempDir("", "stash-chromedp") dir, err := os.MkdirTemp("", "stash-chromedp")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -8,7 +8,6 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"testing" "testing"
@@ -381,7 +380,7 @@ func testTeardown(databaseFile string) {
func runTests(m *testing.M) int { func runTests(m *testing.M) int {
// create the database file // create the database file
f, err := ioutil.TempFile("", "*.sqlite") f, err := os.CreateTemp("", "*.sqlite")
if err != nil { if err != nil {
panic(fmt.Sprintf("Could not create temporary file: %s", err.Error())) panic(fmt.Sprintf("Could not create temporary file: %s", err.Error()))
} }

View File

@@ -4,7 +4,6 @@ import (
"archive/zip" "archive/zip"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/user" "os/user"
@@ -108,10 +107,10 @@ func EmptyDir(path string) error {
// ListDir will return the contents of a given directory path as a string slice // ListDir will return the contents of a given directory path as a string slice
func ListDir(path string) ([]string, error) { func ListDir(path string) ([]string, error) {
var dirPaths []string var dirPaths []string
files, err := ioutil.ReadDir(path) files, err := os.ReadDir(path)
if err != nil { if err != nil {
path = filepath.Dir(path) path = filepath.Dir(path)
files, err = ioutil.ReadDir(path) files, err = os.ReadDir(path)
if err != nil { if err != nil {
return dirPaths, err return dirPaths, err
} }
@@ -196,7 +195,7 @@ func WriteFile(path string, file []byte) error {
return fmt.Errorf("cannot ensure path %s", pathErr) return fmt.Errorf("cannot ensure path %s", pathErr)
} }
err := ioutil.WriteFile(path, file, 0755) err := os.WriteFile(path, file, 0755)
if err != nil { if err != nil {
return fmt.Errorf("write error for thumbnail %s: %s ", path, err) return fmt.Errorf("write error for thumbnail %s: %s ", path, err)
} }

View File

@@ -1,7 +1,6 @@
package utils package utils
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -53,11 +52,11 @@ func TestDirExists(t *testing.T) {
const st = "stash_tmp" const st = "stash_tmp"
tmp := os.TempDir() tmp := os.TempDir()
tmpDir, err := ioutil.TempDir(tmp, st) // create a tmp dir in the system's tmp folder tmpDir, err := os.MkdirTemp(tmp, st) // create a tmp dir in the system's tmp folder
if err == nil { if err == nil {
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
tmpFile, err := ioutil.TempFile(tmpDir, st) tmpFile, err := os.CreateTemp(tmpDir, st)
if err != nil { if err != nil {
return return
} }

View File

@@ -5,7 +5,7 @@ import (
"crypto/tls" "crypto/tls"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@@ -66,7 +66,7 @@ func ReadImageFromURL(url string) ([]byte, error) {
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }