/
os.go
72 lines (63 loc) · 1.72 KB
/
os.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
package helper // import "aduu.dev/utils/helper"
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
// MoveFile copies the given file to the destination. It does not delete the source.
func MoveFile(sourcePath, destPath string) error {
inputFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("couldn't open source file: %s", err)
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return fmt.Errorf("couldn't open dest file: %s", err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return fmt.Errorf("writing to output file failed: %s", err)
}
/*
// The copy was successful, so now delete the original file
err = os.Remove(sourcePath)
if err != nil {
return fmt.Errorf("Failed removing original file: %s", err)
}
*/
return nil
}
// ClearDirectory clears the directory of all files and folders except the listed ones.
// Can only keep files directly inside the folder.
func ClearDirectory(path string, skipFiles ...string) error {
if !filepath.IsAbs(path) {
return fmt.Errorf("won't clean non-absolute path %s", path)
}
if !DoesPathExist(path) {
return fmt.Errorf("path to clear does not exist: %s", path)
}
files, err := ioutil.ReadDir(path)
if err != nil {
return fmt.Errorf("failed to read directory")
}
clearSkipMap := make(map[string]bool)
for _, skipFile := range skipFiles {
clearSkipMap[skipFile] = true
}
for _, file := range files {
// Skip .git-folder.
if clearSkipMap[file.Name()] {
continue
}
absFile := filepath.Join(path, file.Name())
if err := os.RemoveAll(absFile); err != nil {
return fmt.Errorf("failed to remove file %s: %v", absFile, err)
}
}
return nil
}