2020-02-16 15:26:55 +00:00
|
|
|
package connector
|
|
|
|
|
|
|
|
import (
|
2020-02-16 16:53:31 +00:00
|
|
|
"fmt"
|
2020-02-16 15:26:55 +00:00
|
|
|
"strconv"
|
2020-02-16 16:53:31 +00:00
|
|
|
"strings"
|
2020-02-16 15:26:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Configuration map[string]string
|
|
|
|
|
2020-02-16 16:53:31 +00:00
|
|
|
func (c Configuration) GetString(k string, deflt ...string) (string, error) {
|
2020-02-16 15:26:55 +00:00
|
|
|
if ss, ok := c[k]; ok {
|
2020-02-16 16:53:31 +00:00
|
|
|
return ss, nil
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|
2020-02-16 16:53:31 +00:00
|
|
|
if len(deflt) > 0 {
|
|
|
|
return deflt[0], nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("Missing configuration key: %s", k)
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 16:53:31 +00:00
|
|
|
func (c Configuration) GetInt(k string, deflt ...int) (int, error) {
|
2020-02-16 15:26:55 +00:00
|
|
|
if ss, ok := c[k]; ok {
|
|
|
|
return strconv.Atoi(ss)
|
|
|
|
}
|
2020-02-16 16:53:31 +00:00
|
|
|
if len(deflt) > 0 {
|
|
|
|
return deflt[0], nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("Missing configuration key: %s", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Configuration) GetBool(k string, deflt ...bool) (bool, error) {
|
|
|
|
if ss, ok := c[k]; ok {
|
|
|
|
if strings.EqualFold(ss, "true") {
|
|
|
|
return true, nil
|
|
|
|
} else if strings.EqualFold(ss, "false") {
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return false, fmt.Errorf("Invalid value: %s", ss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(deflt) > 0 {
|
|
|
|
return deflt[0], nil
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("Missing configuration key: %s", k)
|
2020-02-16 15:26:55 +00:00
|
|
|
}
|
2020-02-26 21:49:27 +00:00
|
|
|
|
|
|
|
// ----
|
|
|
|
|
2020-02-28 09:18:47 +00:00
|
|
|
type Protocol struct {
|
|
|
|
NewConnector func() Connector
|
|
|
|
Schema ConfigSchema
|
|
|
|
}
|
|
|
|
|
2020-02-26 21:49:27 +00:00
|
|
|
type ConfigSchema []*ConfigEntry
|
|
|
|
|
|
|
|
type ConfigEntry struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Default string
|
|
|
|
FixedValue string
|
|
|
|
Required bool
|
|
|
|
IsPassword bool
|
|
|
|
IsNumeric bool
|
|
|
|
IsBoolean bool
|
|
|
|
}
|
|
|
|
|
2020-02-28 09:18:47 +00:00
|
|
|
var Protocols = map[string]Protocol{}
|
2020-02-26 21:49:27 +00:00
|
|
|
|
2020-02-28 09:18:47 +00:00
|
|
|
func Register(name string, protocol Protocol) {
|
|
|
|
Protocols[name] = protocol
|
2020-02-26 21:49:27 +00:00
|
|
|
}
|