Skip to content

Commit

Permalink
chore: remove usages of the deprecated ioutil (#7332)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Chen <jc@unknwon.io>
  • Loading branch information
zachary-walters and unknwon committed Feb 4, 2023
1 parent 6d22054 commit 5887bc1
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 20 deletions.
3 changes: 1 addition & 2 deletions internal/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -66,7 +65,7 @@ func runBackup(c *cli.Context) error {
if !com.IsExist(tmpDir) {
log.Fatal("'--tempdir' does not exist: %s", tmpDir)
}
rootDir, err := ioutil.TempDir(tmpDir, "gogs-backup-")
rootDir, err := os.MkdirTemp(tmpDir, "gogs-backup-")
if err != nil {
log.Fatal("Failed to create backup root directory '%s': %v", rootDir, err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/db/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"math/big"
"os"
"path"
Expand Down Expand Up @@ -181,7 +180,7 @@ func parseKeyString(content string) (string, 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(conf.SSH.KeyTestPath, "gogs_keytest")
tmpFile, err := os.CreateTemp(conf.SSH.KeyTestPath, "gogs_keytest")
if err != nil {
return "", fmt.Errorf("TempFile: %v", err)
}
Expand Down Expand Up @@ -377,7 +376,7 @@ func addKey(e Engine, key *PublicKey) (err error) {
// Calculate fingerprint.
tmpPath := strings.ReplaceAll(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), "id_rsa.pub"), "\\", "/")
_ = os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
if err = ioutil.WriteFile(tmpPath, []byte(key.Content), 0644); err != nil {
if err = os.WriteFile(tmpPath, []byte(key.Content), 0644); err != nil {
return err
}

Expand Down
3 changes: 1 addition & 2 deletions internal/db/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package db

import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -122,7 +121,7 @@ func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, mes
// The new file we created will be in normal text format.
os.Remove(filename)

if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
if err = os.WriteFile(filename, []byte(content), 0666); err != nil {
return fmt.Errorf("WriteFile: %v", err)
}

Expand Down
6 changes: 3 additions & 3 deletions internal/lfsutil/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package lfsutil

import (
"bytes"
"io/ioutil"
"io"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestLocalStorage_Upload(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
written, err := s.Upload(test.oid, ioutil.NopCloser(strings.NewReader(test.content)))
written, err := s.Upload(test.oid, io.NopCloser(strings.NewReader(test.content)))
assert.Equal(t, test.expWritten, written)
assert.Equal(t, test.expErr, err)
})
Expand All @@ -100,7 +100,7 @@ func TestLocalStorage_Download(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(fpath, []byte("Hello world!"), os.ModePerm)
err = os.WriteFile(fpath, []byte("Hello world!"), os.ModePerm)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/route/lfs/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package lfs
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strconv"

Expand Down Expand Up @@ -82,7 +81,7 @@ func (h *basicHandler) serveUpload(c *macaron.Context, repo *db.Repository, oid
_, err := db.LFS.GetObjectByOID(c.Req.Context(), repo.ID, oid)
if err == nil {
// Object exists, drain the request body and we're good.
_, _ = io.Copy(ioutil.Discard, c.Req.Request.Body)
_, _ = io.Copy(io.Discard, c.Req.Request.Body)
c.Req.Request.Body.Close()
c.Status(http.StatusOK)
return
Expand Down Expand Up @@ -123,7 +122,8 @@ func (h *basicHandler) serveUpload(c *macaron.Context, repo *db.Repository, oid
// POST /{owner}/{repo}.git/info/lfs/object/basic/verify
func (*basicHandler) serveVerify(c *macaron.Context, repo *db.Repository) {
var request basicVerifyRequest
defer c.Req.Request.Body.Close()
defer func() { _ = c.Req.Request.Body.Close() }()

err := json.NewDecoder(c.Req.Request.Body).Decode(&request)
if err != nil {
responseJSON(c.Resp, http.StatusBadRequest, responseError{
Expand Down
3 changes: 1 addition & 2 deletions internal/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package ssh
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -190,7 +189,7 @@ func Listen(host string, port int, ciphers, macs []string) {
log.Trace("SSH: New private key is generateed: %s", keyPath)
}

privateBytes, err := ioutil.ReadFile(keyPath)
privateBytes, err := os.ReadFile(keyPath)
if err != nil {
panic("SSH: Failed to load private key: " + err.Error())
}
Expand Down
5 changes: 2 additions & 3 deletions internal/testutil/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package testutil
import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -45,13 +44,13 @@ func AssertGolden(t testing.TB, path string, update bool, got any) {
t.Fatalf("create directories for golden file %q: %v", path, err)
}

err = ioutil.WriteFile(path, data, 0640)
err = os.WriteFile(path, data, 0640)
if err != nil {
t.Fatalf("update golden file %q: %v", path, err)
}
}

golden, err := ioutil.ReadFile(path)
golden, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read golden file %q: %v", path, err)
}
Expand Down
4 changes: 2 additions & 2 deletions templates/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path"
"strings"

Expand Down Expand Up @@ -73,7 +73,7 @@ func NewTemplateFileSystem(dir, customDir string) macaron.TemplateFileSystem {
var data []byte
fpath := path.Join(customDir, name)
if osutil.IsFile(fpath) {
data, err = ioutil.ReadFile(fpath)
data, err = os.ReadFile(fpath)
} else {
data, err = files.ReadFile(name)
}
Expand Down

0 comments on commit 5887bc1

Please sign in to comment.