forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_command.go
119 lines (101 loc) · 2.91 KB
/
config_command.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
package run
import (
"flag"
"fmt"
"io"
"log"
"os"
"github.com/BurntSushi/toml"
"github.com/influxdata/kapacitor/server"
)
// PrintConfigCommand represents the command executed by "kapacitord config".
type PrintConfigCommand struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
// NewPrintConfigCommand return a new instance of PrintConfigCommand.
func NewPrintConfigCommand() *PrintConfigCommand {
return &PrintConfigCommand{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}
// Run parses and prints the current config loaded.
func (cmd *PrintConfigCommand) Run(args ...string) error {
// Parse command flags.
fs := flag.NewFlagSet("", flag.ContinueOnError)
configPath := fs.String("config", "", "")
hostname := fs.String("hostname", "", "")
fs.Usage = func() { fmt.Fprintln(cmd.Stderr, printConfigUsage) }
if err := fs.Parse(args); err != nil {
return err
}
// Parse config from path.
config, err := cmd.parseConfig(FindConfigPath(*configPath))
if err != nil {
return fmt.Errorf("parse config: %s", err)
}
// Apply any environment variables on top of the parsed config
if err := config.ApplyEnvOverrides(); err != nil {
return fmt.Errorf("apply env config: %v", err)
}
// Override config properties.
if *hostname != "" {
config.Hostname = *hostname
}
// Validate the configuration.
if err := config.Validate(); err != nil {
return fmt.Errorf("%s. To generate a valid configuration file run `kapacitord config > kapacitor.generated.conf`.", err)
}
toml.NewEncoder(cmd.Stdout).Encode(config)
fmt.Fprint(cmd.Stdout, "\n")
return nil
}
// FindConfigPath returns the config path specified or searches for a valid config path.
// It will return a path by searching in this order:
// 1. The given configPath
// 2. The environment variable KAPACITOR_CONFIG_PATH
// 3. The first non empty kapacitor.conf file in the path:
// - ~/.kapacitor/
// - /etc/kapacitor/
func FindConfigPath(configPath string) string {
if configPath != "" {
if configPath == os.DevNull {
return ""
}
return configPath
} else if envVar := os.Getenv("KAPACITOR_CONFIG_PATH"); envVar != "" {
return envVar
}
for _, path := range []string{
os.ExpandEnv("${HOME}/.kapacitor/kapacitor.conf"),
"/etc/kapacitor/kapacitor.conf",
} {
if fi, err := os.Stat(path); err == nil && fi.Size() != 0 {
return path
}
}
return ""
}
// ParseConfig parses the config at path.
// Returns a demo configuration if path is blank.
func (cmd *PrintConfigCommand) parseConfig(path string) (*server.Config, error) {
config, err := server.NewDemoConfig()
if err != nil {
config = server.NewConfig()
}
if path == "" {
return config, nil
}
log.Println("Merging with configuration at:", path)
if _, err := toml.DecodeFile(path, &config); err != nil {
return nil, err
}
config.PostInit()
return config, nil
}
var printConfigUsage = `usage: config
config displays the default configuration
`