forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configtest.go
67 lines (55 loc) · 1.98 KB
/
configtest.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
package command
import (
"flag"
"fmt"
"strings"
"github.com/hashicorp/consul/command/agent"
"github.com/mitchellh/cli"
)
// ConfigTestCommand is a Command implementation that is used to
// verify config files
type ConfigTestCommand struct {
Ui cli.Ui
}
func (c *ConfigTestCommand) Help() string {
helpText := `
Usage: consul configtest [options]
Performs a basic sanity test on Consul configuration files. For each file
or directory given, the configtest command will attempt to parse the
contents just as the "consul agent" command would, and catch any errors.
This is useful to do a test of the configuration only, without actually
starting the agent.
Returns 0 if the configuration is valid, or 1 if there are problems.
Options:
-config-file=foo Path to a JSON file to read configuration from.
This can be specified multiple times.
-config-dir=foo Path to a directory to read configuration files
from. This will read every file ending in ".json"
as configuration in this directory in alphabetical
order.
`
return strings.TrimSpace(helpText)
}
func (c *ConfigTestCommand) Run(args []string) int {
var configFiles []string
cmdFlags := flag.NewFlagSet("configtest", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
cmdFlags.Var((*agent.AppendSliceValue)(&configFiles), "config-file", "json file to read config from")
cmdFlags.Var((*agent.AppendSliceValue)(&configFiles), "config-dir", "directory of json files to read")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
if len(configFiles) <= 0 {
c.Ui.Error("Must specify config using -config-file or -config-dir")
return 1
}
_, err := agent.ReadConfigPaths(configFiles)
if err != nil {
c.Ui.Error(fmt.Sprintf("Config validation failed: %v", err.Error()))
return 1
}
return 0
}
func (c *ConfigTestCommand) Synopsis() string {
return "Validate config file"
}