From bf9f8e1c635a19b84f245fef6a23b4e2d82b8cb4 Mon Sep 17 00:00:00 2001 From: CC11001100 Date: Sat, 7 Jan 2023 14:13:05 +0800 Subject: [PATCH] init --- errors.go | 7 ++++ go.mod | 11 ++++++ go.sum | 21 +++++++++++ main_test/main.go | 9 ++++- project_root_directory.go | 69 ++++++++++++++++++---------------- project_root_directory_test.go | 16 +++++++- test/foo_test.go | 13 +++++++ test/test.go | 4 -- 8 files changed, 110 insertions(+), 40 deletions(-) create mode 100644 errors.go create mode 100644 go.sum create mode 100644 test/foo_test.go delete mode 100644 test/test.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..5d84510 --- /dev/null +++ b/errors.go @@ -0,0 +1,7 @@ +package project_root_directory + +import "errors" + +var ( + ErrProjectRootDirectoryUnknown = errors.New("project root directory is unknown") +) diff --git a/go.mod b/go.mod index 9d5bd8a..27de07e 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,14 @@ module github.com/golang-infrastructure/go-project-root-directory go 1.19 + +require ( + github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748 + github.com/stretchr/testify v1.8.1 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..72fcf4b --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-infrastructure/go-how-run v0.0.0-20221230033321-b14831b0da8b h1:AkNbq9PRXQ2/iaj1Av7NcPnBdSW+UWMxyxYNrUQ/qzc= +github.com/golang-infrastructure/go-how-run v0.0.0-20221230033321-b14831b0da8b/go.mod h1:dNDJD2Dj25J5lLfRzQQRYu/AOhyJw5sfuigs1M+4hDk= +github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748 h1:FcFtw13Akovl0YKh39MJaqlnwewuH4Mx8JFuT0bgsPA= +github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748/go.mod h1:dNDJD2Dj25J5lLfRzQQRYu/AOhyJw5sfuigs1M+4hDk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main_test/main.go b/main_test/main.go index 3d29996..4ad6436 100644 --- a/main_test/main.go +++ b/main_test/main.go @@ -2,11 +2,16 @@ package main import ( "fmt" - "os" + project_root_directory "github.com/golang-infrastructure/go-project-root-directory" ) func main() { - fmt.Println(os.Executable()) + directory, err := project_root_directory.GetRootDirectory() + if err != nil { + fmt.Println(err.Error()) + return + } + fmt.Println("项目根路径: " + directory) } diff --git a/project_root_directory.go b/project_root_directory.go index 9807b3e..6f69e8d 100644 --- a/project_root_directory.go +++ b/project_root_directory.go @@ -1,46 +1,51 @@ package project_root_directory import ( + how_run "github.com/golang-infrastructure/go-how-run" "os" "path/filepath" ) -func Get() string { - return "" -} - -type RunType int - -const ( - RunTypeUintTest RunType = iota - RunTypeRelease -) - -func judgeCurrentRunType() { - +// 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 + } } -// 获取可执行文件的路径 -func getExecutable() string { - ex, err := os.Executable() +func GetSourceCodeRootDirectory() (string, error) { + searchDirectory, err := os.Getwd() if err != nil { - return "" + return "", err } - exPath := filepath.Dir(ex) - realPath, err := filepath.EvalSymlinks(exPath) - if err != nil { - return "" + // 从当前路径往上找,第一个拥有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 realPath - //return filepath.Dir(realPath) + return "", ErrProjectRootDirectoryUnknown } -// 源代码测试 - -// 源代码Example - -// 源代码基准测试 - -// 源代码main方法运行 - -// 编译后的执行 +// GetExecutableRootDirectory 获取可执行文件的root路径 +func GetExecutableRootDirectory() (string, error) { + // 对于可执行文件而言,其所在的路径就是项目的根目录 + return os.Getwd() +} diff --git a/project_root_directory_test.go b/project_root_directory_test.go index 56ca292..839cd5f 100644 --- a/project_root_directory_test.go +++ b/project_root_directory_test.go @@ -1,9 +1,21 @@ package project_root_directory -import "testing" +import ( + "github.com/stretchr/testify/assert" + "os" + "testing" +) func Test_getExecutable(t *testing.T) { - t.Log(getExecutable()) + //t.Log(getExecutable()) + t.Log(os.Getwd()) + +} + +func TestGetRootDirectory(t *testing.T) { + directory, err := GetRootDirectory() + assert.Nil(t, err) + t.Log(directory) } diff --git a/test/foo_test.go b/test/foo_test.go new file mode 100644 index 0000000..6d19e08 --- /dev/null +++ b/test/foo_test.go @@ -0,0 +1,13 @@ +package test + +import ( + project_root_directory "github.com/golang-infrastructure/go-project-root-directory" + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_foo(t *testing.T) { + directory, err := project_root_directory.GetRootDirectory() + assert.Nil(t, err) + t.Log(directory) +} diff --git a/test/test.go b/test/test.go deleted file mode 100644 index 0a86e4e..0000000 --- a/test/test.go +++ /dev/null @@ -1,4 +0,0 @@ -package test - - -