-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
140 lines (113 loc) · 2.78 KB
/
path.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Copyright 2015 Ron Zhang <ronzxy@mx.aketi.cn>. All rights reserved.
*
* Licensed under the Apache License, version 2.0 (the "License").
* You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package helper
import (
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
var Path = NewPathHelper()
type PathHelper struct{}
func NewPathHelper() *PathHelper {
return &PathHelper{}
}
func (this *PathHelper) ExecPath() string {
file, err := exec.LookPath(os.Args[0])
if err != nil {
return ""
}
p, err := filepath.Abs(file)
if err != nil {
return ""
}
return p
}
// WorkDir returns absolute path of work directory.
func (this *PathHelper) WorkDir() string {
execPath := this.ExecPath()
return path.Dir(strings.Replace(execPath, "\\", "/", -1))
}
func (this *PathHelper) WorkName() string {
execPath := this.ExecPath()
return path.Base(strings.Replace(execPath, "\\", "/", -1))
}
// 判断文件夹是否存在
func (this *PathHelper) IsExist(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func (this *PathHelper) Create(dir string, perm os.FileMode) error {
isExist, err := this.IsExist(dir)
if err != nil {
return err
}
if !isExist {
// 创建文件夹
return os.Mkdir(dir, perm)
}
return nil
}
func (this *PathHelper) Abs(filePath string) (string, error) {
return filepath.Abs(filePath)
}
func (this *PathHelper) Dir(filePath string) (string, error) {
p, err := this.Abs(filePath)
if err == nil {
p = path.Dir(p)
}
return p, err
}
func (this *PathHelper) FileName(filePath string) (string, error) {
filePath, err := this.Abs(filePath)
if err != nil {
return "", err
}
basePath, err := this.Dir(filePath)
if err == nil {
filePath = strings.Replace(filePath, "\\", "/", -1)
basePath = strings.Replace(basePath, "\\", "/", -1)
basePath = filePath[len(basePath)+1:]
}
return basePath, err
}
func (this *PathHelper) Split(filePath string) []string {
filePath = strings.Replace(filePath, "\\", "/", -1)
return strings.Split(filePath, "/")
}
func (this *PathHelper) CreateDir(filePath string, perm os.FileMode) error {
var (
dirs []string
err error
)
dirs = this.Split(filePath)
filePath = ""
for _, v := range dirs {
if v == "" {
filePath = "/"
} else {
filePath = path.Join(filePath, v)
err = this.Create(filePath, perm)
if err != nil {
return err
}
}
}
return nil
}