From 343d45175221557fa8f54c0a290ce9689e5839c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Mon, 28 Oct 2024 03:35:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=89=80=E6=9C=89shell=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E5=85=A8=E9=83=A8=E4=BD=BF=E7=94=A8shell=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/io/path.go | 32 ++++++++++++++------------------ pkg/systemctl/service.go | 20 ++++++++++---------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/pkg/io/path.go b/pkg/io/path.go index 7c58d6aa..a30e2b04 100644 --- a/pkg/io/path.go +++ b/pkg/io/path.go @@ -1,10 +1,8 @@ package io import ( - "bytes" "fmt" "os" - "os/exec" "path/filepath" "strconv" "strings" @@ -24,14 +22,14 @@ func Mkdir(path string, permission os.FileMode) error { // Chmod 修改文件/目录权限 func Chmod(path string, permission os.FileMode) error { - cmd := exec.Command("chmod", "-R", fmt.Sprintf("%o", permission), path) - return cmd.Run() + _, err := shell.Execf("chmod -R '%o' '%s'", permission, path) + return err } // Chown 修改文件或目录所有者 func Chown(path, user, group string) error { - cmd := exec.Command("chown", "-R", user+":"+group, path) - return cmd.Run() + _, err := shell.Execf("chown -R '%s:%s' '%s'", user, group, path) + return err } // Exists 判断路径是否存在 @@ -97,12 +95,12 @@ func IsDir(path string) bool { // SizeX 获取路径大小(du命令) func SizeX(path string) (int64, error) { - out, err := exec.Command("du", "-sb", path).Output() + out, err := shell.Execf("du -sb '%s'", path) if err != nil { return 0, err } - parts := strings.Fields(string(out)) + parts := strings.Fields(out) if len(parts) == 0 { return 0, fmt.Errorf("无法解析 du 输出") } @@ -112,12 +110,12 @@ func SizeX(path string) (int64, error) { // CountX 统计目录下文件数 func CountX(path string) (int64, error) { - out, err := exec.Command("find", path, "-printf", ".").Output() + out, err := shell.Execf("find '%s' -printf '.'", path) if err != nil { return 0, err } - count := len(string(out)) + count := len(out) return int64(count), nil } @@ -146,20 +144,18 @@ func Search(path, keyword string, sub bool) (map[string]os.FileInfo, error) { func SearchX(path, keyword string, sub bool) (map[string]os.FileInfo, error) { paths := make(map[string]os.FileInfo) - var cmd *exec.Cmd + var out string + var err error if sub { - cmd = exec.Command("find", path, "-name", "*"+keyword+"*") + out, err = shell.Execf("find '%s' -name '*%s*'", path, keyword) } else { - cmd = exec.Command("find", path, "-maxdepth", "1", "-name", "*"+keyword+"*") + out, err = shell.Execf("find '%s' -maxdepth 1 -name '*%s*'", path, keyword) } - var out bytes.Buffer - cmd.Stdout = &out - - if err := cmd.Run(); err != nil { + if err != nil { return nil, err } - lines := strings.Split(out.String(), "\n") + lines := strings.Split(out, "\n") for _, line := range lines { line = strings.TrimSpace(line) if line == "" { diff --git a/pkg/systemctl/service.go b/pkg/systemctl/service.go index 1f854d53..557cf288 100644 --- a/pkg/systemctl/service.go +++ b/pkg/systemctl/service.go @@ -2,8 +2,7 @@ package systemctl import ( "errors" - "os/exec" - "strings" + "fmt" "github.com/TheTNB/panel/pkg/shell" ) @@ -16,23 +15,24 @@ func Status(name string) (bool, error) { // IsEnabled 服务是否启用 func IsEnabled(name string) (bool, error) { - cmd := exec.Command("systemctl", "is-enabled", name) - output, _ := cmd.CombinedOutput() - status := strings.TrimSpace(string(output)) + out, err := shell.Execf("systemctl is-enabled '%s'", name) + if err != nil { + return false, fmt.Errorf("failed to check service status: %w", err) + } - switch status { + switch out { case "enabled": return true, nil case "disabled": return false, nil case "masked": - return false, errors.New("服务已被屏蔽") + return false, errors.New("service is masked") case "static": - return false, errors.New("服务已被静态启用") + return false, errors.New("service is statically enabled") case "indirect": - return false, errors.New("服务已被间接启用") + return false, errors.New("service is indirectly enabled") default: - return false, errors.New("无法确定服务状态") + return false, errors.New("unknown service status") } }