mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 01:57:19 +08:00
fix: lint
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/process"
|
||||
"github.com/spf13/cast"
|
||||
|
||||
"github.com/acepanel/panel/pkg/shell"
|
||||
)
|
||||
@@ -37,9 +38,7 @@ func GetServiceInfo(name string) (*ServiceInfo, error) {
|
||||
case "ActiveState":
|
||||
info.Status = value
|
||||
case "MainPID":
|
||||
if pid, err := strconv.Atoi(value); err == nil {
|
||||
info.PID = pid
|
||||
}
|
||||
info.PID = cast.ToInt(value)
|
||||
case "ExecMainStartTimestamp":
|
||||
// 格式: Mon 2024-01-01 12:00:00 UTC
|
||||
if value != "" && value != "n/a" {
|
||||
@@ -48,7 +47,6 @@ func GetServiceInfo(name string) (*ServiceInfo, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// 如果有 PID,使用 gopsutil 获取进程信息
|
||||
if info.PID > 0 {
|
||||
if proc, err := process.NewProcess(int32(info.PID)); err == nil {
|
||||
// 获取内存信息
|
||||
@@ -65,41 +63,6 @@ func GetServiceInfo(name string) (*ServiceInfo, error) {
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// calcUptime 计算运行时间
|
||||
func calcUptime(startTime string) string {
|
||||
// 解析时间格式: Mon 2024-01-01 12:00:00 UTC
|
||||
// 或者: Mon 2024-01-01 12:00:00 CST
|
||||
layouts := []string{
|
||||
"Mon 2006-01-02 15:04:05 MST",
|
||||
"Mon 2006-01-02 15:04:05 -0700",
|
||||
}
|
||||
|
||||
var t time.Time
|
||||
var err error
|
||||
for _, layout := range layouts {
|
||||
t, err = time.Parse(layout, startTime)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
duration := time.Since(t)
|
||||
days := int(duration.Hours() / 24)
|
||||
hours := int(duration.Hours()) % 24
|
||||
minutes := int(duration.Minutes()) % 60
|
||||
|
||||
if days > 0 {
|
||||
return strconv.Itoa(days) + "d " + strconv.Itoa(hours) + "h " + strconv.Itoa(minutes) + "m"
|
||||
}
|
||||
if hours > 0 {
|
||||
return strconv.Itoa(hours) + "h " + strconv.Itoa(minutes) + "m"
|
||||
}
|
||||
return strconv.Itoa(minutes) + "m"
|
||||
}
|
||||
|
||||
// Status 获取服务状态
|
||||
func Status(name string) (bool, error) {
|
||||
output, _ := shell.Execf("systemctl is-active '%s'", name) // 不判断错误,因为 is-active 在服务未启用时会返回 3
|
||||
@@ -184,3 +147,38 @@ func DaemonReload() error {
|
||||
_, err := shell.ExecfWithTimeout(2*time.Minute, "systemctl daemon-reload")
|
||||
return err
|
||||
}
|
||||
|
||||
// calcUptime 计算运行时间
|
||||
func calcUptime(startTime string) string {
|
||||
// 解析时间格式: Mon 2024-01-01 12:00:00 UTC
|
||||
// 或者: Mon 2024-01-01 12:00:00 CST
|
||||
layouts := []string{
|
||||
"Mon 2006-01-02 15:04:05 MST",
|
||||
"Mon 2006-01-02 15:04:05 -0700",
|
||||
}
|
||||
|
||||
var t time.Time
|
||||
var err error
|
||||
for _, layout := range layouts {
|
||||
t, err = time.Parse(layout, startTime)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
duration := time.Since(t)
|
||||
days := int(duration.Hours() / 24)
|
||||
hours := int(duration.Hours()) % 24
|
||||
minutes := int(duration.Minutes()) % 60
|
||||
|
||||
if days > 0 {
|
||||
return strconv.Itoa(days) + "d " + strconv.Itoa(hours) + "h " + strconv.Itoa(minutes) + "m"
|
||||
}
|
||||
if hours > 0 {
|
||||
return strconv.Itoa(hours) + "h " + strconv.Itoa(minutes) + "m"
|
||||
}
|
||||
return strconv.Itoa(minutes) + "m"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user