forked from gobackup/gobackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
79 lines (68 loc) 路 1.53 KB
/
base.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
package storage
import (
"fmt"
"github.com/auzty/gobackup/config"
"github.com/auzty/gobackup/logger"
"github.com/spf13/viper"
"path/filepath"
)
// Base storage
type Base struct {
model config.ModelConfig
archivePath string
viper *viper.Viper
keep int
}
// Context storage interface
type Context interface {
open() error
close()
upload(fileKey string) error
delete(fileKey string) error
}
func newBase(model config.ModelConfig, archivePath string) (base Base) {
base = Base{
model: model,
archivePath: archivePath,
viper: model.StoreWith.Viper,
}
if base.viper != nil {
base.keep = base.viper.GetInt("keep")
}
return
}
// Run storage
func Run(model config.ModelConfig, archivePath string) (err error) {
logger.Info("------------- Storage --------------")
newFileKey := filepath.Base(archivePath)
base := newBase(model, archivePath)
var ctx Context
switch model.StoreWith.Type {
case "local":
ctx = &Local{Base: base}
case "ftp":
ctx = &FTP{Base: base}
case "scp":
ctx = &SCP{Base: base}
case "s3":
ctx = &S3{Base: base}
case "oss":
ctx = &OSS{Base: base}
default:
return fmt.Errorf("[%s] storage type has not implement", model.StoreWith.Type)
}
logger.Info("=> Storage | " + model.StoreWith.Type)
err = ctx.open()
if err != nil {
return err
}
defer ctx.close()
err = ctx.upload(newFileKey)
if err != nil {
return err
}
cycler := Cycler{}
cycler.run(model.Name, newFileKey, base.keep, ctx.delete)
logger.Info("------------- Storage --------------\n")
return nil
}