aboutsummaryrefslogtreecommitdiff
path: root/files.go
blob: afdac86efa7f9506174a6833ac454068c6f31ed4 (plain)
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
/*

	This file is part of CompileTree (https://github.com/Pacien/CompileTree)

	CompileTree is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	CompileTree is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with CompileTree. If not, see <http://www.gnu.org/licenses/>.

*/

package main

import (
	"io"
	"io/ioutil"
	"os"
	"path"
	"strings"
)

// Filesystem utils

func isDir(dirPath string) bool {
	stat, err := os.Stat(dirPath)
	if err != nil {
		return false
	}
	return stat.IsDir()
}

func isHidden(fileName string) bool {
	return strings.HasPrefix(fileName, ".")
}

func ls(path string) (dirs []string, files []string) {
	content, err := ioutil.ReadDir(path)
	if err != nil {
		return
	}
	for _, element := range content {
		if isHidden(element.Name()) {
			continue
		}
		if element.IsDir() {
			dirs = append(dirs, element.Name())
		} else {
			files = append(files, element.Name())
		}
	}
	return
}

func explore(dirPath string) (paths []string) {
	dirs, _ := ls(dirPath)
	for _, dir := range dirs {
		sourceDir := path.Join(dirPath, dir)
		paths = append(paths, sourceDir)
		subDirs := explore(sourceDir)
		for _, subDir := range subDirs {
			paths = append(paths, subDir)
		}
	}
	return
}

func cp(source, target string) error {
	sourceFile, err := os.Open(source)
	if err != nil {
		return err
	}
	defer sourceFile.Close()

	dir, _ := path.Split(target)
	err = os.MkdirAll(dir, 0777)
	if err != nil {
		return err
	}

	targetFile, err := os.Create(target)
	if err != nil {
		return err
	}
	defer targetFile.Close()

	_, err = io.Copy(targetFile, sourceFile)
	return err
}

func writeFile(target string, body []byte) error {
	dir, _ := path.Split(target)
	err := os.MkdirAll(dir, 0777)
	if err != nil {
		return err
	}
	err = ioutil.WriteFile(target, body, 0777)
	return err
}