1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
template.go 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
package export
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/libbeat/paths"
"github.com/elastic/beats/libbeat/template"
)
func GenTemplateConfigCmd(name, beatVersion string) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "template",
Short: "Export index template to stdout",
Run: func(cmd *cobra.Command, args []string) {
version, _ := cmd.Flags().GetString("es.version")
index, _ := cmd.Flags().GetString("index")
b, err := instance.NewBeat(name, beatVersion)
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err)
os.Exit(1)
}
err = b.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err)
os.Exit(1)
}
cfg := template.DefaultConfig
if b.Config.Template.Enabled() {
err = b.Config.Template.Unpack(&cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting template settings: %+v", err)
os.Exit(1)
}
}
tmpl, err := template.New(b.Info.Version, index, version, cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating template: %+v", err)
os.Exit(1)
}
fieldsPath := paths.Resolve(paths.Config, cfg.Fields)
templateString, err := tmpl.Load(fieldsPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error generating template: %+v", err)
os.Exit(1)
}
_, err = os.Stdout.WriteString(templateString.StringToPrint() + "\n")
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing template: %+v", err)
os.Exit(1)
}
},
}
genTemplateConfigCmd.Flags().String("es.version", beatVersion, "Elasticsearch version")
genTemplateConfigCmd.Flags().String("index", name, "Base index name")
return genTemplateConfigCmd
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.0.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385