-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_root_directory.go
70 lines (64 loc) · 1.99 KB
/
project_root_directory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package project_root_directory
import (
how_run "github.com/golang-infrastructure/go-how-run"
"os"
"path/filepath"
)
// GetRootDirectory 获取当前项目的根路径在哪里
func GetRootDirectory() (string, error) {
runType, err := how_run.GetRunType()
if err != nil {
return "", err
}
switch runType {
// RunTypeSourceUnknown 咱也不知道咋运行的
case how_run.RunTypeUnknown:
return "", ErrProjectRootDirectoryUnknown
// RunTypeSourceCode 是从源代码中运行的
case how_run.RunTypeSourceCode:
return GetSourceCodeRootDirectory()
// RunTypeReleaseBinary 发布的二进制文件运行
case how_run.RunTypeReleaseBinary:
return GetExecutableRootDirectory()
default:
return "", ErrProjectRootDirectoryUnknown
}
}
// GetSourceCodeRootDirectory 如果是从源代码运行的,则按此方式寻找项目的根目录
func GetSourceCodeRootDirectory() (string, error) {
searchDirectory, err := os.Getwd()
if err != nil {
return "", err
}
// 从当前路径往上找,第一个拥有go.mod文件的目录认为是项目的根路径
for searchDirectory != "" {
goModPath := filepath.Join(searchDirectory, "go.mod")
stat, err := os.Stat(goModPath)
if err == nil && stat != nil && !stat.IsDir() {
return searchDirectory, nil
}
searchDirectory = filepath.Dir(searchDirectory)
}
return "", ErrProjectRootDirectoryUnknown
}
// GetExecutableRootDirectory 获取可执行文件的root路径
func GetExecutableRootDirectory() (string, error) {
// 对于可执行文件而言,其所在的路径就是项目的根目录
return os.Getwd()
}
// GetRootFilePath 返回根路径下的文件路径
func GetRootFilePath(filename string) (string, error) {
directory, err := GetRootDirectory()
if err != nil {
return "", err
}
return filepath.Join(directory, filename), nil
}
// ReadRootFile 读取项目根目录下的文件
func ReadRootFile(filename string) ([]byte, error) {
path, err := GetRootFilePath(filename)
if err != nil {
return nil, err
}
return os.ReadFile(path)
}