-
Notifications
You must be signed in to change notification settings - Fork 75
/
watcher.go
52 lines (41 loc) · 1.08 KB
/
watcher.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
package watcher
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/schemahero/schemahero/pkg/database/interfaces"
"github.com/schemahero/schemahero/pkg/database/mysql"
"github.com/schemahero/schemahero/pkg/database/postgres"
"github.com/spf13/viper"
)
type Watcher struct {
Viper *viper.Viper
}
func NewWatcher() *Watcher {
return &Watcher{
Viper: viper.GetViper(),
}
}
func (w *Watcher) RunSync() error {
fmt.Printf("connecting to %s\n", w.Viper.GetString("uri"))
var conn interfaces.SchemaHeroDatabaseConnection
if w.Viper.GetString("driver") == "postgres" {
c, err := postgres.Connect(w.Viper.GetString("uri"))
if err != nil {
return errors.Wrap(err, "postgres connect")
}
conn = c
} else if w.Viper.GetString("driver") == "mysql" {
c, err := mysql.Connect(w.Viper.GetString("uri"))
if err != nil {
return errors.Wrap(err, "mysql connect")
}
conn = c
}
for {
if _, err := conn.CheckAlive(w.Viper.GetString("namespace"), w.Viper.GetString("instance")); err != nil {
return errors.Wrap(err, "check alive")
}
time.Sleep(time.Second * 10)
}
}