aboutsummaryrefslogtreecommitdiff
path: root/connector/config.go
blob: 0bf3614773e76a599a562a8ebcae99e38a46c8da (plain) (blame)
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
package connector

import (
	"fmt"
	"strconv"
	"strings"
)

type Configuration map[string]string

func (c Configuration) GetString(k string, deflt ...string) (string, error) {
	if ss, ok := c[k]; ok {
		return ss, nil
	}
	if len(deflt) > 0 {
		return deflt[0], nil
	}
	return "", fmt.Errorf("Missing configuration key: %s", k)
}

func (c Configuration) GetInt(k string, deflt ...int) (int, error) {
	if ss, ok := c[k]; ok {
		return strconv.Atoi(ss)
	}
	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)
}

// ----

type Protocol struct {
	NewConnector func() Connector
	Schema       ConfigSchema
}

type ConfigSchema []*ConfigEntry

type ConfigEntry struct {
	Name        string
	Description string
	Default     string
	FixedValue  string
	Required    bool
	IsPassword  bool
	IsNumeric   bool
	IsBoolean   bool
}

var Protocols = map[string]Protocol{}

func Register(name string, protocol Protocol) {
	Protocols[name] = protocol
}