Compare commits

..
0 Commits
5 changed files with 48 additions and 109 deletions

No files matched your search

+1 -3
View File
@@ -23,9 +23,7 @@ type cli struct {
func (c *cli) ParseCLIParameter(parameterNameList []string) map[string]string {
cliParameterTable := make(map[string]*string)
for _, parameterName := range parameterNameList {
var val string
flag.StringVar(&val, parameterName, "", parameterName)
cliParameterTable[parameterName] = &val
cliParameterTable[parameterName] = flag.String(parameterName, "", parameterName)
}
// 这里有一个非常中的操作,转换,必须调用该方法
flag.Parse()
+1 -12
View File
@@ -4,17 +4,6 @@ go 1.21
require (
github.com/mitchellh/go-homedir v1.1.0
github.com/mozillazg/go-pinyin v0.21.0
github.com/mozillazg/go-pinyin v0.20.0
github.com/spaolacci/murmur3 v1.1.0
)
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b // indirect
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d // indirect
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/go-ini/ini v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
-19
View File
@@ -1,25 +1,6 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b h1:C7KftnLh7dOqzNRs5dn/9yqMDvuqMn5RCglvV6bY758=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71 h1:nvVSH+Ju8EmoPiPHTae5lxHo4kDjROYChs19Yayz+NY=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d h1:TV0BCQQewBEtLsv3i9gXkxLFd5A5bWBTiNd3D/I5o4Q=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d/go.mod h1:w7kG4zyTJ1uPFaTWhze+OQuaUBINT2XnDxpyiM6ctc0=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000 h1:ulssHpUMNoxws/fdJD+bhw8tuMPXp+jB54WwLdJNTsU=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000/go.mod h1:rt9wlUf/Y6lpZBXb0KK8n0JxEbI1J5JB73XgQmGHJqw=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/mozillazg/go-pinyin v0.21.0 h1:Wo8/NT45z7P3er/9YSLHA3/kjZzbLz5hR7i+jGeIGao=
github.com/mozillazg/go-pinyin v0.21.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+45 -74
View File
@@ -20,6 +20,10 @@ type project struct {
}
// GetExecutablePath 获取可执行文件的路径
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:41 2022/10/14
func (p *project) GetExecutablePath() (string, error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) //返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
if err != nil {
@@ -29,103 +33,70 @@ func (p *project) GetExecutablePath() (string, error) {
}
// GetFileSystemSeparator 获取当前文件系统的路径分隔符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:51 2022/10/14
func (p *project) GetFileSystemSeparator() string {
return string(filepath.Separator)
}
// BuildPath 构建路径
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:57 2022/10/14
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])
}
if filePathList[0] == "." || len(filePathList[0]) == 0 {
filePathList[0] = projectRootPath
} else {
// unix/ linux
if !strings.HasPrefix(filePathList[0], p.GetFileSystemSeparator()) {
// 不是绝对路径
filePathList[0] = filepath.Join(projectRootPath, filePathList[0])
if strings.ToLower(runtime.GOOS) == "windows" {
// windows
if strings.HasPrefix(filePathList[0], "."+p.GetFileSystemSeparator()) {
// 相对路径
filePathList[0] = strings.ReplaceAll(filePathList[0], ".", projectRootPath)
} else if !strings.Contains(filePathList[0], ":") {
// 不包含 :, 是 相对路径,拼成绝对路径
filePathList[0] = projectRootPath + p.GetFileSystemSeparator() + strings.TrimLeft(filePathList[0], p.GetFileSystemSeparator())
}
} else {
// unix/ linux
if strings.HasPrefix(filePathList[0], "."+p.GetFileSystemSeparator()) {
filePathList[0] = strings.ReplaceAll(filePathList[0], ".", projectRootPath)
} else if !strings.HasPrefix(filePathList[0], p.GetFileSystemSeparator()) {
filePathList[0] = projectRootPath + p.GetFileSystemSeparator() + filePathList[0]
}
}
}
return filepath.Join(filePathList...)
filePathList[0] = strings.TrimRight(filePathList[0], p.GetFileSystemSeparator())
for idx := 1; idx < len(filePathList); idx++ {
filePathList[idx] = strings.Trim(filePathList[idx], p.GetFileSystemSeparator())
}
return strings.Join(filePathList, p.GetFileSystemSeparator())
}
// GetWorkDir 获取工作目录
func (p *project) GetWorkDir() string {
// GetProjectPath 获取项目目录
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:39 2023/1/13
func (p *project) GetProjectPath() string {
dir, _ := os.Getwd()
return dir
}
// GetHomeDir 获取家目录
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:31 2023/4/18
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
}
+1 -1
View File
@@ -23,5 +23,5 @@ func Test_project_BuildPath(t *testing.T) {
}
func Test_project_GetGetProjectPath(t *testing.T) {
fmt.Println(Project.GetWorkDir())
fmt.Println(Project.GetProjectPath())
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.