-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (98 loc) · 1.97 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
package main
import (
"flag"
"os"
"strings"
logger "github.com/sirupsen/logrus"
"github.com/superwomany/keeper/services"
)
const (
GET_LAST_TASK_ID = "last-task-id"
RUN_BACKUP = "run-backup"
TASK_PROGRESS = "task-progress"
DOWNLOAD_LOCAL = "download-local"
BACKUP_SAVE = "back-save"
)
func main() {
mode := flag.String("mode", BACKUP_SAVE, "the name of the task")
taskId := flag.String("id", "", "task id")
fl := flag.String("link", "", "file link")
flag.Parse()
c := services.New()
switch *mode {
case BACKUP_SAVE:
err := BackupSave(c)
if err != nil {
logger.Error(err)
panic(err)
}
case GET_LAST_TASK_ID:
id, err := c.GetLastTaskId()
if err != nil {
logger.Error(err)
panic(err)
}
logger.Info(*id)
case RUN_BACKUP:
id, err := c.RunBackup()
if err != nil {
logger.Error(err)
panic(err)
}
logger.Info(*id)
case TASK_PROGRESS:
if len(*taskId) == 0 {
panic("task id is required")
}
c.EnsureTaskDone(*taskId)
case DOWNLOAD_LOCAL:
if len(*fl) == 0 {
panic("file link is required")
}
fileId := strings.Split(*fl, "=")[1]
logger.Info(fileId)
out, err := os.Create(fileId + ".zip")
if err != nil {
logger.Error(err)
}
defer out.Close()
c.Download(*fl, out)
default:
logger.Fatal("unknown mode")
}
}
func BackupSave(c services.ServiceIntf) error {
id, err := c.RunBackup()
if err != nil {
id, err = c.GetLastTaskId()
if err != nil {
return err
}
}
dl := c.EnsureTaskDone(*id)
if dl != nil {
fileId := strings.Split(*dl, "=")[1]
out, err := os.Create(fileId + ".zip")
if err != nil {
logger.Error(err)
return err
}
defer out.Close()
c.Download(*dl, out)
if os.Getenv("BACKUP_BUCKET") != "" {
input, err := os.Open(fileId + ".zip")
if err != nil {
logger.Error(err)
return err
}
defer input.Close()
s3k := "jira/" + fileId + ".zip"
err = services.UploadS3(s3k, input)
if err != nil {
logger.Error(err)
return err
}
}
}
return nil
}