-
Notifications
You must be signed in to change notification settings - Fork 2
/
state.go
116 lines (100 loc) · 2.96 KB
/
state.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
113
114
115
116
package main
import (
"context"
"fmt"
docker "github.com/docker/docker/client"
"github.com/wdullaer/dd-dns/dns"
"github.com/wdullaer/dd-dns/store"
"go.uber.org/zap"
)
// State is a type that serves as a container for all the state the program
// manages
// It makes the signature of functions which act on all of these easier to read
// and can act as a poor mans named parameters
type State struct {
Config *config
Provider dns.Provider
DockerClient *docker.Client
Store store.Store
Logger *zap.SugaredLogger
}
// NewState returns a fully initialized application State baed on the
// configuration options
func NewState(config *config, logger *zap.SugaredLogger) (*State, error) {
state := &State{
Config: config,
Logger: logger,
}
// Connect to docker daemon
state.Logger.Infow("Connecting to docker")
dockerClient, err := getDockerClient()
if err != nil {
return nil, err
}
state.DockerClient = dockerClient
state.Logger.Infow("Connected to docker")
// Create the Provider
state.Logger.Infow("Connecting to DNS Provider", "provider", config.Provider)
provider, err := getDNSProvider(config, logger)
if err != nil {
return nil, err
}
state.Provider = provider
state.Logger.Infow("Connected to DNS Provider", "provider", config.Provider)
// Create the store
state.Logger.Infow("Connecting to Store", "store", config.Store)
db, err := getStore(config, logger)
if err != nil {
return nil, err
}
state.Store = db
state.Logger.Infow("Connected to Store", "store", state.Config.Store)
return state, nil
}
func getLogger(config *config) (*zap.SugaredLogger, error) {
var logger *zap.Logger
var err error
if config.DebugLogger {
logger, err = zap.NewDevelopment()
} else {
logger, err = zap.NewProduction()
}
if err != nil {
return nil, err
}
return logger.Named("dd-dns").Sugar(), nil
}
func getDockerClient() (*docker.Client, error) {
dockerClient, err := docker.NewClientWithOpts(docker.FromEnv)
if err != nil {
return nil, err
}
dockerPing, err := dockerClient.Ping(context.Background())
if err != nil {
return nil, err
}
dockerClient.NegotiateAPIVersionPing(dockerPing)
return dockerClient, nil
}
func getDNSProvider(config *config, logger *zap.SugaredLogger) (dns.Provider, error) {
switch config.Provider {
case "cloudflare":
return dns.NewCloudflareProvider(config.AccountName, config.AccountSecret, logger)
case "dryrun":
return dns.NewDryrunProvider(logger)
default:
// Since we are eagerly validating the config, this should never happen
return nil, fmt.Errorf("Invalid provider specified: %s", config.Provider)
}
}
func getStore(config *config, logger *zap.SugaredLogger) (store.Store, error) {
switch config.Store {
case "memory":
return store.NewMemoryStore(logger)
case "boltdb":
return store.NewBoltDBStore(logger, config.DataDirectory)
default:
// Since we are eagerly validating the config, this should never happen
return nil, fmt.Errorf("Invalid store specified: %s", config.Store)
}
}