-
Notifications
You must be signed in to change notification settings - Fork 3
/
assert.go
165 lines (144 loc) · 4.27 KB
/
assert.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package fileKit
import (
"github.com/richelieu-yang/chimera/v2/src/core/errorKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"github.com/richelieu-yang/chimera/v2/src/funcKit"
)
// AssertExist
/*
@param path 文件(或目录)的路径
*/
func AssertExist(path string) error {
if strKit.IsBlank(path) {
return errorKit.NewSkip(1, "[%s] path(%s) is blank", funcKit.GetFuncName(1), path)
}
if !Exists(path) {
return errorKit.NewSkip(1, "[%s] path(%s) doesn't exist", funcKit.GetFuncName(1), path)
}
return nil
}
// AssertNotExistOrIsFile
/*
通过的情况: 不存在 || 存在但是个文件
不通过的情况: 存在但是个目录
@param mkdirArgs (1) true: path不存在的话,为其创建父目录(可多级)
(2) 默认: true
*/
func AssertNotExistOrIsFile(path string, mkdirArgs ...bool) error {
if strKit.IsBlank(path) {
return errorKit.NewSkip(1, "[%s] path(%s) is blank", funcKit.GetFuncName(1), path)
}
if Exists(path) && IsDir(path) {
return errorKit.NewSkip(1, "[%s] path(%s) exists but it is a directory", funcKit.GetFuncName(1), path)
}
var flag bool = true
if mkdirArgs != nil {
flag = mkdirArgs[0]
}
if flag {
if err := MkParentDirs(path); err != nil {
return err
}
}
return nil
}
// AssertNotExistOrIsDir
/*
通过的情况: 不存在 || 存在但是个目录
不通过的情况: 存在但是个文件
@param mkdirArgs (1) true: path不存在的话,为其创建目录(可多级)
(2) 默认: true
*/
func AssertNotExistOrIsDir(path string, mkdirArgs ...bool) error {
if strKit.IsBlank(path) {
return errorKit.NewSkip(1, "[%s] path(%s) is blank", funcKit.GetFuncName(1), path)
}
if Exists(path) && IsFile(path) {
return errorKit.NewSkip(1, "[%s] path(%s) exists but it is a file", funcKit.GetFuncName(1), path)
}
var flag bool = true
if mkdirArgs != nil {
flag = mkdirArgs[0]
}
if flag {
if err := MkDirs(path); err != nil {
return err
}
}
return nil
}
// AssertExistAndIsFile
/*
@return 如果path存在且是个文件,返回nil
*/
func AssertExistAndIsFile(path string) error {
if strKit.IsBlank(path) {
return errorKit.NewSkip(1, "[%s] path(%s) is blank", funcKit.GetFuncName(1), path)
}
if !Exists(path) {
return errorKit.NewSkip(1, "[%s] path(%s) doesn't exist", funcKit.GetFuncName(1), path)
}
if IsDir(path) {
return errorKit.NewSkip(1, "[%s] path(%s) exists but it is a directory", funcKit.GetFuncName(1), path)
}
return nil
}
// AssertExistAndIsDir
/*
@return 如果path存在且是个目录,返回nil
*/
func AssertExistAndIsDir(path string) error {
if strKit.IsBlank(path) {
return errorKit.NewSkip(1, "[%s] path(%s) is blank", funcKit.GetFuncName(1), path)
}
if !Exists(path) {
return errorKit.NewSkip(1, "[%s] path(%s) doesn't exist", funcKit.GetFuncName(1), path)
}
if IsFile(path) {
return errorKit.NewSkip(1, "[%s] path(%s) exists but it is a file", funcKit.GetFuncName(1), path)
}
return nil
}
// AssertReadableAndWritable
/*
前提: path 非blank && 存在.
*/
func AssertReadableAndWritable(path string) error {
if strKit.IsBlank(path) {
return errorKit.NewSkip(1, "[%s] path(%s) is blank", funcKit.GetFuncName(1), path)
}
if !Exists(path) {
return errorKit.NewSkip(1, "[%s] path(%s) doesn't exist", funcKit.GetFuncName(1), path)
}
if !IsReadable(path) {
return errorKit.NewSkip(1, "[%s] path(%s) isn't readable", funcKit.GetFuncName(1), path)
}
if !IsWritable(path) {
return errorKit.NewSkip(1, "[%s] path(%s) isn't writable", funcKit.GetFuncName(1), path)
}
return nil
}
func AssertReadable(path string) error {
if strKit.IsBlank(path) {
return errorKit.NewSkip(1, "[%s] path(%s) is blank", funcKit.GetFuncName(1), path)
}
if !Exists(path) {
return errorKit.NewSkip(1, "[%s] path(%s) doesn't exist", funcKit.GetFuncName(1), path)
}
if !IsReadable(path) {
return errorKit.NewSkip(1, "[%s] path(%s) isn't readable", funcKit.GetFuncName(1), path)
}
return nil
}
func AssertWritable(path string) error {
if strKit.IsBlank(path) {
return errorKit.NewSkip(1, "[%s] path(%s) is blank", funcKit.GetFuncName(1), path)
}
if !Exists(path) {
return errorKit.NewSkip(1, "[%s] path(%s) doesn't exist", funcKit.GetFuncName(1), path)
}
if !IsWritable(path) {
return errorKit.NewSkip(1, "[%s] path(%s) isn't writable", funcKit.GetFuncName(1), path)
}
return nil
}