-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a5c7f2
commit bf9f8e1
Showing
8 changed files
with
110 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package project_root_directory | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrProjectRootDirectoryUnknown = errors.New("project root directory is unknown") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file was deleted.
Oops, something went wrong.