2016-08-28 03:59:17 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2016-10-25 15:30:14 +00:00
|
|
|
"log"
|
|
|
|
|
2016-09-26 18:48:55 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
2016-08-28 03:59:17 +00:00
|
|
|
)
|
|
|
|
|
2016-10-25 15:30:14 +00:00
|
|
|
const defaultMaxAttempts int = 15
|
2016-08-28 03:59:17 +00:00
|
|
|
|
|
|
|
// DBOption is used to pass optional arguments to a database connection
|
|
|
|
type DBOption func(o *dbOptions) error
|
|
|
|
|
|
|
|
type dbOptions struct {
|
|
|
|
// maxAttempts configures the number of retries to connect to the DB
|
2016-09-14 16:11:06 +00:00
|
|
|
maxAttempts int
|
|
|
|
db kolide.Datastore
|
|
|
|
debug bool // gorm debug
|
2016-10-25 15:30:14 +00:00
|
|
|
logger *log.Logger
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logger adds a logger to the datastore
|
2016-10-25 15:30:14 +00:00
|
|
|
func Logger(l *log.Logger) DBOption {
|
2016-08-28 03:59:17 +00:00
|
|
|
return func(o *dbOptions) error {
|
|
|
|
o.logger = l
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LimitAttempts sets a the number of attempts
|
|
|
|
// to try establishing a connection to the database backend
|
|
|
|
// the default value is 15 attempts
|
|
|
|
func LimitAttempts(attempts int) DBOption {
|
|
|
|
return func(o *dbOptions) error {
|
|
|
|
o.maxAttempts = attempts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug sets the GORM debug level
|
|
|
|
func Debug() DBOption {
|
|
|
|
return func(o *dbOptions) error {
|
|
|
|
o.debug = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// datastore allows you to pass your own datastore
|
|
|
|
// this option can be used to pass a specific testing implementation
|
|
|
|
func datastore(db kolide.Datastore) DBOption {
|
|
|
|
return func(o *dbOptions) error {
|
|
|
|
o.db = db
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|