Skip to content

Commit

Permalink
fix: lint and test
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 20, 2023
1 parent 4ab4f27 commit 58a13f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 66 deletions.
10 changes: 8 additions & 2 deletions app/services/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ func (s *BackupImpl) MysqlRestore(database string, backupFile string) error {
if err != nil {
return err
}
defer tools.Remove(tempDir)

if !strings.HasSuffix(backupFile, ".sql") {
backupFile = "" // 置空,防止干扰后续判断
Expand Down Expand Up @@ -237,6 +236,10 @@ func (s *BackupImpl) MysqlRestore(database string, backupFile string) error {
return err
}

if err = tools.Remove(tempDir); err != nil {
return err
}

return os.Unsetenv("MYSQL_PWD")
}

Expand Down Expand Up @@ -305,7 +308,6 @@ func (s *BackupImpl) PostgresqlRestore(database string, backupFile string) error
if err != nil {
return err
}
defer tools.Remove(tempDir)

if !strings.HasSuffix(backupFile, ".sql") {
backupFile = "" // 置空,防止干扰后续判断
Expand Down Expand Up @@ -334,5 +336,9 @@ func (s *BackupImpl) PostgresqlRestore(database string, backupFile string) error
return err
}

if err = tools.Remove(tempDir); err != nil {
return err
}

return nil
}
52 changes: 5 additions & 47 deletions pkg/tools/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

"github.com/goravel/framework/support"
"github.com/mholt/archiver/v3"
"github.com/spf13/cast"
)

// Write 写入文件
Expand Down Expand Up @@ -101,52 +98,13 @@ func Chmod(path string, permission os.FileMode) error {
}

