forked from agrinman/chasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
311 lines (257 loc) · 6.57 KB
/
main.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"sync"
"github.com/codegangsta/cli"
"github.com/fatih/color"
)
/// chasm commands ///
func loadChasm(c *cli.Context) error {
CreateOrLoadChasmDir(chasmRoot)
return nil
}
func startChasm(c *cli.Context) error {
loadChasm(c)
if preferences.NeedSetup() {
color.Red("Warning: not enough services.")
return nil
}
// start the watcher
color.Green("Starting chasm. Listening on %s", preferences.root)
StartWatching(preferences.root, preferences.DirMap)
return nil
}
func statusChasm(c *cli.Context) error {
loadChasm(c)
color.Green("Cloud stores:")
for i, cs := range preferences.AllCloudStores() {
fmt.Println(color.GreenString("%v)", i+1), cs.Description())
}
if preferences.NeedSetup() {
color.Red("Warning: not enough services.")
}
return nil
}
func restoreChasm(c *cli.Context) error {
loadChasm(c)
if preferences.NeedSetup() {
color.Red("Warning: not enough services. Cannot Restore.")
return nil
}
color.Green("Preparing to restore chasm to %s", preferences.root)
Restore()
return nil
}
func removeChasm(c *cli.Context) error {
loadChasm(c)
numStores := preferences.RegisteredServices()
if numStores == 0 {
color.Red("There are no cloud stores to delete.")
return nil
}
color.Green("Cloud stores:")
for i, cs := range preferences.AllCloudStores() {
fmt.Println(color.GreenString("%v)", i+1), cs.ShortDescription())
}
color.Cyan("Enter the number of the store you would like to remove:")
var d int
for true {
_, err := fmt.Scanf("%d", &d)
if err != nil || d < 0 || d > numStores {
color.Red("Please enter a number between %v and %v", 1, numStores)
} else {
break
}
}
if d <= len(preferences.FolderStores) {
ind := d - 1
preferences.FolderStores[ind].Clean()
preferences.FolderStores = append(preferences.FolderStores[:ind], preferences.FolderStores[ind+1:]...)
color.Yellow("Deleting Folder Store...")
} else if d <= len(preferences.FolderStores)+len(preferences.GDriveStores) {
ind := d - 1 - len(preferences.FolderStores)
preferences.GDriveStores[ind].Clean()
preferences.GDriveStores = append(preferences.GDriveStores[:ind], preferences.GDriveStores[ind+1:]...)
color.Yellow("Deleting Google Drive Store...")
} else {
ind := d - 1 - len(preferences.FolderStores) - len(preferences.GDriveStores)
preferences.DropboxStores[ind].Clean()
preferences.DropboxStores = append(preferences.DropboxStores[:ind], preferences.DropboxStores[ind+1:]...)
color.Yellow("Deleting Dropbox Store...")
}
preferences.Save()
syncChasm(c)
return nil
}
func cleanChasm(c *cli.Context) error {
loadChasm(c)
var wg sync.WaitGroup
for _, cs := range preferences.AllCloudStores() {
wg.Add(1)
go func(c CloudStore) {
defer wg.Done()
c.Clean()
color.Green("Done cleaning %v", c.ShortDescription())
}(cs)
}
wg.Wait()
return nil
}
func syncChasm(c *cli.Context) error {
color.Green("Clean:")
cleanChasm(c)
color.Green("Done cleaning.\nBeginning sync:")
if preferences.NeedSetup() {
color.Red("Error: not enough services. Cannot sync.")
return nil
}
files, _ := ioutil.ReadDir(preferences.root)
currentFileMap := make(map[string]bool)
for _, f := range files {
if f.Name() == chasmPrefFile {
continue
}
path := path.Join(preferences.root, f.Name())
currentFileMap[path] = true
AddFile(path)
}
// remove invalid entries in existing file map
for filePath, _ := range preferences.FileMap {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
delete(preferences.FileMap, filePath)
}
}
preferences.Save()
AddFile(path.Join(preferences.root, chasmPrefFile))
color.Green("Done syncing.")
return nil
}
//MARK: Add Handlers
func addFolder(c *cli.Context) error {
loadChasm(c)
var folderStore FolderStore
if len(c.Args()) < 1 {
color.Red("Error: missing folder path")
return nil
}
folderStore.Path = c.Args()[0]
if !folderStore.Setup() {
color.Red("(Cloud Store) Folder Store: setup incomplete.")
return nil
}
preferences.FolderStores = append(preferences.FolderStores, folderStore)
preferences.Save()
color.Green("Success! Added folder store: %s", folderStore.Path)
return nil
}
func addDrive(c *cli.Context) error {
loadChasm(c)
var gdrive GDriveStore
if (&gdrive).Setup() == false {
color.Red("(Cloud Store) Google Drive: setup incomplete.")
return nil
}
// only 1 gdrive store
preferences.GDriveStores = append(preferences.GDriveStores, gdrive)
preferences.Save()
color.Green("Success! Added Google Drive Store.")
return nil
}
func addDropbox(c *cli.Context) error {
loadChasm(c)
var dropbox DropboxStore
if (&dropbox).Setup() == false {
color.Red("(Cloud Store) Dropbox: setup incomplete.")
return nil
}
// only 1 dropbox store
preferences.DropboxStores = append(preferences.DropboxStores, dropbox)
preferences.Save()
color.Green("Success! Added Dropbox Store.")
return nil
}
/// Cli toolchain ///
var chasmRoot string
func main() {
app := cli.NewApp()
app.Name = color.GreenString("chasm")
app.Usage = color.GreenString("A secret-sharing based secure cloud backup solution.")
app.EnableBashCompletion = true
app.Version = "0.1"
usr, _ := user.Current()
defaultRoot := path.Join(usr.HomeDir, "Chasm")
chasmRoot = defaultRoot
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "root, r",
Value: defaultRoot,
Usage: "Destination of the Chasm secure folder.",
Destination: &chasmRoot,
},
}
app.Commands = []cli.Command{
{
Name: "start",
Aliases: nil,
Usage: "Start running chasm.",
Action: startChasm,
},
{
Name: "status",
Aliases: nil,
Usage: "Prints out the current Chasm setup.",
Action: statusChasm,
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a new cloud store to chasm.",
Subcommands: []cli.Command{
{
Name: "folder",
Usage: "add folder",
Action: addFolder,
},
{
Name: "dropbox",
Usage: "add dropbox",
Action: addDropbox,
},
{
Name: "drive",
Usage: "add google drive",
Action: addDrive,
},
},
},
{
Name: "restore",
Aliases: nil,
Usage: "Restores chasm after repeating setup.",
Action: restoreChasm,
},
{
Name: "remove",
Aliases: nil,
Usage: "Removes a cloud store.",
Action: removeChasm,
},
{
Name: "clean",
Aliases: nil,
Usage: "Deletes all shares in cloud stores",
Action: cleanChasm,
},
{
Name: "sync",
Aliases: nil,
Usage: "Clean cloud stores, sync all items in Chasm folder by secret-sharing.",
Action: syncChasm,
},
}
app.Run(os.Args)
}