/
init.go
executable file
·51 lines (45 loc) · 1.09 KB
/
init.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
package cmd
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/tomruk/kopyaship/backup"
"github.com/tomruk/kopyaship/backup/provider"
)
var initCmd = &cobra.Command{
Use: "init",
Run: func(cmd *cobra.Command, args []string) {
include := args
ctx, cancel := context.WithCancel(context.Background())
addExitHandler(cancel)
backups, err := backup.FromConfig(ctx, &config.Backups, cacheDir, debugLog, false, include...)
if err != nil {
errPrintln(err)
exit(exitError)
}
// If certain backups were choosen:
if len(args) >= 1 {
backups = make(backup.Backups)
for _, name := range args {
b, ok := backups[name]
if !ok {
errPrintln(fmt.Errorf("backup with name %s could not be found", name))
exit(exitError)
}
backups[name] = b
}
}
for _, backup := range backups {
restic, ok := backup.Provider.(*provider.Restic)
if !ok {
errPrintln(fmt.Errorf("backup with name %s is not a restic backup", backup.Name))
exit(exitError)
}
err = restic.Init()
if err != nil {
errPrintln(err)
exit(exitError)
}
}
},
}