-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (83 loc) · 2.01 KB
/
main.go
File metadata and controls
104 lines (83 loc) · 2.01 KB
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 mysql
import (
"context"
"database/sql"
"fmt"
"math"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/scorify/schema"
)
type Schema struct {
Server string `key:"target"`
Port int `key:"port" default:"3306"`
Username string `key:"username"`
Password string `key:"password"`
Database string `key:"database"`
Query string `key:"query"`
}
func Validate(config string) error {
conf := Schema{}
err := schema.Unmarshal([]byte(config), &conf)
if err != nil {
return err
}
if conf.Server == "" {
return fmt.Errorf("server is required; got %q", conf.Server)
}
if conf.Port <= 0 || conf.Port > 65535 {
return fmt.Errorf("port is invalid; got %d", conf.Port)
}
if conf.Username == "" {
return fmt.Errorf("username is required; got %q", conf.Username)
}
if conf.Password == "" {
return fmt.Errorf("password is required; got %q", conf.Password)
}
if conf.Database == "" {
return fmt.Errorf("database is required; got %q", conf.Database)
}
return nil
}
func Run(ctx context.Context, config string) error {
conf := Schema{}
err := schema.Unmarshal([]byte(config), &conf)
if err != nil {
return err
}
deadline, ok := ctx.Deadline()
if !ok {
return fmt.Errorf("context deadline is not set")
}
connStr := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?timeout=%ds",
conf.Username,
conf.Password,
conf.Server,
conf.Port,
conf.Database,
int(math.Floor(time.Until(deadline).Seconds())),
)
conn, err := sql.Open("mysql", connStr)
if err != nil {
return fmt.Errorf("failed to connect to mysql server: %w", err)
}
defer conn.Close()
conn.SetMaxIdleConns(-1)
conn.SetMaxOpenConns(1)
err = conn.PingContext(ctx)
if err != nil {
return fmt.Errorf("failed to ping mysql server: %w", err)
}
if conf.Query != "" {
rows, err := conn.QueryContext(ctx, conf.Query)
if err != nil {
return fmt.Errorf("failed to execute query: %w", err)
}
defer rows.Close()
if !rows.Next() {
return fmt.Errorf("no rows returned from query: %q", conf.Query)
}
}
return nil
}