-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
76 lines (66 loc) · 1.39 KB
/
file.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
package main
import (
"github.com/bmatcuk/doublestar"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
func readFile(file string) string {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal("File reading error: "+file, err)
}
return string(data)
}
func listFiles(directory string, pattern string) []string {
globPattern := filepath.Join(directory, pattern)
files, err := doublestar.Glob(globPattern)
if err != nil {
log.Fatal(err)
}
return files
}
func excludePath(files []string, path string) (res []string) {
for _, file := range files {
if !hasSubPath(path, file) {
res = append(res, file)
}
}
return res
}
func hasSubPath(path string, subPath string) bool {
relativePath, err := filepath.Rel(path, subPath)
if err != nil {
log.Fatal(err)
}
return !strings.Contains(relativePath, "..")
}
func writeFile(file string, content string) {
err := ioutil.WriteFile(file, []byte(content), 0644)
if err != nil {
log.Fatal(err)
}
}
func createTempFile(content string) *os.File {
tmpfile, err := ioutil.TempFile("", "tpl*.yaml")
if err != nil {
log.Fatal(err)
}
if _, err := tmpfile.Write([]byte(content)); err != nil {
tmpfile.Close()
log.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
return tmpfile
}
func pathExist(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return true
}