From 58a13f063640dbe6fd04e841f4d66035b5fdf452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Mon, 20 Nov 2023 22:15:33 +0800 Subject: [PATCH] fix: lint and test --- app/services/backup.go | 10 ++++++-- pkg/tools/system.go | 52 ++++------------------------------------ pkg/tools/system_test.go | 34 +++++++++++++------------- 3 files changed, 30 insertions(+), 66 deletions(-) diff --git a/app/services/backup.go b/app/services/backup.go index 68d0012086..96a5b78421 100644 --- a/app/services/backup.go +++ b/app/services/backup.go @@ -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 = "" // 置空,防止干扰后续判断 @@ -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") } @@ -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 = "" // 置空,防止干扰后续判断 @@ -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 } diff --git a/pkg/tools/system.go b/pkg/tools/system.go index 543a89979a..e5a7242408 100644 --- a/pkg/tools/system.go +++ b/pkg/tools/system.go @@ -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 写入文件 @@ -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 判断路径是否存在 @@ -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 } @@ -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 } diff --git a/pkg/tools/system_test.go b/pkg/tools/system_test.go index c5a79e46ee..ffa23a48b1 100644 --- a/pkg/tools/system_test.go +++ b/pkg/tools/system_test.go @@ -19,18 +19,17 @@ 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) @@ -38,6 +37,7 @@ func (s *SystemHelperTestSuite) TestRead() { data, err := Read(filePath.Name()) s.Nil(err) s.Equal("test data", data) + s.Nil(Remove(filePath.Name())) } func (s *SystemHelperTestSuite) TestRemove() { @@ -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) @@ -113,6 +112,7 @@ func (s *SystemHelperTestSuite) TestChown() { } else { s.Nil(err) } + s.Nil(Remove(filePath.Name())) } func (s *SystemHelperTestSuite) TestExists() { @@ -125,7 +125,6 @@ func (s *SystemHelperTestSuite) TestExists() { func (s *SystemHelperTestSuite) TestEmpty() { filePath, _ := TempFile("testfile") - defer Remove(filePath.Name()) s.True(Empty(filePath.Name())) if IsWindows() { @@ -133,39 +132,39 @@ func (s *SystemHelperTestSuite) TestEmpty() { } 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) @@ -173,11 +172,11 @@ func (s *SystemHelperTestSuite) TestSize() { 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) @@ -185,13 +184,12 @@ func (s *SystemHelperTestSuite) TestFileInfo() { 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) @@ -203,22 +201,22 @@ 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) @@ -226,13 +224,15 @@ func (s *SystemHelperTestSuite) TestArchiveSuccessfullyArchivesFiles() { 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)) }