/
root.go
116 lines (97 loc) · 3.13 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
package cmd
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-co-op/gocron"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/liweiyi88/onedump/config"
"github.com/liweiyi88/onedump/handler"
"github.com/liweiyi88/onedump/storage/s3"
)
var file, s3Bucket, s3Region, s3AccessKeyId, s3SecretAccessKey, cron string
var rootCmd = &cobra.Command{
Use: "onedump",
Short: "Dump database content from different sources to different destinations with a yaml config file.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
_, stop := context.WithCancel(context.Background())
appSignal := make(chan os.Signal, 3)
signal.Notify(appSignal, os.Interrupt, syscall.SIGTERM)
defer func() {
stop()
}()
content, err := getConfigContent()
if err != nil {
return fmt.Errorf("failed to read job file from %s, error: %v", file, err)
}
var oneDump config.Dump
err = yaml.Unmarshal(content, &oneDump)
if err != nil {
return fmt.Errorf("failed to read job content from %s, error: %v", file, err)
}
err = oneDump.Validate()
if err != nil {
return fmt.Errorf("invalid job configuration, error: %v", err)
}
numberOfJobs := len(oneDump.Jobs)
if numberOfJobs == 0 {
return fmt.Errorf("no job is defined in the file %s", file)
}
if cron == "" {
return handler.NewDumpHandler(&oneDump).Do()
} else {
d, err := time.ParseDuration(cron)
if err != nil {
return fmt.Errorf("invalid job's interval time duration, error: %v", err)
}
scheduler := gocron.NewScheduler(time.UTC)
job, err := scheduler.Every(d).Do(handler.NewDumpHandler(&oneDump).Do)
if err != nil {
return fmt.Errorf("failed to specify the job func, error: %v", err)
}
var jobErr error
job.RegisterEventListeners(gocron.WhenJobReturnsError(func(jobName string, err error) {
go func() {
jobErr = err
// Stop the scheduler if job has an error.
scheduler.Stop()
}()
}))
go func() {
<-appSignal
stop()
// graceful shutdown
scheduler.Stop()
}()
scheduler.StartBlocking()
return jobErr
}
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func getConfigContent() ([]byte, error) {
if s3Bucket != "" {
s3Client := s3.NewS3(s3Bucket, file, s3Region, s3AccessKeyId, s3SecretAccessKey)
return s3Client.GetContent()
} else {
return os.ReadFile(file)
}
}
func init() {
rootCmd.Flags().StringVarP(&file, "file", "f", "", "jobs yaml file path.")
rootCmd.MarkFlagRequired("file")
rootCmd.Flags().StringVarP(&cron, "cron", "c", "", "run onedump with cron mode by passing cron experssions. e.g. --cron '1h' (optional)")
rootCmd.Flags().StringVarP(&s3Bucket, "s3-bucket", "b", "", "read config file from a s3 bucket (optional)")
rootCmd.Flags().StringVarP(&s3Region, "aws-region", "r", "", "the aws region to read the config file (optional)")
rootCmd.Flags().StringVarP(&s3AccessKeyId, "aws-key", "k", "", "aws access key id to overwrite the default one. (optional)")
rootCmd.Flags().StringVarP(&s3SecretAccessKey, "aws-secret", "s", "", "aws secret access key to overwrite the default one. (optional)")
}