package utils

import (
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

func FileIsExist(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return false
}

// ExecCmd 执行脚本
func ExecCmd(command string) ([]byte, error) {
	var cmd *exec.Cmd
	if runtime.GOOS == "windows" {
		cmd = exec.Command("powershell", "-command", command)
	} else {
		cmd = exec.Command("sh", "-c", command)
	}
	result, err := cmd.CombinedOutput()
	if err != nil {
		log.Println("install deps fail", err)
		return nil, err
	}
	log.Println("install deps success")
	return result, nil
}

func GetCurDir() string {
	ex, err := os.Executable()
	if err != nil {
		panic(err)
	}
	// 可执行文件的路径
	log.Println(ex)
	//	获取执行文件所在目录
	exPath := filepath.Dir(ex)
	log.Println("可执行文件路径 :" + exPath)
	// 使用EvalSymlinks获取真是路径
	realPath, err := filepath.EvalSymlinks(exPath)
	if err != nil {
		panic(err)
	}
	log.Println("符号链接真实路径:" + realPath)
	return realPath
}
