diff options
author | Eng Zer Jun <engzerjun@gmail.com> | 2021-09-22 13:38:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-22 13:38:34 +0800 |
commit | f2e7d5477f076789da5d0e95fe61a56ddb939f5a (patch) | |
tree | 922ca8769761c30e93f3b4deaf27858026b27ebf /models | |
parent | aa631d8cd18251aa9b18ce72f75c8d8c7090e5e7 (diff) | |
download | gitea-f2e7d5477f076789da5d0e95fe61a56ddb939f5a.tar.gz gitea-f2e7d5477f076789da5d0e95fe61a56ddb939f5a.zip |
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>
Diffstat (limited to 'models')
-rw-r--r-- | models/db/unit_tests.go | 5 | ||||
-rw-r--r-- | models/engine_test.go | 3 | ||||
-rw-r--r-- | models/fixture_test.go | 4 | ||||
-rw-r--r-- | models/helper_directory.go | 3 | ||||
-rw-r--r-- | models/migrations/v115.go | 10 | ||||
-rw-r--r-- | models/repo.go | 9 | ||||
-rw-r--r-- | models/ssh_key_parse.go | 4 |
7 files changed, 16 insertions, 22 deletions
diff --git a/models/db/unit_tests.go b/models/db/unit_tests.go index 8d834fe118..0540c9ba8a 100644 --- a/models/db/unit_tests.go +++ b/models/db/unit_tests.go @@ -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) } diff --git a/models/engine_test.go b/models/engine_test.go index 2c93667511..66e3ef099f 100644 --- a/models/engine_test.go +++ b/models/engine_test.go @@ -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 { diff --git a/models/fixture_test.go b/models/fixture_test.go index 387ee8ff57..3c6ebc0685 100644 --- a/models/fixture_test.go +++ b/models/fixture_test.go @@ -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 } diff --git a/models/helper_directory.go b/models/helper_directory.go index aed2dbcf9b..10114959ef 100644 --- a/models/helper_directory.go +++ b/models/helper_directory.go @@ -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) diff --git a/models/migrations/v115.go b/models/migrations/v115.go index 8222a8ceea..7708ed5e28 100644 --- a/models/migrations/v115.go +++ b/models/migrations/v115.go @@ -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 diff --git a/models/repo.go b/models/repo.go index 914001191a..39e5a089eb 100644 --- a/models/repo.go +++ b/models/repo.go @@ -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 { diff --git a/models/ssh_key_parse.go b/models/ssh_key_parse.go index a86b7de02a..d2c24b0a2a 100644 --- a/models/ssh_key_parse.go +++ b/models/ssh_key_parse.go @@ -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) } |