Skip to content

Commit

Permalink
add examples & readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
CC11001100 committed Jan 7, 2023
1 parent bf9f8e1 commit 4055ad1
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 0 deletions.
125 changes: 125 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Project Root Directory

# 一、为什么会有这个东西?

在Golang的单元测试`foo_test.go`中读取文件和在`main.go`中读取文件的初始路径根本就不一样,如果你使用的是相对路径读取配置文件之类的,就可能会被恶心到,此项目就是解决类似问题的,提供一个固定的项目根目录的绝对路径,你可以从这里出发去读取你的文件,为从不同地方运行访问文件都提供一致性的行为。

# 二、安装

```bash
go get -u https://github.com/golang-infrastructure/go-project-root-directory
```

# 三、示例

## 3.1 获取项目的根目录

在main.go中:

```go
package main

import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)

func main() {

directory, err := project_root_directory.GetRootDirectory()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(directory)
// Output:
// D:\workspace\go-project-root-directory

}
```

在单元测试中,获取到的路径都是相同的:

```go
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)
// Output:
// D:\workspace\go-project-root-directory
}
```

## 3.2 获取项目根路径下的文件的路径

```go
package main

import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)

func main() {

path, err := project_root_directory.GetRootFilePath("go.mod")
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(path)
// Output:
// D:\workspace\go-project-root-directory\go.mod

}
```

## 3.3 读取项目根路径下的文件的内容

```go
package main

import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)

func main() {

file, err := project_root_directory.ReadRootFile("go.mod")
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(file))
// Output:
// 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
// )

}
```





19 changes: 19 additions & 0 deletions examples/get_root_directory/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)

func main() {

directory, err := project_root_directory.GetRootDirectory()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(directory)
// Output:
// D:\workspace\go-project-root-directory

}
19 changes: 19 additions & 0 deletions examples/get_root_file_path/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)

func main() {

path, err := project_root_directory.GetRootFilePath("go.mod")
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(path)
// Output:
// D:\workspace\go-project-root-directory\go.mod

}
32 changes: 32 additions & 0 deletions examples/read_root_file/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)

func main() {

file, err := project_root_directory.ReadRootFile("go.mod")
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(file))
// Output:
// 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
// )

}
2 changes: 2 additions & 0 deletions main_test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ func main() {
return
}
fmt.Println("项目根路径: " + directory)
// Output:
// 项目根路径: D:\workspace\go-project-root-directory

}
19 changes: 19 additions & 0 deletions project_root_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func GetRootDirectory() (string, error) {
}
}

// GetSourceCodeRootDirectory 如果是从源代码运行的,则按此方式寻找项目的根目录
func GetSourceCodeRootDirectory() (string, error) {
searchDirectory, err := os.Getwd()
if err != nil {
Expand All @@ -49,3 +50,21 @@ 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)
}
8 changes: 8 additions & 0 deletions project_root_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ func TestGetRootDirectory(t *testing.T) {
assert.Nil(t, err)
t.Log(directory)
}

func TestGetRootFilePath(t *testing.T) {
path, err := GetRootFilePath("go.mod")
assert.Nil(t, err)
t.Log(path)
// Output:
// D:\workspace\go-project-root-directory\go.mod
}
2 changes: 2 additions & 0 deletions test/foo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ func Test_foo(t *testing.T) {
directory, err := project_root_directory.GetRootDirectory()
assert.Nil(t, err)
t.Log(directory)
// Output:
// D:\workspace\go-project-root-directory
}

0 comments on commit 4055ad1

Please sign in to comment.