1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-05-11 05:55:29 +00:00

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

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>

Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
Eng Zer Jun
2021-09-22 13:38:34 +08:00
committed by GitHub
parent aa631d8cd1
commit f2e7d5477f
103 changed files with 261 additions and 314 deletions

View File

@@ -6,7 +6,6 @@ package db
import (
"fmt"
"io/ioutil"
"math"
"net/url"
"os"
@@ -57,11 +56,11 @@ func MainTest(m *testing.M, pathToGiteaRoot string) {
setting.SSH.Port = 3000
setting.SSH.Domain = "try.gitea.io"
setting.Database.UseSQLite3 = true
setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos")
setting.RepoRootPath, err = os.MkdirTemp(os.TempDir(), "repos")
if err != nil {
fatalTestError("TempDir: %v\n", err)
}
setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata")
setting.AppDataPath, err = os.MkdirTemp(os.TempDir(), "appdata")
if err != nil {
fatalTestError("TempDir: %v\n", err)
}

View File

@@ -5,7 +5,6 @@
package models
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -19,7 +18,7 @@ import (
func TestDumpDatabase(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
dir, err := ioutil.TempDir(os.TempDir(), "dump")
dir, err := os.MkdirTemp(os.TempDir(), "dump")
assert.NoError(t, err)
type Version struct {

View File

@@ -5,7 +5,7 @@
package models
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -23,7 +23,7 @@ func TestFixtureGeneration(t *testing.T) {
if !assert.NoError(t, err) {
return
}
bytes, err := ioutil.ReadFile(filepath.Join(db.FixturesDir(), name+".yml"))
bytes, err := os.ReadFile(filepath.Join(db.FixturesDir(), name+".yml"))
if !assert.NoError(t, err) {
return
}

View File

@@ -6,7 +6,6 @@ package models
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
@@ -30,7 +29,7 @@ func CreateTemporaryPath(prefix string) (string, error) {
log.Error("Unable to create localcopypath directory: %s (%v)", LocalCopyPath(), err)
return "", fmt.Errorf("Failed to create localcopypath directory %s: %v", LocalCopyPath(), err)
}
basePath, err := ioutil.TempDir(LocalCopyPath(), prefix+".git")
basePath, err := os.MkdirTemp(LocalCopyPath(), prefix+".git")
if err != nil {
log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err)
return "", fmt.Errorf("Failed to create dir %s-*.git: %v", prefix, err)

View File

@@ -7,7 +7,7 @@ package migrations
import (
"crypto/md5"
"fmt"
"io/ioutil"
"io"
"math"
"os"
"path/filepath"
@@ -141,9 +141,9 @@ func copyOldAvatarToNewLocation(userID int64, oldAvatar string) (string, error)
}
defer fr.Close()
data, err := ioutil.ReadAll(fr)
data, err := io.ReadAll(fr)
if err != nil {
return "", fmt.Errorf("ioutil.ReadAll: %v", err)
return "", fmt.Errorf("io.ReadAll: %v", err)
}
newAvatar := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%d-%x", userID, md5.Sum(data)))))
@@ -151,8 +151,8 @@ func copyOldAvatarToNewLocation(userID int64, oldAvatar string) (string, error)
return newAvatar, nil
}
if err := ioutil.WriteFile(filepath.Join(setting.Avatar.Path, newAvatar), data, 0o666); err != nil {
return "", fmt.Errorf("ioutil.WriteFile: %v", err)
if err := os.WriteFile(filepath.Join(setting.Avatar.Path, newAvatar), data, 0o666); err != nil {
return "", fmt.Errorf("os.WriteFile: %v", err)
}
return newAvatar, nil

View File

@@ -10,11 +10,7 @@ import (
"errors"
"fmt"
"html/template"
"unicode/utf8"
// Needed for jpeg support
_ "image/jpeg"
"io/ioutil"
_ "image/jpeg" // Needed for jpeg support
"net"
"net/url"
"os"
@@ -24,6 +20,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/lfs"
@@ -1013,7 +1010,7 @@ func GetRepoInitFile(tp, name string) ([]byte, error) {
log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
}
if isFile {
return ioutil.ReadFile(customPath)
return os.ReadFile(customPath)
}
switch tp {

View File

@@ -13,8 +13,8 @@ import (
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"strconv"
"strings"
@@ -263,7 +263,7 @@ func SSHNativeParsePublicKey(keyLine string) (string, int, error) {
// writeTmpKeyFile writes key content to a temporary file
// and returns the name of that file, along with any possible errors.
func writeTmpKeyFile(content string) (string, error) {
tmpFile, err := ioutil.TempFile(setting.SSH.KeyTestPath, "gitea_keytest")
tmpFile, err := os.CreateTemp(setting.SSH.KeyTestPath, "gitea_keytest")
if err != nil {
return "", fmt.Errorf("TempFile: %v", err)
}