-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.go
55 lines (47 loc) · 1.24 KB
/
methods.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
package dir
import (
"errors"
"os"
"path/filepath"
)
type Directory string
// New builds an absolute path from the provided path, makes the required directories to achieve it, and returns a Directory
func New(path string) (Directory, error) {
_path, err := filepath.Abs(path)
if err != nil {
return "", err
}
return Directory(_path), os.MkdirAll(path, 0755)
}
// Open builds an absolute path from the provided path, confirms the path exists, and returns a Directory
func Open(path string) (Directory, error) {
_path, err := filepath.Abs(path)
if err != nil {
return "", err
}
_, err = os.Stat(_path)
return Directory(_path), err
}
// Path gets the absolute path of the directory
func (d Directory) Path() string {
return string(d)
}
// Remove removes the directory and any children within it
func (d Directory) Remove() error {
if d == "" {
return errors.New("Can't remove empty Directory")
}
return os.RemoveAll(d.Path())
}
// Move moves the directory to the provided path, returns a directory of the new path
func (d Directory) Move(to string) (Directory, error) {
_path, err := filepath.Abs(to)
if err != nil {
return "", err
}
err = os.Rename(d.Path(), _path)
if err != nil {
return "", err
}
return Directory(_path), nil
}