-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
177 lines (144 loc) · 3.48 KB
/
local.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package boxes
import (
"os"
"os/user"
"strings"
log "github.com/sirupsen/logrus"
"io/ioutil"
"path/filepath"
"errors"
)
type LocalBox Box
func (box LocalBox) ReadBoxItem() (string, error) {
log.WithFields(log.Fields{
"boxPath": box.boxPath,
"boxName": box.boxName,
"itemName": box.itemName,
}).Info("Reading item from box")
boxDir, err := mountBox(box)
if err != nil {
return "", err
}
item, err := mountItem(boxDir, box.itemName)
if err != nil {
return "", err
}
return ReadFromFile(item)
}
func (box LocalBox) WriteBoxItem(content string) error {
log.WithFields(log.Fields{
"boxPath": box.boxPath,
"boxName": box.boxName,
"itemName": box.itemName,
}).Info("Writing item into box")
boxDir, err := mountBox(box)
if err != nil {
return err
}
item, err := mountItem(boxDir, box.itemName)
if err != nil {
return err
}
return WriteIntoFile(item, content)
}
func ReadFromFile(path string) (string, error) {
absPath, err := dirExpansion(path)
if err != nil {
return "", err
}
log.WithFields(log.Fields{
"file": absPath,
}).Debug("Open file descriptor.",)
file, err := os.Open(absPath)
defer file.Close()
if err != nil {
log.WithFields(log.Fields{
"file": absPath,
}).Debug("Problem to read file",)
return "", err
}
log.WithFields(log.Fields{
"file": absPath,
}).Debug("Reading file content.")
content, err := ioutil.ReadAll(file)
if err != nil {
log.WithFields(log.Fields{
"file": absPath,
}).Debug("Fail to read content of file.")
return "", err
}
log.WithFields(log.Fields{
"file": absPath,
}).Debug("File content readed from file!")
return string(content), nil
}
func WriteIntoFile(path, content string) error {
byteContent := []byte(content)
absPath, err := dirExpansion(path)
if err != nil {
return err
}
dir := filepath.Dir(absPath)
err = os.MkdirAll(dir, 0755)
if err != nil {
log.Debug(err.Error())
return err
}
log.WithFields(log.Fields{
"file": absPath,
}).Debug("Write bytes in file ")
err = ioutil.WriteFile(absPath, byteContent, 384)
if err != nil {
log.WithFields(log.Fields{
"file": absPath,
}).Debug(err.Error())
return err
}
log.WithFields(log.Fields{
"file": absPath,
}).Debug("Bytes writed with success")
return nil
}
func mountBox(box LocalBox) (string, error) {
absPath, err := dirExpansion(box.boxPath)
if err != nil {
return "", err
}
if box.boxName != "" {
absPath = filepath.Join(absPath, box.boxName)
} else {
log.WithFields(log.Fields{
"boxPath": box.boxPath,
"boxName": box.boxName,
}).Debug("No box was selected")
}
return absPath, nil
}
func mountItem(box, itemName string) (string, error) {
if itemName == "" {
log.Debug("Empty item name")
return "", errors.New("No item was selected.")
}
log.WithFields(log.Fields{
"parentId": box,
"itemName": itemName,
}).Debug("Non item name", itemName)
item := addSufix(filepath.Join(box, itemName))
return item, nil
}
func addSufix(box string) (string) {
ext := filepath.Ext(box)
if ext == "" || ext != ".vlt" {
return box + ".vlt"
}
return box
}
func dirExpansion(path string) (string, error) {
// This is a function to expand to absolute path in Unix System like
if strings.HasPrefix(path, "~/") {
user, _ := user.Current()
dir := user.HomeDir
return filepath.Join(dir, path[2:]), nil
}
return filepath.Abs(path)
}