-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconfigurator.go
More file actions
96 lines (82 loc) · 1.89 KB
/
configurator.go
File metadata and controls
96 lines (82 loc) · 1.89 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
)
const Version = "0.0.3"
var port = flag.String("p", "9000", "port to listen on")
var checkCmd = flag.String("c", "", "config check command. FILE set in env")
var reloadCmd = flag.String("r", "", "reload command")
var showVersion = flag.Bool("v", false, "prints current configurator version")
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func marshal(obj interface{}) []byte {
bytes, err := json.MarshalIndent(obj, "", " ")
if err != nil {
log.Println("marshal:", err)
}
return bytes
}
func unmarshal(input io.ReadCloser, obj interface{}) error {
body, err := ioutil.ReadAll(input)
if err != nil {
return err
}
err = json.Unmarshal(body, obj)
if err != nil {
return err
}
return nil
}
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %v [options] <configstore-uri> <transformer> <target-file>\n\n", os.Args[0])
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if *showVersion {
fmt.Println(Version)
os.Exit(0)
}
if flag.NArg() < 3 {
flag.Usage()
os.Exit(64)
}
uri, err := url.Parse(flag.Arg(0))
assert(err)
factory := map[string]func(*url.URL) (ConfigStore, error){
"consul": NewConsulStore,
"file": NewFileStore,
}[uri.Scheme]
if factory == nil {
log.Fatal("Unrecognized config store backend: ", uri.Scheme)
}
store, err := factory(uri)
assert(err)
transformer := flag.Arg(1)
target := flag.Arg(2)
config, err := NewConfig(store, target, transformer, *reloadCmd, *checkCmd)
assert(err)
log.Printf("Pulling and validating from %s...\n", flag.Arg(0))
err = config.Update()
if e, ok := err.(*ExecError); ok {
fmt.Printf("!! Initial pull from config store resulted in validation error.\n")
fmt.Printf("!! Output of '%s':\n", *checkCmd)
fmt.Println(e.Output)
os.Exit(3)
} else {
assert(err)
}
runHttp(config)
}