Skip to content

Commit

Permalink
feat: 所有shell操作全部使用shell包
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 27, 2024
1 parent 401a01c commit bef638b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
32 changes: 14 additions & 18 deletions pkg/io/path.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io

import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
Expand All @@ -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 判断路径是否存在
Expand Down Expand Up @@ -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 输出")
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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 == "" {
Expand Down
20 changes: 10 additions & 10 deletions pkg/systemctl/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package systemctl

import (
"errors"
"os/exec"
"strings"
"fmt"

"github.com/TheTNB/panel/pkg/shell"
)
Expand All @@ -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")
}
}

Expand Down

0 comments on commit bef638b

Please sign in to comment.