forked from goanywhere/fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
183 lines (162 loc) · 4.24 KB
/
fs.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package fs
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
)
// Getcd returns absolute path of the caller.
func Getcd(skip int) string {
if dir := Geted(); strings.HasPrefix(dir, os.TempDir()) {
pc, _, _, _ := runtime.Caller(skip + 1)
function := runtime.FuncForPC(pc)
filename, _ := function.FileLine(0)
return path.Dir(filename)
} else {
return dir
}
}
// Geted returns an absolute path to the executable.
func Geted() string {
if dir, err := filepath.Abs(filepath.Dir(os.Args[0])); err == nil {
return dir
} else {
panic("Failed to retrieve executable directory")
}
}
// Getwd returns a absolute path of the current directory.
func Getwd() string {
if cwd, err := os.Getwd(); err == nil {
cwd, _ = filepath.Abs(cwd)
return cwd
} else {
panic("Failed to retrieve current working directory")
}
}
// Abs finds the absolute path for the given path.
// Supported Formats:
// * empty path => current working directory.
// * '.', '..' & '~'
// *NOTE* Abs does NOT check the existence of the path.
func Abs(path string) string {
var abs string
cwd, _ := os.Getwd()
if path == "" || path == "." {
abs = cwd
} else if path == ".." {
abs = filepath.Join(cwd, path)
} else if strings.HasPrefix(path, "~/") {
abs = filepath.Join(UserDir(), path[2:])
} else if strings.HasPrefix(path, "./") {
abs = filepath.Join(cwd, path[2:])
} else if strings.HasPrefix(path, "../") {
abs = filepath.Join(cwd, "..", path[2:])
} else {
return path
}
return abs
}
// Copy recursively copies files/(sub)directoires into the given path.
// *NOTE* It uses platform's native copy commands (windows: copy, *nix: rsync).
func Copy(src, dst string) (err error) {
var cmd *exec.Cmd
src, dst = Abs(src), Abs(dst)
// Determine the command we need to use.
if runtime.GOOS == "windows" {
// *NOTE* Not sure this will work correctly, we don't have Windows to test.
if IsFile(src) {
cmd = exec.Command("copy", src, dst)
} else {
cmd = exec.Command("xcopy", src, dst, "/S /E")
}
} else {
cmd = exec.Command("rsync", "-a", src, dst)
}
if stdout, err := cmd.StdoutPipe(); err == nil {
if stderr, err := cmd.StderrPipe(); err == nil {
// Start capturing the stdout/err.
err = cmd.Start()
io.Copy(os.Stdout, stdout)
buffer := new(bytes.Buffer)
buffer.ReadFrom(stderr)
cmd.Wait()
if cmd.ProcessState.String() != "exit status 0" {
err = fmt.Errorf("\t%s\n", buffer.String())
}
}
}
return
}
// Exists check if the given path exists.
func Exists(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// Find matches files with regular expression pattern under the given root.
func Find(root string, pattern *regexp.Regexp) (paths []string) {
if Exists(root) {
filepath.Walk(root, func(path string, info os.FileInfo, e error) error {
if pattern.MatchString(path) {
paths = append(paths, info.Name())
}
return e
})
}
return
}
// Grep searches text files via regular expression under the given path,
// paths of the files contain matched line(s) will be returned.
func Grep(root string, pattern *regexp.Regexp) (paths []string) {
panic(fmt.Errorf("Not Implemented"))
}
// Glob recursively finds the names of all files matching pattern under the given path.
func Glob(path string, pattern string) (matches []string, err error) {
err = filepath.Walk(path, func(path string, info os.FileInfo, e error) error {
if e == nil {
if info.IsDir() {
if filenames, e := filepath.Glob(filepath.Join(path, pattern)); e == nil {
matches = append(matches, filenames...)
}
}
}
return e
})
return
}
// IsDir checks if the given path is a directory.
func IsDir(path string) bool {
src, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return src.IsDir()
}
// IsFile checks if the given path is a file.
func IsFile(path string) bool {
src, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return !src.IsDir()
}
// UserDir finds base path of current system user.
func UserDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}