-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handling.go
162 lines (141 loc) · 4.13 KB
/
file_handling.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
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2024 Dídimo Grimaldo T.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
*-----------------------------------------------------------------*/
package wipechromium
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
/* ----------------------------------------------------------------
* G l o b a l s
*-----------------------------------------------------------------*/
/* ----------------------------------------------------------------
* M e t h o d s
*-----------------------------------------------------------------*/
/* ----------------------------------------------------------------
* F u n c t i o n s
*-----------------------------------------------------------------*/
// AtHome gets HOME directory and return the path of fileOrDir right underneath.
// If HOME is /home/toering then AtHome(".config") returns /home/toering/.config
func AtHome(fileOrDir string) string {
atHome := ""
if home, err := os.UserHomeDir(); err == nil {
atHome = filepath.Join(home, fileOrDir)
} else {
log.Fatal("Could not get HOME directory")
}
return atHome
}
// FromHome() takes a path and if it starts with the HOME directory, it is
// replaced by the ~/ home directory notation.
func FromHome(fileOrDir string) string {
atHome := fileOrDir
if home, err := os.UserHomeDir(); err == nil {
if strings.HasPrefix(fileOrDir, home) { // replace by ~/ notation
atHome = "~" + strings.TrimPrefix(fileOrDir, home)
}
}
return atHome
}
// Check whether path exists and it is a directory
func IsDirectory(path string) bool {
if finfo, err := os.Stat(path); err == nil {
return finfo.IsDir()
}
// not exist or not a directory
return false
}
func IsFile(path string) TriState {
if finfo, err := os.Stat(path); err == nil {
if !finfo.IsDir() {
return Yes
} else {
return No
}
}
// not exist or not a directory
return Undecided
}
func MoveDir(src, dest string) error {
// ensure both are directories
if !IsDirectory(src) {
return fmt.Errorf("Not a directory: %s", src)
}
if !IsDirectory(dest) {
return fmt.Errorf("Not a directory: %s", dest)
}
// Rename will do Move IF both are directories
if err := os.Rename(src, dest); err != nil {
return err
}
log.Printf("Moved directory to %s", dest)
return nil
}
// Move and optionally rename a file.
// Example: moveFile("code/paragraph.go", "code/text/text_handling.go")
func MoveFile(src, dest string, notify bool) error {
// In GO rename allows to rename and move a file in one step
if err := os.Rename(src, dest); err != nil {
log.Print("REN-F", src)
log.Print("REN-T", dest)
return err
}
if notify {
log.Printf("Moved %s", src)
}
return nil
}
// Same as moveFile except the destination is a directory rather than a
// fully-qualified filename
// Example: moveFileTo("/home/pi/delete.me", "/tmp")
func MoveFileTo(src, dest string, notify bool) error {
if !IsDirectory(dest) {
return fmt.Errorf("Not a directory: %s", dest)
}
//filename := filepath.Base(src)
//destFull := filepath.Join(dest, filename)
//return moveFile(src, destFull, true)
return MoveFile(src, dest, true)
}
// Example: changePath("/home/pi/test.sh", "/tmp/anydir")
func ChangePath(src, dest string) string {
base := filepath.Base(src)
result := filepath.Join(dest, base)
return result
}
// Removes all files matching a Pattern at Dir.
// Example: removeWithPattern("/home/lordofscripts/.cache", "*.log")
func RemoveWithPattern(dir, pattern string) error {
glob := dir + string(os.PathSeparator) + pattern
files, err := filepath.Glob(glob)
if err != nil {
return err
}
for _, fname := range files {
if err := os.Remove(fname); err != nil {
return err
}
}
log.Printf("Deleted %s on %s", pattern, dir)
return nil
}
func MoveWithPattern(dir, pattern string) error {
glob := dir + string(os.PathSeparator) + pattern
files, err := filepath.Glob(glob)
if err != nil {
return err
}
for _, fname := range files {
if err := os.Remove(fname); err != nil {
return err
}
}
log.Printf("Deleted %s on %s", pattern, dir)
return nil
}