Skip to content

Commit

Permalink
integrate plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Nov 26, 2023
1 parent 23b23cf commit a17334b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 107 deletions.
38 changes: 21 additions & 17 deletions hook_file.go → plugins/file/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package warp
package main

import (
"fmt"
"io"
"os"
"time"

"github.com/linyows/warp"
)

const (
Expand All @@ -14,17 +16,17 @@ const (
`
)

type HookFile struct {
type File struct {
file io.Writer
}

func (h *HookFile) Name() string {
func (f *File) Name() string {
return "file"
}

func (h *HookFile) writer() (io.Writer, error) {
if h.file != nil {
return h.file, nil
func (f *File) writer() (io.Writer, error) {
if f.file != nil {
return f.file, nil
}

path := os.Getenv("FILE_PATH")
Expand All @@ -33,37 +35,39 @@ func (h *HookFile) writer() (io.Writer, error) {
}

var err error
h.file, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f.file, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, fmt.Errorf("os.OpenFile error: %s\n", err)
}

return h.file, nil
return f.file, nil
}

func (h *HookFile) AfterInit() {
func (f *File) AfterInit() {
}

func (h *HookFile) AfterComm(d *AfterCommData) {
writer, err := h.writer()
func (f *File) AfterComm(d *warp.AfterCommData) {
writer, err := f.writer()
if err != nil {
fmt.Printf("[%s] %s\n", h.Name(), err)
fmt.Printf("[%s] %s\n", f.Name(), err)
return
}

if _, err := fmt.Fprintf(writer, fileCommJson, d.OccurredAt.Format(time.RFC3339), d.ConnID, d.Direction, d.Data); err != nil {
fmt.Printf("[%s] file append error: %s\n", h.Name(), err)
fmt.Printf("[%s] file append error: %s\n", f.Name(), err)
}
}

func (h *HookFile) AfterConn(d *AfterConnData) {
writer, err := h.writer()
func (f *File) AfterConn(d *warp.AfterConnData) {
writer, err := f.writer()
if err != nil {
fmt.Printf("[%s] %s\n", h.Name(), err)
fmt.Printf("[%s] %s\n", f.Name(), err)
return
}

if _, err := fmt.Fprintf(writer, fileConnJson, d.OccurredAt.Format(time.RFC3339), d.ConnID, d.MailFrom, d.MailTo, d.Elapse); err != nil {
fmt.Printf("[%s] file append error: %s\n", h.Name(), err)
fmt.Printf("[%s] file append error: %s\n", f.Name(), err)
}
}

var Hook File //nolint
26 changes: 14 additions & 12 deletions hook_file_test.go → plugins/file/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package warp
package main

import (
"bytes"
Expand All @@ -7,9 +7,11 @@ import (
"strings"
"testing"
"time"

"github.com/linyows/warp"
)

func TestHookFileConst(t *testing.T) {
func TestFileConst(t *testing.T) {
var expect string
var got string

Expand Down Expand Up @@ -49,16 +51,16 @@ func TestHookFileConst(t *testing.T) {
}
}

func TestHookFileName(t *testing.T) {
f := &HookFile{}
func TestFileName(t *testing.T) {
f := &File{}
expect := "file"
got := f.Name()
if got != expect {
t.Errorf("expected %s, got %s", expect, got)
}
}

func TestHookFileWriter(t *testing.T) {
func TestFileWriter(t *testing.T) {
var tests = []struct {
expectFileName string
expectError string
Expand All @@ -85,7 +87,7 @@ func TestHookFileWriter(t *testing.T) {
defer os.Unsetenv(v.envName)
}

f := &HookFile{}
f := &File{}
w, err := f.writer()

if w != nil || v.expectFileName != "" {
Expand All @@ -100,13 +102,13 @@ func TestHookFileWriter(t *testing.T) {
}
}

func TestHookFileAfterComm(t *testing.T) {
func TestFileAfterComm(t *testing.T) {
ti := time.Date(2023, time.August, 16, 14, 48, 0, 0, time.UTC)
buffer := new(bytes.Buffer)
f := &HookFile{
f := &File{
file: buffer,
}
data := &AfterCommData{
data := &warp.AfterCommData{
ConnID: "abcdefg",
OccurredAt: ti,
Data: []byte("hello"),
Expand All @@ -121,13 +123,13 @@ func TestHookFileAfterComm(t *testing.T) {
}
}

func TestHookFileAfterConn(t *testing.T) {
func TestFileAfterConn(t *testing.T) {
ti := time.Date(2023, time.August, 16, 14, 48, 0, 0, time.UTC)
buffer := new(bytes.Buffer)
f := &HookFile{
f := &File{
file: buffer,
}
data := &AfterConnData{
data := &warp.AfterConnData{
ConnID: "abcdefg",
OccurredAt: ti,
MailFrom: []byte("alice@example.local"),
Expand Down
43 changes: 23 additions & 20 deletions hook_mysql.go → plugins/mysql/main.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package warp
package main

import (
"database/sql"
"fmt"
"os"

_ "github.com/go-sql-driver/mysql"
"github.com/linyows/warp"
)

const (
mysqlCommQuery string = "insert into communications (id, connection_id, occurred_at, direction, data) values (?, ?, ?, ?, ?)"
mysqlConnQuery string = "insert into connections (id, occurred_at, mail_from, mail_to, elapse) values (?, ?, ?, ?, ?)"
)

type HookMysql struct {
type Mysql struct {
pool *sql.DB // Database connection pool.
}

func (h *HookMysql) Name() string {
func (m *Mysql) Name() string {
return "mysql"
}

func (h *HookMysql) conn() (*sql.DB, error) {
if h.pool != nil {
return h.pool, nil
func (m *Mysql) conn() (*sql.DB, error) {
if m.pool != nil {
return m.pool, nil
}

dsn := os.Getenv("DSN")
Expand All @@ -32,53 +33,55 @@ func (h *HookMysql) conn() (*sql.DB, error) {
}

var err error
h.pool, err = sql.Open("mysql", dsn)
m.pool, err = sql.Open("mysql", dsn)
if err != nil {
return nil, fmt.Errorf("sql.Open error: %s\n", err)
}

return h.pool, nil
return m.pool, nil
}

func (h *HookMysql) AfterInit() {
func (m *Mysql) AfterInit() {
}

func (h *HookMysql) AfterComm(d *AfterCommData) {
conn, err := h.conn()
func (m *Mysql) AfterComm(d *warp.AfterCommData) {
conn, err := m.conn()
if err != nil {
fmt.Printf("[%s] %s\n", h.Name(), err)
fmt.Printf("[%s] %s\n", m.Name(), err)
return
}

_, err = conn.Exec(
mysqlCommQuery,
GenID().String(),
warp.GenID().String(),
d.ConnID,
d.OccurredAt.Format(TimeFormat),
d.OccurredAt.Format(warp.TimeFormat),
d.Direction,
d.Data,
)
if err != nil {
fmt.Printf("[%s] db exec error: %s\n", h.Name(), err)
fmt.Printf("[%s] db exec error: %s\n", m.Name(), err)
}
}

func (h *HookMysql) AfterConn(d *AfterConnData) {
conn, err := h.conn()
func (m *Mysql) AfterConn(d *warp.AfterConnData) {
conn, err := m.conn()
if err != nil {
fmt.Printf("[%s] %s\n", h.Name(), err)
fmt.Printf("[%s] %s\n", m.Name(), err)
return
}

_, err = conn.Exec(
mysqlConnQuery,
d.ConnID,
d.OccurredAt.Format(TimeFormat),
d.OccurredAt.Format(warp.TimeFormat),
d.MailFrom,
d.MailTo,
d.Elapse,
)
if err != nil {
fmt.Printf("[%s] db exec error: %s\n", h.Name(), err)
fmt.Printf("[%s] db exec error: %s\n", m.Name(), err)
}
}

var Hook Mysql //nolint
29 changes: 15 additions & 14 deletions hook_mysql_test.go → plugins/mysql/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package warp
package main

import (
"database/sql/driver"
Expand All @@ -8,9 +8,10 @@ import (

"github.com/DATA-DOG/go-sqlmock"
_ "github.com/go-sql-driver/mysql"
"github.com/linyows/warp"
)

func TestHookMysqlConst(t *testing.T) {
func TestMysqlConst(t *testing.T) {
var expect string
var got string

Expand All @@ -27,8 +28,8 @@ func TestHookMysqlConst(t *testing.T) {
}
}

func TestHookMysqlName(t *testing.T) {
mysql := &HookMysql{}
func TestMysqlName(t *testing.T) {
mysql := &Mysql{}
var expect string
var got string

Expand All @@ -39,9 +40,9 @@ func TestHookMysqlName(t *testing.T) {
}
}

func TestHookMysqlConn(t *testing.T) {
func TestMysqlConn(t *testing.T) {
expectError := "missing dsn for mysql, please set `DSN`"
mysql := &HookMysql{}
mysql := &Mysql{}
_, err := mysql.conn()

if err != nil && fmt.Sprintf("%s", err) != expectError {
Expand All @@ -56,7 +57,7 @@ func (a AnyID) Match(v driver.Value) bool {
return ok
}

func TestHookMysqlAfterComm(t *testing.T) {
func TestMysqlAfterComm(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
Expand All @@ -68,23 +69,23 @@ func TestHookMysqlAfterComm(t *testing.T) {
mock.ExpectExec("insert into communications").WithArgs(
AnyID{},
"abcdefg",
ti.Format(TimeFormat),
ti.Format(warp.TimeFormat),
"--",
[]byte("hello"),
).WillReturnResult(sqlmock.NewResult(1, 1))

data := &AfterCommData{
data := &warp.AfterCommData{
ConnID: "abcdefg",
OccurredAt: ti,
Data: []byte("hello"),
Direction: "--",
}

mysql := &HookMysql{pool: db}
mysql := &Mysql{pool: db}
mysql.AfterComm(data)
}

func TestHookMysqlAfterConn(t *testing.T) {
func TestMysqlAfterConn(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
Expand All @@ -95,20 +96,20 @@ func TestHookMysqlAfterConn(t *testing.T) {

mock.ExpectExec("insert into connections").WithArgs(
"abcdefg",
ti.Format(TimeFormat),
ti.Format(warp.TimeFormat),
[]byte("alice@example.local"),
[]byte("bob@example.test"),
20,
).WillReturnResult(sqlmock.NewResult(1, 1))

data := &AfterConnData{
data := &warp.AfterConnData{
ConnID: "abcdefg",
OccurredAt: ti,
MailFrom: []byte("alice@example.local"),
MailTo: []byte("bob@example.test"),
Elapse: 20,
}

mysql := &HookMysql{pool: db}
mysql := &Mysql{pool: db}
mysql.AfterConn(data)
}

0 comments on commit a17334b

Please sign in to comment.