Skip to content

Commit bf9f8e1

Browse files
committed
init
1 parent 6a5c7f2 commit bf9f8e1

File tree

8 files changed

+110
-40
lines changed

8 files changed

+110
-40
lines changed

errors.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package project_root_directory
2+
3+
import "errors"
4+
5+
var (
6+
ErrProjectRootDirectoryUnknown = errors.New("project root directory is unknown")
7+
)

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
module github.com/golang-infrastructure/go-project-root-directory
22

33
go 1.19
4+
5+
require (
6+
github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748
7+
github.com/stretchr/testify v1.8.1
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

go.sum

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/golang-infrastructure/go-how-run v0.0.0-20221230033321-b14831b0da8b h1:AkNbq9PRXQ2/iaj1Av7NcPnBdSW+UWMxyxYNrUQ/qzc=
5+
github.com/golang-infrastructure/go-how-run v0.0.0-20221230033321-b14831b0da8b/go.mod h1:dNDJD2Dj25J5lLfRzQQRYu/AOhyJw5sfuigs1M+4hDk=
6+
github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748 h1:FcFtw13Akovl0YKh39MJaqlnwewuH4Mx8JFuT0bgsPA=
7+
github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748/go.mod h1:dNDJD2Dj25J5lLfRzQQRYu/AOhyJw5sfuigs1M+4hDk=
8+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
9+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
12+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
13+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
15+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
16+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
17+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
18+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
19+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
21+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main_test/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ package main
22

33
import (
44
"fmt"
5-
"os"
5+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
66
)
77

88
func main() {
99

10-
fmt.Println(os.Executable())
10+
directory, err := project_root_directory.GetRootDirectory()
11+
if err != nil {
12+
fmt.Println(err.Error())
13+
return
14+
}
15+
fmt.Println("项目根路径: " + directory)
1116

1217
}

project_root_directory.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
11
package project_root_directory
22

33
import (
4+
how_run "github.com/golang-infrastructure/go-how-run"
45
"os"
56
"path/filepath"
67
)
78

8-
func Get() string {
9-
return ""
10-
}
11-
12-
type RunType int
13-
14-
const (
15-
RunTypeUintTest RunType = iota
16-
RunTypeRelease
17-
)
18-
19-
func judgeCurrentRunType() {
20-
9+
// GetRootDirectory 获取当前项目的根路径在哪里
10+
func GetRootDirectory() (string, error) {
11+
runType, err := how_run.GetRunType()
12+
if err != nil {
13+
return "", err
14+
}
15+
switch runType {
16+
// RunTypeSourceUnknown 咱也不知道咋运行的
17+
case how_run.RunTypeUnknown:
18+
return "", ErrProjectRootDirectoryUnknown
19+
// RunTypeSourceCode 是从源代码中运行的
20+
case how_run.RunTypeSourceCode:
21+
return GetSourceCodeRootDirectory()
22+
// RunTypeReleaseBinary 发布的二进制文件运行
23+
case how_run.RunTypeReleaseBinary:
24+
return GetExecutableRootDirectory()
25+
default:
26+
return "", ErrProjectRootDirectoryUnknown
27+
}
2128
}
2229

23-
// 获取可执行文件的路径
24-
func getExecutable() string {
25-
ex, err := os.Executable()
30+
func GetSourceCodeRootDirectory() (string, error) {
31+
searchDirectory, err := os.Getwd()
2632
if err != nil {
27-
return ""
33+
return "", err
2834
}
29-
exPath := filepath.Dir(ex)
30-
realPath, err := filepath.EvalSymlinks(exPath)
31-
if err != nil {
32-
return ""
35+
// 从当前路径往上找,第一个拥有go.mod文件的目录认为是项目的根路径
36+
for searchDirectory != "" {
37+
goModPath := filepath.Join(searchDirectory, "go.mod")
38+
stat, err := os.Stat(goModPath)
39+
if err == nil && stat != nil && !stat.IsDir() {
40+
return searchDirectory, nil
41+
}
42+
searchDirectory = filepath.Dir(searchDirectory)
3343
}
34-
return realPath
35-
//return filepath.Dir(realPath)
44+
return "", ErrProjectRootDirectoryUnknown
3645
}
3746

38-
// 源代码测试
39-
40-
// 源代码Example
41-
42-
// 源代码基准测试
43-
44-
// 源代码main方法运行
45-
46-
// 编译后的执行
47+
// GetExecutableRootDirectory 获取可执行文件的root路径
48+
func GetExecutableRootDirectory() (string, error) {
49+
// 对于可执行文件而言,其所在的路径就是项目的根目录
50+
return os.Getwd()
51+
}

project_root_directory_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
package project_root_directory
22

3-
import "testing"
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
7+
)
48

59
func Test_getExecutable(t *testing.T) {
610

7-
t.Log(getExecutable())
11+
//t.Log(getExecutable())
812

13+
t.Log(os.Getwd())
14+
15+
}
16+
17+
func TestGetRootDirectory(t *testing.T) {
18+
directory, err := GetRootDirectory()
19+
assert.Nil(t, err)
20+
t.Log(directory)
921
}

test/foo_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test
2+
3+
import (
4+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func Test_foo(t *testing.T) {
10+
directory, err := project_root_directory.GetRootDirectory()
11+
assert.Nil(t, err)
12+
t.Log(directory)
13+
}

test/test.go

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)