// Chown 修改文件或目录所有者
func Chown(path, userName, groupName string) error {
func Chown(path, user, group string) error {
if IsWindows() {
return errors.New("chown is not supported on Windows")
}

usr, err := user.Lookup(userName)
if err != nil {
return err
}

grp, err := user.LookupGroup(groupName)
if err != nil {
return err
}

return chownR(path, cast.ToInt(usr.Uid), cast.ToInt(grp.Gid))
}

// chownR 递归修改文件或目录的所有者
func chownR(path string, uid, gid int) error {
err := os.Chown(path, uid, gid)
if err != nil {
return err
}

fileInfo, err := os.Stat(path)
if err != nil {
return err
}

if fileInfo.IsDir() {
entries, err := os.ReadDir(path)
if err != nil {
return err
}

for _, entry := range entries {
subPath := path + "/" + entry.Name()
err = chownR(subPath, uid, gid)
if err != nil {
return err
}
}
}

return nil
cmd := exec.Command("chown", "-R", user+":"+group, path)
return cmd.Run()
}

// Exists 判断路径是否存在
Expand All @@ -171,7 +129,7 @@ func Mv(src, dst string) error {
// 如果在不同的文件系统中移动文件,os.Rename 可能会失败
// 在这种情况下,可以先复制然后删除原文件
if os.IsExist(err) {
err := Cp(src, dst)
err = Cp(src, dst)
if err != nil {
return err
}
Expand Down Expand Up @@ -222,7 +180,7 @@ func copyDir(src, dst string) error {
return err
}

entries, err := ioutil.ReadDir(src)
entries, err := os.ReadDir(src)
if err != nil {
return err
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/tools/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ func TestSystemHelperTestSuite(t *testing.T) {

func (s *SystemHelperTestSuite) TestWrite() {
filePath, _ := TempFile("testfile")
defer Remove(filePath.Name())

s.Nil(Write(filePath.Name(), "test data", 0644))
s.FileExists(filePath.Name())

content, _ := Read(filePath.Name())
s.Equal("test data", content)
s.Nil(Remove(filePath.Name()))
}

func (s *SystemHelperTestSuite) TestRead() {
filePath, _ := TempFile("testfile")
defer Remove(filePath.Name())

err := Write(filePath.Name(), "test data", 0644)
s.Nil(err)

data, err := Read(filePath.Name())
s.Nil(err)
s.Equal("test data", data)
s.Nil(Remove(filePath.Name()))
}

func (s *SystemHelperTestSuite) TestRemove() {
Expand Down Expand Up @@ -80,24 +80,23 @@ func (s *SystemHelperTestSuite) TestExecAsync() {

func (s *SystemHelperTestSuite) TestMkdir() {
dirPath, _ := TempDir("testdir")
defer Remove(dirPath)

s.Nil(Mkdir(dirPath, 0755))
s.Nil(Remove(dirPath))
}

func (s *SystemHelperTestSuite) TestChmod() {
filePath, _ := TempFile("testfile")
defer Remove(filePath.Name())

err := Write(filePath.Name(), "test data", 0644)
s.Nil(err)

s.Nil(Chmod(filePath.Name(), 0755))
s.Nil(Remove(filePath.Name()))
}

func (s *SystemHelperTestSuite) TestChown() {
filePath, _ := TempFile("testfile")
defer Remove(filePath.Name())

err := Write(filePath.Name(), "test data", 0644)
s.Nil(err)
Expand All @@ -113,6 +112,7 @@ func (s *SystemHelperTestSuite) TestChown() {
} else {
s.Nil(err)
}
s.Nil(Remove(filePath.Name()))
}

func (s *SystemHelperTestSuite) TestExists() {
Expand All @@ -125,73 +125,71 @@ func (s *SystemHelperTestSuite) TestExists() {

func (s *SystemHelperTestSuite) TestEmpty() {
filePath, _ := TempFile("testfile")
defer Remove(filePath.Name())

s.True(Empty(filePath.Name()))
if IsWindows() {
s.True(Empty("C:\\Windows\\System32\\drivers\\etc\\hosts"))
} else {
s.True(Empty("/etc/hosts"))
}
s.Nil(Remove(filePath.Name()))
}

func (s *SystemHelperTestSuite) TestMv() {
filePath, _ := TempFile("testfile")
defer Remove(filePath.Name())

err := Write(filePath.Name(), "test data", 0644)
s.Nil(err)

newFilePath, _ := TempFile("testfile2")
defer Remove(newFilePath.Name())

filePath.Close()
newFilePath.Close()

s.Nil(Mv(filePath.Name(), newFilePath.Name()))
s.False(Exists(filePath.Name()))
s.Nil(Remove(newFilePath.Name()))
s.Nil(Remove(filePath.Name()))
}

func (s *SystemHelperTestSuite) TestCp() {
tempDir, _ := TempDir("testdir")
defer Remove(tempDir)

err := Write(filepath.Join(tempDir, "testfile"), "test data", 0644)
s.Nil(err)

s.Nil(Cp(filepath.Join(tempDir, "testfile"), filepath.Join(tempDir, "testfile2")))
s.True(Exists(filepath.Join(tempDir, "testfile2")))
s.Nil(Remove(tempDir))
}

func (s *SystemHelperTestSuite) TestSize() {
filePath, _ := TempFile("testfile")
defer Remove(filePath.Name())

err := Write(filePath.Name(), "test data", 0644)
s.Nil(err)

size, err := Size(filePath.Name())
s.Nil(err)
s.Equal(int64(len("test data")), size)
s.Nil(Remove(filePath.Name()))
}

func (s *SystemHelperTestSuite) TestFileInfo() {
filePath, _ := TempFile("testfile")
defer Remove(filePath.Name())

err := Write(filePath.Name(), "test data", 0644)
s.Nil(err)

info, err := FileInfo(filePath.Name())
s.Nil(err)
s.Equal(filepath.Base(filePath.Name()), info.Name())
s.Nil(Remove(filePath.Name()))
}

func (s *SystemHelperTestSuite) TestUnArchiveSuccessfullyUnarchivesFile() {
file, _ := TempFile("test")
defer Remove(file.Name())
dstDir, _ := TempDir("archive")
defer Remove(dstDir)

err := Write(file.Name(), "test data", 0644)
s.Nil(err)
Expand All @@ -203,36 +201,38 @@ func (s *SystemHelperTestSuite) TestUnArchiveSuccessfullyUnarchivesFile() {
err = UnArchive(filepath.Join(dstDir, "test.zip"), dstDir)
s.Nil(err)
s.FileExists(filepath.Join(dstDir, filepath.Base(file.Name())))
s.Nil(Remove(file.Name()))
s.Nil(Remove(dstDir))
}

func (s *SystemHelperTestSuite) TestUnArchiveFailsForNonExistentFile() {
srcFile := "nonexistent.zip"
dstDir, _ := TempDir("unarchived")
defer Remove(dstDir)

err := UnArchive(srcFile, dstDir)
s.NotNil(err)
s.Nil(Remove(dstDir))
}

func (s *SystemHelperTestSuite) TestArchiveSuccessfullyArchivesFiles() {
srcFile, _ := TempFile("test")
defer Remove(srcFile.Name())
dstDir, _ := TempDir("archive")
defer Remove(dstDir)

err := Write(srcFile.Name(), "test data", 0644)
s.Nil(err)

err = Archive([]string{srcFile.Name()}, filepath.Join(dstDir, "test.zip"))
s.Nil(err)
s.FileExists(filepath.Join(dstDir, "test.zip"))
s.Nil(Remove(srcFile.Name()))
s.Nil(Remove(dstDir))
}

func (s *SystemHelperTestSuite) TestArchiveFailsForNonExistentFiles() {
srcFile := "nonexistent"
dstDir, _ := TempDir("archive")
defer Remove(dstDir)

err := Archive([]string{srcFile}, filepath.Join(dstDir, "test.zip"))
s.NotNil(err)
s.Nil(Remove(dstDir))
}

0 comments on commit 58a13f0

Please sign in to comment.