Files
sunhpc-go/pkg/utils/utils.go

34 lines
610 B
Go
Raw Normal View History

2026-02-14 05:36:00 +08:00
package utils
import (
2026-02-20 18:44:43 +08:00
"crypto/rand"
"encoding/hex"
2026-02-14 05:36:00 +08:00
"os"
"os/exec"
2026-02-20 18:44:43 +08:00
"time"
2026-02-14 05:36:00 +08:00
)
// CommandExists 检查命令是否存在
func CommandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
// FileExists 检查文件是否存在
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil || !os.IsNotExist(err)
}
2026-02-20 18:44:43 +08:00
func GenerateID() (string, error) {
bytes := make([]byte, 16)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func GetTimestamp() string {
return time.Now().Format("2006-01-02 15:04:05")
}