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"
)

// 定义目录
const (
	DefaultBaseDir string = "/etc/sunhpc"
	DefaultTmplDir string = DefaultBaseDir + "/tmpl.d"
	DefaultLogDir  string = "/var/log/sunhpc"
	DefaultLogFile string = DefaultLogDir + "/sunhpc.log"
)

// 定义数据库
const (
	DefaultDBName string = "sunhpc.db"
	DefaultDBPath string = "/var/lib/sunhpc"
	DefaultDBArgs string = "_foreign_keys=on&_journal_mode=WAL&_timeout=5000"
)
