-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
212 lines (175 loc) · 5.27 KB
/
util.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//Package filefriend is a convenient wrapper around the os/filepath module for Go. Provides a wide variety of functions that combines
//packages into one, single wrapper, allowing for flexible and easy modifications of the systems file and folders.
package filefriend
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
"strings"
)
// File represent a file in a folder. Containing useful stats and information about the file.
//
// name: The name of the file without extension or path.
//
// extension: The extension of the file.
//
// folder: The relative folder path of the file.
//
// path: The absolute filesystems path to the folder.
//
// size: The size of the file in bytes in string format.
//
// lastChanged: The files last modified time in string format.
type File struct {
name string
extension string
folder string
path string
size string
lastChanged string
}
// FilenameWithoutExt returns the cleaned filename without the folderpath or extension.
func FilenameWithoutExt(file string) string {
return strings.TrimSuffix(filepath.Base(file), path.Ext(file))
}
// FullAbsPath returns the full absolute path from the passed in file or potensial error that occurred.
func FullAbsPath(file string) (string, error) {
absPath, err := os.Getwd()
if err != nil {
return "", err
}
// return the joined absolute path
// and dirname of passed in file
return filepath.Join(absPath, filepath.Dir(file)), err
}
// GetSize returns the size of the passed in file or potensial error that occurred.
func GetSize(file string) (string, error) {
stats, err := os.Stat(file)
if err != nil {
return "", err
}
// return the file size from stats
return strconv.Itoa(int(stats.Size())), err
}
// GetChangedTime returns the timestamp of the passed in file's last modified time, or potensial error that occurred.
func GetChangedTime(file string) (string, error) {
stats, err := os.Stat(file)
if err != nil {
return "", err
}
// return the modified time as string
return stats.ModTime().String(), err
}
// IsFolder returns a boolean (true) if the given path is a folder.
// If the path is a file, return boolean (false) and the error with the reason.
func IsFolder(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
// return info if path is folder
return fileInfo.IsDir(), err
}
// SanitizePath formats the path into system navigatable format.
// If the path is invalid for system, format the path with starting './' and trailing '/'
func SanitizePath(path string) string {
// check first char
if path[0] != '/' && path[0] != '\\' {
path = "./" + path
}
// check last char
if path[len(path)-1] != '/' && path[len(path)-1] != '\\' {
path = path + "/"
}
return path
}
// Move renames and moves the files from one directory,
// to another depending on folder structure input.
// Returns error or nil depending on successful move or rename.
func Move(from string, to string) error {
err := os.Rename(from, to)
return err
}
// Create creates 'n' amount of files, with extension, to passed in folder,
// with passed in content, that will be written to each single file.
//
// If file in folder exists, Create will skip to avoid remake of the file.
// Returns an error or nil depending on successful write or fail.
func Create(name string, ext string, folder string, content string, amount int) error {
// check if folder exists, create if not exists
folder = SanitizePath(folder)
err := CreateFolder(folder)
if err != nil {
return err
}
fileName := name
for i := 0; i < amount; i++ {
// update file index name
if i > 0 {
fileName = name + strconv.Itoa(i)
}
// check if path to file that should be created exists
file := folder + fileName + "." + ext
exists := PathExists(file)
// if it does not exists, continue to create file
if !exists {
// attempt to create file
err := ioutil.WriteFile(file, []byte(content), 0755)
// handle potensial error
if err != nil {
return err
}
}
}
return nil
}
// PathExists checks if a given path exists or not.
// Returns a boolean (true/false) depending on case.
func PathExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
// CreateFolder creates a new folder at a given path.
// If the folder does not exist create it.
// Returns an error if the creation failed.
func CreateFolder(path string) error {
// check if folder exists, create if not
if !PathExists(path) {
err := os.MkdirAll(path, 0755)
if err != nil {
return err
}
}
return nil
}
// GetFileInfo retrieves the stats & information about a specific file.
// Returns a pointer to the file or potensial error that occurred.
func GetFileInfo(file string) (*File, error) {
// get stats releated data from file and,
// handle error occurred during stats opertation
path, err := FullAbsPath(file)
if err != nil {
return nil, err
}
size, err := GetSize(file)
if err != nil {
return nil, err
}
lastChanged, err := GetChangedTime(file)
if err != nil {
return nil, err
}
// add new File struct with file properties
return &File{
name: FilenameWithoutExt(file),
extension: filepath.Ext(file),
folder: filepath.Dir(file),
path: path,
size: size,
lastChanged: lastChanged,
}, nil
}