-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathutils.go
65 lines (57 loc) · 1.33 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
func getEnvPath() (string, bool) {
// 返回值 string代表路径,bool代表是否存在
p, _ := os.Getwd()
path := p + "/.env.yaml" // 拼接路径
// 如果存在
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return path, false
}
}
return path, true
}
func getTempDir() string {
return os.Getenv("GOPATH") + "/src/github.com/silsuer/bingo/templates"
}
func IfFileExist(dir string) bool {
_, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
return false
} else {
log.Fatal(err)
}
}
return true
}
// 获取替换时的要替换的包名
func getReplacePackage() string {
path, _ := os.Getwd()
goSrcPath := os.Getenv("GOPATH") + "/src/"
if !strings.Contains(path, goSrcPath) { // 包含这个路径
fmt.Printf("You must use dys command in the $GOPATH directory.\n")
fmt.Printf("\tNow the current path: " + path + "\n")
fmt.Println("\tNow the $GOPATH: " + goSrcPath + "\n")
return ""
}
p := path[len(goSrcPath):]
return p
}
// 创建文件,递归创建文件目录,并且返回文件句柄
func CreateFile(path string, permision os.FileMode) (*os.File, error) {
dirPath := filepath.Dir(path)
err := os.MkdirAll(dirPath, permision)
if err != nil {
return nil, err
}
return os.Create(path)
}