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

39 lines
703 B
Go

package utils
import (
"crypto/rand"
"encoding/hex"
"os"
"os/exec"
"time"
)
// 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)
}
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")
}
// 定义短语
const (
NoAvailableNetworkInterfaces = "No available network interfaces"
)