forked from StephanU/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
48 lines (40 loc) · 1.15 KB
/
config.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
package export
import (
"fmt"
"os"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/libbeat/common/cli"
)
// GenExportConfigCmd write to stdout the current configuration in the YAML format.
func GenExportConfigCmd(name, idxPrefix, beatVersion string) *cobra.Command {
return &cobra.Command{
Use: "config",
Short: "Export current config to stdout",
Run: cli.RunWith(func(cmd *cobra.Command, args []string) error {
return exportConfig(name, idxPrefix, beatVersion)
}),
}
}
func exportConfig(name, idxPrefix, beatVersion string) error {
b, err := instance.NewBeat(name, idxPrefix, beatVersion)
if err != nil {
return fmt.Errorf("error initializing beat: %s", err)
}
err = b.Init()
if err != nil {
return fmt.Errorf("error initializing beat: %s", err)
}
var config map[string]interface{}
err = b.RawConfig.Unpack(&config)
if err != nil {
return fmt.Errorf("error unpacking config, error: %s", err)
}
res, err := yaml.Marshal(config)
if err != nil {
return fmt.Errorf("Error converting config to YAML format, error: %s", err)
}
os.Stdout.Write(res)
return nil
}