forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.go
150 lines (136 loc) · 5.1 KB
/
bootstrap.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2014 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, sub to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package service
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
//------------------------------------------------------------------------------
var (
version string
dateBuilt string
showVersion *bool
showConfigJSON *bool
showConfigYAML *bool
configPath *string
)
func init() {
showVersion = flag.Bool("version", false, "Display version info, then exit")
showConfigJSON = flag.Bool("print-json", false, "Print loaded configuration as JSON, then exit")
showConfigYAML = flag.Bool("print-yaml", false, "Print loaded configuration as YAML, then exit")
configPath = flag.String("c", "", "Path to a configuration file")
}
//------------------------------------------------------------------------------
func readConfig(path string, config interface{}) error {
configBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
ext := filepath.Ext(path)
if ".js" == ext || ".json" == ext {
if err = json.Unmarshal(configBytes, config); err != nil {
return err
}
} else if ".yml" == ext || ".yaml" == ext {
if err = yaml.Unmarshal(configBytes, config); err != nil {
return err
}
} else {
return fmt.Errorf("config file extension not recognised: %v", path)
}
return nil
}
// Bootstrap bootstraps the configuration loading, parsing and reporting for a
// service through cmd flags. The argument configPtr should be a pointer to a
// serializable configuration object with all default values.
//
// configPtr should be a pointer to a config struct, which contains default
// values and should be populated with a users config values if applicable. For
// an example look at the stats and logger files.
//
// defaultConfigPaths should contain any known standard configuration paths, if
// the user neglects to specify a config then bootstrap will iterate these paths
// and read the first one that exists, if any.
//
// Bootstrap allows a user to do the following:
// - Print version and build info and exit
// - Load an optional configuration file (supports JSON, YAML)
// - Print the config file (supports JSON, YAML) and exit
//
// NOTE: The user may request a version and build time stamp, in which case
// Bootstrap will print the values of util.Version and util.DateBuilt. To
// populate those values you must run go build with the following:
//
// -ldflags "-X github.com/jeffail/benthos/lib/util/service.version $(VERSION) \
// -X github.com/jeffail/benthos/lib/util/service.dateBuilt $(DATE)"
//
// Returns a flag indicating whether the service should continue or not.
func Bootstrap(configPtr interface{}, defaultConfigPaths ...string) bool {
// Ensure that cmd flags are parsed.
if !flag.Parsed() {
flag.Parse()
}
// If the user wants the version we print it.
if *showVersion {
fmt.Printf("Version: %v\nDate: %v\n", version, dateBuilt)
return false
}
if len(*configPath) > 0 {
if err := readConfig(*configPath, configPtr); err != nil {
fmt.Fprintf(os.Stderr, "Configuration file read error: %v\n", err)
return false
}
} else {
// Iterate default config paths
for _, path := range defaultConfigPaths {
if _, err := os.Stat(path); err == nil {
fmt.Fprintf(os.Stderr, "Config file not specified, reading from %v\n", path)
if err = readConfig(path, configPtr); err != nil {
fmt.Fprintf(os.Stderr, "Configuration file read error: %v\n", err)
return false
}
break
}
}
}
// If the user wants the configuration to be printed we do so and then exit.
if *showConfigJSON {
if configJSON, err := json.MarshalIndent(configPtr, "", "\t"); err == nil {
fmt.Println(string(configJSON))
} else {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Configuration marshal error: %v", err))
}
return false
} else if *showConfigYAML {
if configYAML, err := yaml.Marshal(configPtr); err == nil {
fmt.Println(string(configYAML))
} else {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Configuration marshal error: %v", err))
}
return false
}
return true
}
//------------------------------------------------------------------------------