-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl.filepath.go
137 lines (120 loc) · 2.87 KB
/
stl.filepath.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
package stl
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
type XPFilePathImpl struct {
absolutePath string
exists bool
nameWithExt string
nameWithOutExt string
ext string
dir *XPFilePathImpl
isFile *bool
}
func NewFilePath(p string) (*XPFilePathImpl, error) {
path := new(XPFilePathImpl)
absPath, err := filepath.Abs(p)
if err != nil {
return nil, err
}
path.absolutePath = filepath.ToSlash(absPath)
path.nameWithExt = filepath.Base(absPath)
path.ext = filepath.Ext(path.nameWithExt)
path.nameWithOutExt = path.nameWithExt[:len(path.nameWithExt)-len(path.ext)]
path.Update()
return path, nil
}
func NewFilePathFromCurrentPath() (*XPFilePathImpl, error) {
path := new(XPFilePathImpl)
file, _ := exec.LookPath(os.Args[0])
absPath, err := filepath.Abs(file)
if err != nil {
return nil, err
}
path.absolutePath = filepath.ToSlash(absPath)
path.nameWithExt = filepath.Base(absPath)
path.ext = filepath.Ext(path.nameWithExt)
path.nameWithOutExt = path.nameWithExt[:len(path.nameWithExt)-len(path.ext)]
path.Update()
return path, nil
}
func (p *XPFilePathImpl) Update() {
stat, err := os.Stat(p.absolutePath)
if err != nil {
return
}
p.exists = true
isf, isd := stat.Mode().IsRegular(), stat.Mode().IsDir()
if !isf && !isd {
return
}
p.isFile = &isf
if *p.isFile {
p.dir, _ = NewFilePath(p.absolutePath[:len(p.absolutePath)-len(p.nameWithExt)])
} else {
p.dir = p
}
}
func (p *XPFilePathImpl) Info() string {
info := fmt.Sprintf(
"Absolute path: %s\nExists: %t\n",
p.AbsolutePath(),
p.Exists(),
)
if p.Exists() {
if !*p.isFile {
info += fmt.Sprint("Type: Directory\n")
return info
}
info += fmt.Sprintf("Type: File\nExtension: %s\n", p.Extension())
}
return info
}
func (p *XPFilePathImpl) AbsolutePath() string {
return p.absolutePath
}
func (p *XPFilePathImpl) Exists() bool {
return p.exists
}
func (p *XPFilePathImpl) Directory() *XPFilePathImpl {
return p.dir
}
func (p *XPFilePathImpl) NameWithExtension() string {
return p.nameWithExt
}
func (p *XPFilePathImpl) Name() string {
return p.nameWithExt
}
func (p *XPFilePathImpl) NameWithoutExtension() string {
return p.nameWithOutExt
}
func (p *XPFilePathImpl) Extension() string {
return p.ext
}
func (p *XPFilePathImpl) IsFile() *bool {
return p.isFile
}
func (p *XPFilePathImpl) IsDirectory() *bool {
if p.isFile == nil {
return nil
}
isDir := !*p.isFile
return &isDir
}
func (p *XPFilePathImpl) Append(sp string) *XPFilePathImpl {
result, _ := NewFilePath(filepath.Join(p.absolutePath, sp))
return result
}
func (p *XPFilePathImpl) ReadFile() (content []byte, err error) {
if p.Exists() {
if *p.IsFile() {
return ioutil.ReadFile(p.AbsolutePath())
}
return nil, NewErrors("path object must be of file type")
}
return nil, NewErrors("path object refers to non-existing entity")
}