2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 14:57:16 +08:00

refactor: 重构 tools.Read 函数

This commit is contained in:
耗子
2023-11-14 02:08:26 +08:00
parent 2b1b58ea2b
commit 83cbae034c
21 changed files with 147 additions and 64 deletions

View File

@@ -68,5 +68,10 @@ func NewSSHClient(conf *SSHClientConfig) (*ssh.Client, error) {
}
func getKey(keyPath string) (ssh.Signer, error) {
return ssh.ParsePrivateKey([]byte(tools.Read(keyPath)))
key, err := tools.Read(keyPath)
if err != nil {
return nil, err
}
return ssh.ParsePrivateKey([]byte(key))
}

View File

@@ -25,14 +25,9 @@ func Write(path string, data string, permission os.FileMode) error {
}
// Read 读取文件
// TODO 重构带 error 返回
func Read(path string) string {
func Read(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return ""
}
return string(data)
return string(data), err
}
// Remove 删除文件/目录

View File

@@ -35,7 +35,9 @@ func (s *SystemHelperTestSuite) TestRead() {
err := os.WriteFile(filePath, []byte("test data"), 0644)
s.Nil(err)
s.Equal("test data", Read(filePath))
data, err := Read(filePath)
s.Nil(err)
s.Equal("test data", data)
}
func (s *SystemHelperTestSuite) TestRemove() {