-
Notifications
You must be signed in to change notification settings - Fork 140
/
test.go
64 lines (57 loc) · 1.23 KB
/
test.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
package sh
import (
"os"
"path/filepath"
)
func filetest(name string, modemask os.FileMode) (match bool, err error) {
fi, err := os.Stat(name)
if err != nil {
return
}
match = (fi.Mode() & modemask) == modemask
return
}
func (s *Session) Getwd() string {
dir := string(s.dir)
if dir == "" {
dir, _ = os.Getwd()
}
return dir
}
func (s *Session) abspath(name string) string {
if filepath.IsAbs(name) {
return name
}
return filepath.Join(s.Getwd(), name)
}
func init() {
//log.SetFlags(log.Lshortfile | log.LstdFlags)
}
// expression can be dir, file, link
func (s *Session) Test(expression string, argument string) bool {
var err error
var fi os.FileInfo
fi, err = os.Lstat(s.abspath(argument))
switch expression {
case "d", "dir":
return err == nil && fi.IsDir()
case "f", "file":
return err == nil && fi.Mode().IsRegular()
case "x", "executable":
/*
fmt.Println(expression, argument)
if err == nil {
fmt.Println(fi.Mode())
}
*/
return err == nil && fi.Mode()&os.FileMode(0100) != 0
case "L", "link":
return err == nil && fi.Mode()&os.ModeSymlink != 0
}
return false
}
// expression can be d,dir, f,file, link
func Test(exp string, arg string) bool {
s := NewSession()
return s.Test(exp, arg)
}