/
root.go
395 lines (304 loc) · 11.1 KB
/
root.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package cmd
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
//"strconv"
"github.com/Fancman/BackupSoftware/database"
helper "github.com/Fancman/BackupSoftware/helpers"
"github.com/spf13/cobra"
)
var (
// Used for flags.
db = &database.SQLite{}
//source_path string
source_paths []string
backup_paths []string
source_ids []int64
archive_names []string
archive_name string
listBackupsCmd = &cobra.Command{
Use: "list-backups",
Short: "List stored backup records",
RunE: func(cmd *cobra.Command, args []string) error {
ListBackups()
/*backups := list_backups(conn)
if len(backups) > 0 {
for _, b := range backups {
fmt.Printf("[id]: %b, [source]: %s, [destinations]: %v", b.id, b.source, b.destinations)
}
}*/
return nil
},
}
listDrivesCmd = &cobra.Command{
Use: "list-drives",
Short: "List available drives.",
RunE: func(cmd *cobra.Command, args []string) error {
/*if len(args) == 0 {
return errors.New("You have to enter target of action.")
}*/
ListDrives()
return nil
},
}
listArchivesCmd = &cobra.Command{
Use: "list-archives",
Short: "List all archives.",
RunE: func(cmd *cobra.Command, args []string) error {
ListArchives()
return nil
},
}
ClearAllTablesCmd = &cobra.Command{
Use: "clear-tables",
Short: "Deletes all records from tables.",
RunE: func(cmd *cobra.Command, args []string) error {
db.ClearAllTables()
return nil
},
}
addDriveCmd = &cobra.Command{
Use: "add-drive [drive letter] -n [Custom drive name]",
Short: "Adds drive to db with optional custom name and creates .drive file",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("you have to enter exactly one drive identification")
}
if (args[0] < "a" || args[0] > "z") && (args[0] < "A" || args[0] > "Z") {
return errors.New("typed argument is not an alphabetic letter")
}
drive_name, err := cmd.Flags().GetString("drive-name")
if err != nil {
return err
}
AddDrive(string(args[0]), drive_name)
return nil
},
}
startBackupCmd = &cobra.Command{
Use: "start-backup -s [source ids] -a [archive names]",
Short: "Start backup for record in db by its ids",
RunE: func(cmd *cobra.Command, args []string) error {
if len(source_ids) == 0 && len(archive_names) == 0 {
return errors.New("requires atleast one source id or archive name")
}
BackupFileDir(source_ids, archive_names)
//backup := find_backup(db, i)
//start_backup(backup.id, backup.source, backup.destinations)
return nil
},
}
startRestoreCmd = &cobra.Command{
Use: "start-restore -s [source ids] | -a [archive names] -b [backup paths]",
Short: "Start restore from record in db by its ids or archive names, optional backup paths (extract only this archive)",
RunE: func(cmd *cobra.Command, args []string) error {
if len(source_ids) == 0 && len(archive_names) == 0 {
return errors.New("requires atleast one source id or archive name")
}
RestoreFileDir(source_ids, archive_names, backup_paths)
//backup := find_backup(db, i)
//start_restore(backup.id, backup.source, backup.destinations)
return nil
},
}
/*createBackupCmd = &cobra.Command{
Use: "create-backup -s [source] -d [destination drive letter] -p [path] | optional: -ksuid [drive ksuid]",
Short: "Create backup record by enetring drive letter or ksuid -s [source] -d [destination drive letter] -p [path] | optional: -ksuid [drive ksuid]",
RunE: func(cmd *cobra.Command, args []string) error {
//fmt.Println(source, dest_drive_ksuid, dest_path)
//insert_backups_db(source, dest_drive_ksuid, dest_drive_letter, dest_path)
return nil
},
}*/
createBackupCmd = &cobra.Command{
Use: "create-backup -s [source paths] -d [destination paths] -a [archive name]",
Short: "Create backup record from source and destination paths. Archive name is optional.",
RunE: func(cmd *cobra.Command, args []string) error {
//fmt.Println(source_paths)
if len(source_paths) == 0 {
return errors.New("requires atleast one source path")
}
if len(backup_paths) == 0 {
return errors.New("requires atleast one destination path")
}
var backup_drives []string
for _, backup_path := range backup_paths {
backup_letter := strings.ReplaceAll(filepath.VolumeName(backup_path), ":", "")
if helper.FindElm(backup_drives, backup_letter) {
return errors.New("destinations have to be on different drives")
}
backup_drives = append(backup_drives, backup_letter)
}
err := CreateSourceBackup(source_paths, backup_paths, archive_name)
if err != nil {
fmt.Println(err)
}
//CreateSourceBackup("C:/Users/tomas/Pictures/Backgrounds", "E:/backup", "test.7z")
//fmt.Println(source, dest_drive_ksuid, dest_path)
//insert_backups_db(source, dest_drive_ksuid, dest_drive_letter, dest_path)
return nil
},
}
removeSourceCmd = &cobra.Command{
Use: "remove-source -s [source id]",
Short: "Remove source by source ids",
RunE: func(cmd *cobra.Command, args []string) error {
source_id, err := cmd.Flags().GetInt64("source-id")
if err != nil {
return err
}
if source_id == 0 {
return errors.New("atleast one source id has to be passed")
}
RemoveSource(source_id)
return nil
},
}
loadDBFromDriveCmd = &cobra.Command{
Use: "load-db -d [drive letter]",
Short: "Load database from drive",
RunE: func(cmd *cobra.Command, args []string) error {
drive_letter, err := cmd.Flags().GetString("drive-letter")
if err != nil {
return err
}
if (drive_letter < "a" || drive_letter > "z") && (drive_letter < "A" || drive_letter > "Z") {
return errors.New("typed drive letter is not an alphabetic letter")
}
err = LoadDBFromDrive(drive_letter)
if err != nil {
return err
}
return nil
},
}
removeDestinationCmd = &cobra.Command{
Use: "remove-backup -i [archive id] -d [drive ksuid] | -p [path to destination] | -a [archive name] | -l [drive letter]",
Short: "Remove backup records by archive_id and drive_ksuid, path to destination, archive name or drive letter",
RunE: func(cmd *cobra.Command, args []string) error {
archive_id, err_1 := cmd.Flags().GetInt64("archive-id")
drive_ksuid, err_2 := cmd.Flags().GetString("drive-ksuid")
if err_1 != nil {
return err_1
}
if err_2 != nil {
return err_2
}
if archive_id != 0 && drive_ksuid != "" {
RemoveDestination(archive_id, drive_ksuid)
}
destination_path, err := cmd.Flags().GetString("dest-path")
if err != nil {
return err
}
if destination_path != "" {
RemoveDestinationByPath(destination_path)
}
drive_letter, err := cmd.Flags().GetString("drive-letter")
if err != nil {
return err
}
if drive_letter != "" {
RemoveDestinationByDrive(drive_letter)
}
archive_name, err := cmd.Flags().GetString("archive-name")
if err != nil {
return err
}
if archive_name != "" {
RemoveDestinationByArchive(archive_name)
}
return nil
},
}
addSourceCmd = &cobra.Command{
Use: "add-source -s [source path] -a [archive name]",
Short: "Add source to archive name",
RunE: func(cmd *cobra.Command, args []string) error {
if len(source_paths) == 0 {
return errors.New("requires atleast one source path")
}
if archive_name == "" {
return errors.New("requires archive name")
}
return nil
},
}
//insert_backups_db(source string, dest_drive_ksuid string, path string)
rootCmd = &cobra.Command{
Use: "Backupsoft",
Short: "Simple 7z backup utility",
Long: `Backupsoft is a distributed backup utility using 7z as its tool for archiving.`,
}
)
// Execute executes the root command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func TestRootCmd() *cobra.Command {
return &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Fprintf(cmd.OutOrStdout(), args[0])
return nil
},
}
}
func init() {
//RemoveDestinationByArchive("crypto-images.7z")
//RemoveSource(19)
//RestoreFileDir(source_ids, []string{"test-archiv-epic-installer-a-obrazky.7z"}, backup_paths)
//BackupFileDir([]int64{18, 19, 20})
BackupDatabase()
err := db.Fixtures()
if err != nil {
fmt.Println("Database tables couldnt be created.")
}
rootCmd.AddCommand(listDrivesCmd)
rootCmd.AddCommand(listBackupsCmd)
rootCmd.AddCommand(addDriveCmd)
rootCmd.AddCommand(startBackupCmd)
rootCmd.AddCommand(createBackupCmd)
rootCmd.AddCommand(startRestoreCmd)
rootCmd.AddCommand(removeSourceCmd)
rootCmd.AddCommand(removeDestinationCmd)
rootCmd.AddCommand(ClearAllTablesCmd)
rootCmd.AddCommand(loadDBFromDriveCmd)
rootCmd.AddCommand(listArchivesCmd)
addDriveCmd.Flags().StringP("drive-name", "n", "", "Drive name")
removeSourceCmd.Flags().Int64P("source-id", "s", 0, "Source ID")
removeDestinationCmd.Flags().Int64P("archive-id", "i", 0, "Archive ID")
removeDestinationCmd.Flags().StringP("drive-ksuid", "d", "", "Drive Ksuid")
removeDestinationCmd.Flags().StringP("dest-path", "p", "", "Destination path")
removeDestinationCmd.Flags().StringP("archive-name", "a", "", "Archive name")
removeDestinationCmd.Flags().StringP("drive-letter", "l", "", "Drive letter")
//rootCmd.AddCommand(createBackupCmdTest)
//createBackupCmd.Flags().StringSlice("source", source_paths, "sources paths")
createBackupCmd.Flags().StringArrayVarP(&source_paths, "source", "s", make([]string, 0), "sources paths")
createBackupCmd.Flags().StringArrayVarP(&backup_paths, "destination", "d", make([]string, 0), "destination path")
createBackupCmd.Flags().StringVarP(&archive_name, "archive", "a", "", "archive name")
createBackupCmd.MarkFlagRequired("source")
createBackupCmd.MarkFlagRequired("destination")
startBackupCmd.Flags().Int64SliceVarP(&source_ids, "source", "s", make([]int64, 0), "source ids")
startBackupCmd.Flags().StringArrayVarP(&archive_names, "archive", "a", make([]string, 0), "archive name")
//p *[]int64, name, shorthand string, value []int64, usage string
startRestoreCmd.Flags().Int64SliceVarP(&source_ids, "source", "s", make([]int64, 0), "source ids")
startRestoreCmd.Flags().StringArrayVarP(&archive_names, "archive", "a", make([]string, 0), "archive name")
startRestoreCmd.Flags().StringArrayVarP(&backup_paths, "backup", "b", make([]string, 0), "backup paths")
addSourceCmd.Flags().StringArrayVarP(&source_paths, "source", "s", make([]string, 0), "sources paths")
addSourceCmd.Flags().StringVarP(&archive_name, "archive", "a", "", "archive name")
loadDBFromDriveCmd.Flags().StringP("drive-letter", "l", "", "Drive letter")
//RestoreFileDir(10)
/*createBackupCmd.Flags().StringVarP(&source, "source", "s", "", "source path")
createBackupCmd.Flags().StringVarP(&dest_drive_ksuid, "drive_ksuid", "k", "", "destination drive ksuid")
createBackupCmd.Flags().StringVarP(&dest_drive_letter, "destination drive letter", "d", "", "destination drive letter")
createBackupCmd.Flags().StringVarP(&dest_path, "additional path", "p", "", "additional path")*/
//singleCmd.Flags().StringVarP(&options.File, "file", "f", "", "file containing urls. use - for stdin")
//rootCmd.AddCommand(rootCmd)
}