-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpathlib.go
134 lines (116 loc) · 2.75 KB
/
pathlib.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
package pathlib
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"syscall"
"github.com/pkg/errors"
)
// Path
type Path struct {
Path string
}
// New Returns a new path.
func New(path string) *Path {
p := new(Path)
p.Path = path
return p
}
// Absolute Returns an absolute representation of path.
func (p *Path) Absolute() (*Path, error) {
pth, err := filepath.Abs(p.Path)
if err != nil {
return nil, errors.Wrap(err, "get absolute failed")
}
newP := New(pth)
return newP, nil
}
// Cwd Return a new path pointing to the current working directory.
func (p *Path) Cwd() (*Path, error) {
pth, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "get cwd failed")
}
newP := New(pth)
return newP, nil
}
// Parent Return a new path for current path parent.
func (p *Path) Parent() (*Path, error) {
pth, err := p.Absolute()
if err != nil {
return nil, errors.Wrap(err, "get parent failed")
}
dir := filepath.Dir(pth.Path)
newP := New(dir)
return newP, nil
}
// Touch Create creates the named file with mode 0666 (before umask), regardless of whether it exists.
func (p *Path) Touch() error {
f, err := os.Create(p.Path)
f.Close()
return err
}
// Unlink Remove this file or link.
func (p *Path) Unlink() error {
err := syscall.Unlink(p.Path)
return err
}
// RmDir Remove this directory. The directory must be empty.
func (p *Path) RmDir() error {
err := os.Remove(p.Path)
return err
}
// MkDir Create a new directory at this given path.
func (p *Path) MkDir(mode os.FileMode, parents bool) (err error) {
if parents {
err = os.MkdirAll(p.Path, mode)
} else {
err = os.Mkdir(p.Path, mode)
}
return
}
// Open Reads the file named by filename and returns the contents.
func (p *Path) Open() ([]byte, error) {
buf, err := ioutil.ReadFile(p.Path)
if err != nil {
return nil, err
}
return buf, nil
}
// Chmod changes the mode of the named file to mode.
func (p *Path) Chmod(mode os.FileMode) error {
return os.Chmod(p.Path, mode)
}
// JoinPath Returns a new path, Combine current path with one or several arguments
func (p *Path) JoinPath(elem ...string) *Path {
temp := []string{p.Path}
elem = append(temp, elem[0:]...)
newP := New(path.Join(elem...))
return newP
}
// Exists reports current path parent exists.
func (p *Path) Exists() bool {
_, err := os.Stat(p.Path)
return err == nil || os.IsExist(err)
}
// IsDir reports Whether this path is a directory.
func (p *Path) IsDir() bool {
f, err := os.Stat(p.Path)
if err != nil {
return false
}
return f.IsDir()
}
// IsFile reports Whether this path is a regular file.
func (p *Path) IsFile() bool {
f, e := os.Stat(p.Path)
if e != nil {
return false
}
return !f.IsDir()
}
// IsAbs reports whether the path is absolute.
func (p *Path) IsAbs() bool {
return filepath.IsAbs(p.Path)
}