-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.go
153 lines (121 loc) · 2.91 KB
/
filesystem.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
package libdotfilesync
import (
"errors"
//"fmt"
"io"
"os"
//"github.com/jochemste/dotfile_sync/utils"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/sergi/go-diff/diffmatchpatch"
)
type FileSystem struct {
FS billy.Filesystem
}
var Filesystem FileSystem
/*
var filesystem billy.Filesystem
var pFS *billy.Filesystem
*/
func NewFS() {
Filesystem = FileSystem{}
Filesystem.FS = memfs.New()
}
// Find in filesystem
func FindInFS(filename string) (string, error) {
// Read root directory of memory file system
files, err := Filesystem.FS.ReadDir("/")
if err != nil {
return "", errors.New("Could not find file: " + err.Error())
}
res, err := FindInFSDir(filename, files, "")
if err != nil {
return "", errors.New("Could not find file: " + err.Error())
}
return res, nil
}
// Recursive find in directory function (private)
func FindInFSDir(filename string, files []os.FileInfo, dirname string) (string, error) {
for _, file := range files {
if file.Name() == filename {
return dirname + "/" + file.Name(), nil
}
if file.IsDir() == true {
f, err := Filesystem.FS.ReadDir(file.Name())
if err != nil {
return "", err
}
res, err := FindInFSDir(filename, f, dirname+"/"+file.Name())
if res != "" {
return res, err
}
}
}
return "", errors.New("File was not found")
}
/*
func FileToFS(filename string) error {
if !utils.FileExists(filename) {
return errors.New("Could not write file to FS, File " + filename + " does not exist")
}
filesystem.Create(filename)
return nil
}
*/
func WriteToFS(filename string, content string) error {
newfd, err := Filesystem.FS.Create(filename)
if err != nil {
errors.New("Could not write to FS: " + err.Error())
}
newfd.Write([]byte(content))
newfd.Close()
return nil
}
func GetFileDescriptorFS(filename string) (billy.File, error) {
var fd billy.File
path, err := FindInFS(filename)
if path == "" && err != nil {
fd, err = Filesystem.FS.Create(filename)
}
return fd, err
}
func GetContentFS(filename string) (string, error) {
file, err := Filesystem.FS.Open(filename)
if err != nil {
return "", errors.New("Could not read file contents from FS: " + err.Error())
}
defer file.Close()
const chunk = 1024
buffer := make([]byte, chunk)
content := ""
for {
length, err := file.Read(buffer)
if err == io.EOF {
break
}
if err != nil {
return "", errors.New("Could not read file contents from FS: " + err.Error())
}
if length > 0 {
content += string(buffer[:length])
}
}
return content, nil
}
func ContentIsDiff(cont1 string, cont2 string) (bool, error) {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(cont1, cont2, false)
for _, d := range diffs {
if d.Type != diffmatchpatch.DiffEqual {
return true, nil
}
}
return false, nil
}
func ExistsInFS(filename string) bool {
path, err := FindInFS(filename)
if path == "" && err != nil {
return false
}
return true
}