-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrice.go
218 lines (175 loc) · 6.18 KB
/
rice.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package ricecore
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
)
//A rice struct represents a rice with its files and other basic info
type Rice struct {
Name string `json:"name"`
Program string `json:"prog"`
Root string `json:"root"`
Files []*RiceFile `json:"files"`
}
//A ricefile struct is a file within a rice. It contains the file name and
//its respective location
type RiceFile struct {
Location string `json:"location"`
File string `json:"file"`
}
//Creates a rice and stores the information as a json
func CreateRice(name string, prog string, root string, files []*RiceFile) (rice *Rice, err error) {
rice = &Rice{Name: name, Program: prog, Root: root, Files: files}
jsonData, err := json.MarshalIndent(rice, "", " ")
if err != nil {
return nil, err
}
riceDir := rdbDir + prog + "/" + name + "/"
if !exists(riceDir) {
os.MkdirAll(riceDir, 0775)
}
jsonFile, err := os.Create(riceDir + "rice.json")
if err != nil {
return nil, err
}
defer jsonFile.Close()
jsonFile.Write(jsonData)
return rice, nil
}
//Loads a rice.json file from an existing rice and returns the struct
func GetRice(name string, prog string) (rice *Rice, err error) {
riceDir := rdbDir + prog + "/" + name + "/"
jsonFile, err := os.Open(riceDir + "rice.json")
if err != nil {
return nil, err
}
defer jsonFile.Close()
rice = new(Rice)
jsonParser := json.NewDecoder(jsonFile)
if err = jsonParser.Decode(&rice); err != nil {
return nil, err
}
return rice, nil
}
//Returns the currently active rice for a program
func GetActiveRice(prog string) (rice *Rice, err error) {
progDir := rdbDir + prog + "/"
s, err := ioutil.ReadFile(progDir + ".active")
if err != nil {
return nil, errors.New("Error, the .active file does not exist for this program.")
}
rname := string(s)
return GetRice(rname, prog)
}
//Deactivates the currently active rice for a program
func DeactivateCurrentRice(prog string) (err error) {
crice, err := GetActiveRice(prog)
if err != nil {
return err
}
if err = crice.deactivate(); err != nil {
return err
}
return nil
}
//Initializes a created local rice, extracting the files from the directory to
//the rdb dir. Should be used to intialize a rice the first time
//for a given prgoram.
func (rice Rice) FirstInit() (err error) {
riceDir := rdbDir + rice.Program + "/" + rice.Name + "/"
progDir := expandDir(rice.Root)
for _, rf := range rice.Files {
if !exists(riceDir + rf.Location) {
os.MkdirAll(riceDir+rf.Location, 0755)
}
if err = os.Rename(progDir+rf.Location+rf.File, riceDir+rf.Location+rf.File); err != nil {
return errors.New("Error, this rice was not initialized properly: File: " + rf.Location + rf.File + " was not properly moved. Additional info: " + err.Error())
}
}
return nil
}
func (rice Rice) LocalInit(riceLoc string) (err error) {
riceDir := rdbDir + rice.Program + "/" + rice.Name + "/"
progDir := expandDir(riceLoc)
for _, rf := range rice.Files {
if !exists(riceDir + rf.Location) {
os.MkdirAll(riceDir+rf.Location, 0755)
}
if err = os.Rename(progDir+rf.Location+rf.File, riceDir+rf.Location+rf.File); err != nil {
return errors.New("Error, this rice was not initialized properly: File: " + rf.Location + rf.File + " was not properly moved. Additional info: " + err.Error())
}
}
return nil
}
//Activates a Rice by symlinking the files into the specified dirs
func (rice Rice) Activate() (err error) {
riceDir := rdbDir + rice.Program + "/" + rice.Name + "/"
progDir := expandDir(rice.Root)
for _, rf := range rice.Files {
if !exists(progDir + rf.Location) {
os.MkdirAll(progDir+rf.Location, 0755)
}
if err = os.Symlink(riceDir+rf.Location+rf.File, progDir+rf.Location+rf.File); err != nil {
return errors.New("Error, this rice was not symlinked properly: File: " + rf.Location + rf.File + " was not properly symlinked. Additional info: " + err.Error())
}
}
activeRice := []byte(rice.Name)
if err = ioutil.WriteFile(rdbDir+rice.Program+"/.active", activeRice, 0755); err != nil {
return errors.New("Error, the .active file could not be created properly. Additional info: " + err.Error())
}
return nil
}
//Deactivates a rice by deleting all specified symlinks for that rice.
//Not publicly available because it would be another dumb thing to
//screw up. DeactivateCurrentRice should be used publicly
func (rice Rice) deactivate() (err error) {
activeFile := rdbDir + rice.Program + "/.active"
progDir := expandDir(rice.Root)
for _, rf := range rice.Files {
if !exists(progDir + rf.Location + rf.File) {
return errors.New("Error, a specified file does not seem to exist")
}
if err = os.Remove(progDir + rf.Location + rf.File); err != nil {
return errors.New("Error, this rice was not deactivated properly: File: " + rf.Location + rf.File + " was not properly removed. Additional info: " + err.Error())
}
}
if exists(activeFile) {
if err = os.Remove(activeFile); err != nil {
return errors.New("Error, could not remove .active file. Additional info: " + err.Error())
}
}
return nil
}
//Swaps in a rice by deactivating the currently active rice
//and activating the given rice
func (rice Rice) Swap() (err error) {
if err := DeactivateCurrentRice(rice.Program); err != nil {
return err
}
if err = rice.Activate(); err != nil {
return err
}
return nil
}
//Uninstalls a rice for a program, deleting the symlinks
//and moving the files back to their original locations
func (rice Rice) Uninstall() (err error) {
//Deactivates any active rice
DeactivateCurrentRice(rice.Program)
riceDir := rdbDir + rice.Program + "/" + rice.Name + "/"
progDir := expandDir(rice.Root)
for _, rf := range rice.Files {
if !exists(progDir + rf.Location) {
os.MkdirAll(progDir+rf.Location, 0755)
}
//Do better safety here, possibly use copys(these are more painful)
if err = os.Rename(riceDir+rf.Location+rf.File, progDir+rf.Location+rf.File); err != nil {
return errors.New("Error, this rice was not uninstalled properly: File: " + rf.Location + rf.File + " was not properly moved. Additional info: " + err.Error())
}
}
if err = os.RemoveAll(riceDir); err != nil {
return errors.New("Error, the remaining files could not be removed. Additional info: " + err.Error())
}
return nil
}