Skip to content

Commit 4055ad1

Browse files
committedJan 7, 2023
add examples & readme.md
1 parent bf9f8e1 commit 4055ad1

File tree

8 files changed

+226
-0
lines changed

8 files changed

+226
-0
lines changed
 

‎README.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Project Root Directory
2+
3+
# 一、为什么会有这个东西?
4+
5+
在Golang的单元测试`foo_test.go`中读取文件和在`main.go`中读取文件的初始路径根本就不一样,如果你使用的是相对路径读取配置文件之类的,就可能会被恶心到,此项目就是解决类似问题的,提供一个固定的项目根目录的绝对路径,你可以从这里出发去读取你的文件,为从不同地方运行访问文件都提供一致性的行为。
6+
7+
# 二、安装
8+
9+
```bash
10+
go get -u https://github.com/golang-infrastructure/go-project-root-directory
11+
```
12+
13+
# 三、示例
14+
15+
## 3.1 获取项目的根目录
16+
17+
在main.go中:
18+
19+
```go
20+
package main
21+
22+
import (
23+
"fmt"
24+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
25+
)
26+
27+
func main() {
28+
29+
directory, err := project_root_directory.GetRootDirectory()
30+
if err != nil {
31+
fmt.Println(err.Error())
32+
return
33+
}
34+
fmt.Println(directory)
35+
// Output:
36+
// D:\workspace\go-project-root-directory
37+
38+
}
39+
```
40+
41+
在单元测试中,获取到的路径都是相同的:
42+
43+
```go
44+
package test
45+
46+
import (
47+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
48+
"github.com/stretchr/testify/assert"
49+
"testing"
50+
)
51+
52+
func Test_foo(t *testing.T) {
53+
directory, err := project_root_directory.GetRootDirectory()
54+
assert.Nil(t, err)
55+
t.Log(directory)
56+
// Output:
57+
// D:\workspace\go-project-root-directory
58+
}
59+
```
60+
61+
## 3.2 获取项目根路径下的文件的路径
62+
63+
```go
64+
package main
65+
66+
import (
67+
"fmt"
68+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
69+
)
70+
71+
func main() {
72+
73+
path, err := project_root_directory.GetRootFilePath("go.mod")
74+
if err != nil {
75+
fmt.Println(err.Error())
76+
return
77+
}
78+
fmt.Println(path)
79+
// Output:
80+
// D:\workspace\go-project-root-directory\go.mod
81+
82+
}
83+
```
84+
85+
## 3.3 读取项目根路径下的文件的内容
86+
87+
```go
88+
package main
89+
90+
import (
91+
"fmt"
92+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
93+
)
94+
95+
func main() {
96+
97+
file, err := project_root_directory.ReadRootFile("go.mod")
98+
if err != nil {
99+
fmt.Println(err.Error())
100+
return
101+
}
102+
fmt.Println(string(file))
103+
// Output:
104+
// module github.com/golang-infrastructure/go-project-root-directory
105+
//
106+
// go 1.19
107+
//
108+
// require (
109+
// github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748
110+
// github.com/stretchr/testify v1.8.1
111+
// )
112+
//
113+
// require (
114+
// github.com/davecgh/go-spew v1.1.1 // indirect
115+
// github.com/pmezard/go-difflib v1.0.0 // indirect
116+
// gopkg.in/yaml.v3 v3.0.1 // indirect
117+
// )
118+
119+
}
120+
```
121+
122+
123+
124+
125+

‎examples/get_root_directory/main.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
6+
)
7+
8+
func main() {
9+
10+
directory, err := project_root_directory.GetRootDirectory()
11+
if err != nil {
12+
fmt.Println(err.Error())
13+
return
14+
}
15+
fmt.Println(directory)
16+
// Output:
17+
// D:\workspace\go-project-root-directory
18+
19+
}

‎examples/get_root_file_path/main.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
6+
)
7+
8+
func main() {
9+
10+
path, err := project_root_directory.GetRootFilePath("go.mod")
11+
if err != nil {
12+
fmt.Println(err.Error())
13+
return
14+
}
15+
fmt.Println(path)
16+
// Output:
17+
// D:\workspace\go-project-root-directory\go.mod
18+
19+
}

‎examples/read_root_file/main.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
6+
)
7+
8+
func main() {
9+
10+
file, err := project_root_directory.ReadRootFile("go.mod")
11+
if err != nil {
12+
fmt.Println(err.Error())
13+
return
14+
}
15+
fmt.Println(string(file))
16+
// Output:
17+
// module github.com/golang-infrastructure/go-project-root-directory
18+
//
19+
// go 1.19
20+
//
21+
// require (
22+
// github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748
23+
// github.com/stretchr/testify v1.8.1
24+
// )
25+
//
26+
// require (
27+
// github.com/davecgh/go-spew v1.1.1 // indirect
28+
// github.com/pmezard/go-difflib v1.0.0 // indirect
29+
// gopkg.in/yaml.v3 v3.0.1 // indirect
30+
// )
31+
32+
}

‎main_test/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ func main() {
1313
return
1414
}
1515
fmt.Println("项目根路径: " + directory)
16+
// Output:
17+
// 项目根路径: D:\workspace\go-project-root-directory
1618

1719
}

‎project_root_directory.go

+19
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func GetRootDirectory() (string, error) {
2727
}
2828
}
2929

30+
// GetSourceCodeRootDirectory 如果是从源代码运行的,则按此方式寻找项目的根目录
3031
func GetSourceCodeRootDirectory() (string, error) {
3132
searchDirectory, err := os.Getwd()
3233
if err != nil {
@@ -49,3 +50,21 @@ func GetExecutableRootDirectory() (string, error) {
4950
// 对于可执行文件而言,其所在的路径就是项目的根目录
5051
return os.Getwd()
5152
}
53+
54+
// GetRootFilePath 返回根路径下的文件路径
55+
func GetRootFilePath(filename string) (string, error) {
56+
directory, err := GetRootDirectory()
57+
if err != nil {
58+
return "", err
59+
}
60+
return filepath.Join(directory, filename), nil
61+
}
62+
63+
// ReadRootFile 读取项目根目录下的文件
64+
func ReadRootFile(filename string) ([]byte, error) {
65+
path, err := GetRootFilePath(filename)
66+
if err != nil {
67+
return nil, err
68+
}
69+
return os.ReadFile(path)
70+
}

‎project_root_directory_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ func TestGetRootDirectory(t *testing.T) {
1919
assert.Nil(t, err)
2020
t.Log(directory)
2121
}
22+
23+
func TestGetRootFilePath(t *testing.T) {
24+
path, err := GetRootFilePath("go.mod")
25+
assert.Nil(t, err)
26+
t.Log(path)
27+
// Output:
28+
// D:\workspace\go-project-root-directory\go.mod
29+
}

‎test/foo_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ func Test_foo(t *testing.T) {
1010
directory, err := project_root_directory.GetRootDirectory()
1111
assert.Nil(t, err)
1212
t.Log(directory)
13+
// Output:
14+
// D:\workspace\go-project-root-directory
1315
}

0 commit comments

Comments
 (0)
Please sign in to comment.