132 lines
3.4 KiB
Go
132 lines
3.4 KiB
Go
// Package util ...
|
|
//
|
|
// Description : util ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2022-10-14 12:39
|
|
package util
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
)
|
|
|
|
type project struct {
|
|
}
|
|
|
|
// GetExecutablePath 获取可执行文件的路径
|
|
func (p *project) GetExecutablePath() (string, error) {
|
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) //返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return dir, nil
|
|
}
|
|
|
|
// GetFileSystemSeparator 获取当前文件系统的路径分隔符
|
|
func (p *project) GetFileSystemSeparator() string {
|
|
return string(filepath.Separator)
|
|
}
|
|
|
|
// BuildPath 构建路径
|
|
func (p *project) BuildPath(filePathList ...string) string {
|
|
projectRootPath, _ := p.GetExecutablePath()
|
|
if p.IsRunningWithGoRun() || p.IsInIDE() {
|
|
// go run 运行 或 者 ide 中运行, 则工作目录为项目目录
|
|
projectRootPath = p.GetWorkDir()
|
|
}
|
|
if len(filePathList) == 0 {
|
|
// 降为获取项目根目录
|
|
return projectRootPath
|
|
}
|
|
// 第一个特殊处理
|
|
if strings.ToLower(runtime.GOOS) == "windows" {
|
|
// windows
|
|
if !strings.Contains(filePathList[0], ":") {
|
|
// 不包含 :, 是 相对路径,拼成绝对路径
|
|
filePathList[0] = filepath.Join(projectRootPath, filePathList[0])
|
|
}
|
|
} else {
|
|
// unix/ linux
|
|
if !strings.HasPrefix(filePathList[0], p.GetFileSystemSeparator()) {
|
|
// 不是绝对路径
|
|
filePathList[0] = filepath.Join(projectRootPath, filePathList[0])
|
|
}
|
|
}
|
|
return filepath.Join(filePathList...)
|
|
}
|
|
|
|
// GetWorkDir 获取工作目录
|
|
func (p *project) GetWorkDir() string {
|
|
dir, _ := os.Getwd()
|
|
return dir
|
|
}
|
|
|
|
// GetHomeDir 获取家目录
|
|
func (p *project) GetHomeDir() string {
|
|
homePath, _ := homedir.Dir()
|
|
return homePath
|
|
}
|
|
|
|
// IsRunningWithGoRun 获取程序是否通过 go run 运行
|
|
func (p *project) IsRunningWithGoRun() bool {
|
|
// 方法1: 检查执行文件路径是否包含临时目录特征
|
|
exePath, err := os.Executable()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
// go run 会在临时目录创建二进制文件
|
|
// 不同系统的临时目录特征:
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
// Windows: 通常在 C:\Users\<user>\AppData\Local\Temp\go-build\
|
|
return strings.Contains(exePath, "go-build") ||
|
|
strings.Contains(exePath, `\go-build\`) ||
|
|
strings.Contains(filepath.Base(exePath), "go-build")
|
|
case "darwin", "linux":
|
|
// macOS/Linux: 通常在 /tmp/go-build/
|
|
return strings.Contains(exePath, "/go-build/") ||
|
|
strings.Contains(exePath, "/go-build") ||
|
|
(strings.HasPrefix(exePath, "/var/folders/") && strings.Contains(exePath, "/go-build/"))
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// IsInIDE 检查当前程序是否在IDE中运行
|
|
func (p *project) IsInIDE() bool {
|
|
// 检查常见的 IDE环境变量
|
|
ideEnvVars := []string{
|
|
"PYCHARM_HOSTED", // PyCharm
|
|
"IDEA_INITIAL_DIRECTORY", // IntelliJ IDEA
|
|
"VSCODE_PID", // VS Code
|
|
"GOLAND", // GoLand
|
|
"PHPSTORM", // PhpStorm
|
|
}
|
|
|
|
for _, env := range ideEnvVars {
|
|
if os.Getenv(env) != "" {
|
|
return true
|
|
}
|
|
}
|
|
|
|
// 检查父进程
|
|
parentProcess := os.Getenv("_")
|
|
if parentProcess != "" {
|
|
ideProcesses := []string{"goland", "idea", "pycharm", "vscode", "code"}
|
|
for _, ide := range ideProcesses {
|
|
if strings.Contains(strings.ToLower(parentProcess), ide) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|