-
Notifications
You must be signed in to change notification settings - Fork 0
/
envtrack.go
86 lines (73 loc) · 1.73 KB
/
envtrack.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
package main
import (
"flag"
"log"
"net/url"
)
var backendFlag = flag.String("backend", "file", "Backend to use. Either 'file' or 'git'.")
var backendPath = flag.String("path", "config", "Path to the file to store the saved key-value pairs.")
var scripting = flag.Bool("script", false, "Output script based on file pointed to by path.")
type KeyListener interface {
GetKeys() ([]byte, error)
Script(srcPath string) error
}
type Backend interface {
Store(data []byte) error
}
func main() {
flag.Parse()
uri, err := url.Parse(flag.Arg(0))
if err != nil {
log.Fatal("envtrack: ", err)
}
listener := NewKeyListener(uri, *scripting)
if *scripting {
err := listener.Script(*backendPath)
if err != nil {
log.Fatal(err)
}
} else {
backend := NewBackend(*backendPath)
// To allow requests to be received while we're writing to a file.
pipe := make(chan []byte, 100)
go func() {
for {
data, err := listener.GetKeys()
if err != nil {
log.Println(err)
continue
}
if data != nil {
pipe <- data
}
}
}()
for data := range pipe {
err := backend.Store(data)
if err != nil {
log.Println(err)
continue
}
}
}
}
func NewKeyListener(uri *url.URL, scripting bool) KeyListener {
factory := map[string]func(*url.URL, bool) KeyListener{
"consul": NewConsulKeyListener,
//"etcd": NewEtcdKeyListener,
}[uri.Scheme]
if factory == nil {
log.Fatal("unrecognized listener: ", uri.Scheme)
}
return factory(uri, scripting)
}
func NewBackend(path string) Backend {
factory := map[string]func(string) Backend{
"file": NewFileBackend,
"git": NewGitBackend,
}[*backendFlag]
if factory == nil {
log.Fatal("unrecognized backend: ", *backendFlag)
}
return factory(path)
}