aboutsummaryrefslogtreecommitdiff
path: root/files.go
diff options
context:
space:
mode:
Diffstat (limited to 'files.go')
-rw-r--r--files.go106
1 files changed, 106 insertions, 0 deletions
diff --git a/files.go b/files.go
new file mode 100644
index 0000000..afdac86
--- /dev/null
+++ b/files.go
@@ -0,0 +1,106 @@
1/*
2
3 This file is part of CompileTree (https://github.com/Pacien/CompileTree)
4
5 CompileTree is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 CompileTree is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with CompileTree. If not, see <http://www.gnu.org/licenses/>.
17
18*/
19
20package main
21
22import (
23 "io"
24 "io/ioutil"
25 "os"
26 "path"
27 "strings"
28)
29
30// Filesystem utils
31
32func isDir(dirPath string) bool {
33 stat, err := os.Stat(dirPath)
34 if err != nil {
35 return false
36 }
37 return stat.IsDir()
38}
39
40func isHidden(fileName string) bool {
41 return strings.HasPrefix(fileName, ".")
42}
43
44func ls(path string) (dirs []string, files []string) {
45 content, err := ioutil.ReadDir(path)
46 if err != nil {
47 return
48 }
49 for _, element := range content {
50 if isHidden(element.Name()) {
51 continue
52 }
53 if element.IsDir() {
54 dirs = append(dirs, element.Name())
55 } else {
56 files = append(files, element.Name())
57 }
58 }
59 return
60}
61
62func explore(dirPath string) (paths []string) {
63 dirs, _ := ls(dirPath)
64 for _, dir := range dirs {
65 sourceDir := path.Join(dirPath, dir)
66 paths = append(paths, sourceDir)
67 subDirs := explore(sourceDir)
68 for _, subDir := range subDirs {
69 paths = append(paths, subDir)
70 }
71 }
72 return
73}
74
75func cp(source, target string) error {
76 sourceFile, err := os.Open(source)
77 if err != nil {
78 return err
79 }
80 defer sourceFile.Close()
81
82 dir, _ := path.Split(target)
83 err = os.MkdirAll(dir, 0777)
84 if err != nil {
85 return err
86 }
87
88 targetFile, err := os.Create(target)
89 if err != nil {
90 return err
91 }
92 defer targetFile.Close()
93
94 _, err = io.Copy(targetFile, sourceFile)
95 return err
96}
97
98func writeFile(target string, body []byte) error {
99 dir, _ := path.Split(target)
100 err := os.MkdirAll(dir, 0777)
101 if err != nil {
102 return err
103 }
104 err = ioutil.WriteFile(target, body, 0777)
105 return err
106}