This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
112 lines (99 loc) · 2.67 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
105
106
107
108
109
110
111
112
package main
import (
"database/sql"
"fmt"
"log"
"strings"
_ "github.com/go-sql-driver/mysql"
"github.com/timtosi/fishfinger"
)
// -----------------------------------------------------------------------------
// printRes is an helper function that prints the result of a query.
func printRes(rows *sql.Rows) error {
columns, err := rows.Columns()
if err != nil {
return err
}
values := make([]sql.RawBytes, len(columns))
scanArgs := make([]interface{}, len(values))
for i := range values {
scanArgs[i] = &values[i]
}
for rows.Next() {
if err := rows.Scan(scanArgs...); err != nil {
return err
}
for i, col := range values {
fmt.Printf("%s : %s\n", columns[i], string(col))
}
}
return nil
}
// -----------------------------------------------------------------------------
func main() {
// First of all, let's create a new Compose file handler by using the
// `fishfinger.NewCompose` function that takes the path to your Compose file
// as an argument.
c, err := fishfinger.NewCompose("./docker-compose.yaml")
if err != nil {
log.Fatal(err)
}
// Now, time to start the MySQL service with the `Compose.StartBackoff`
// function. This function takes two arguments: the Backoff function used
// and a list of service name that will be started by this function call.
// The Backoff function used here is the one provided by default by the
// Fishfinger project but you are expected to provide another one that suits
// your need.
if err := c.StartBackoff(fishfinger.SocketBackoff, "datastore:9090/tcp"); err != nil {
log.Fatal(err)
}
// It's only keep trying to connect to a specific port exposed by the
// container. The fact is the function will not find any remote listener
// until all data is correctly loaded. In this way, you are assured
// everything is ready to be processed by the rest of your program.
addr, err := c.Port("datastore", "3306/tcp")
if err != nil {
log.Fatal(err)
}
user, err := c.Env("datastore", "MYSQL_USER")
if err != nil {
log.Fatal(err)
}
pass, err := c.Env("datastore", "MYSQL_PASS")
if err != nil {
log.Fatal(err)
}
proto, err := c.Env("datastore", "MYSQL_PROTO")
if err != nil {
log.Fatal(err)
}
dbName, err := c.Env("datastore", "MYSQL_DB_NAME")
if err != nil {
log.Fatal(err)
}
db, err := sql.Open(
"mysql",
fmt.Sprintf(
"%s:%s@%s(%s:%s)/%s",
user,
pass,
proto,
strings.Split(addr, ":")[0],
strings.Split(addr, ":")[1],
dbName,
),
)
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("SELECT * FROM sinking_ships")
if err != nil {
log.Fatal(err)
}
if err := printRes(rows); err != nil {
log.Fatal(err)
}
if err := c.Stop(); err != nil {
log.Fatal(err)
}
}