influxdb/telegraf/plugins/outputs/file.go

67 lines
1.4 KiB
Go
Raw Normal View History

2018-10-05 21:43:01 +00:00
package outputs
import (
2018-10-17 19:51:35 +00:00
"errors"
2018-10-05 21:43:01 +00:00
"fmt"
"strconv"
"strings"
)
// File is based on telegraf file output plugin.
type File struct {
2018-10-16 00:38:36 +00:00
baseOutput
2018-10-05 21:43:01 +00:00
Files []FileConfig `json:"files"`
}
// FileConfig is the config settings of outpu file plugin.
type FileConfig struct {
Typ string `json:"type"`
Path string `json:"path"`
}
2018-10-16 00:38:36 +00:00
// PluginName is based on telegraf plugin name.
func (f *File) PluginName() string {
return "file"
}
2018-10-05 21:43:01 +00:00
// TOML encodes to toml string.
func (f *File) TOML() string {
s := make([]string, len(f.Files))
for k, v := range f.Files {
if v.Typ == "stdout" {
s[k] = strconv.Quote(v.Typ)
continue
}
s[k] = strconv.Quote(v.Path)
}
2018-10-16 00:38:36 +00:00
return fmt.Sprintf(`[[outputs.%s]]
2018-10-05 21:43:01 +00:00
## Files to write to, "stdout" is a specially handled file.
files = [%s]
2018-10-16 00:38:36 +00:00
`, f.PluginName(), strings.Join(s, ", "))
2018-10-05 21:43:01 +00:00
}
2018-10-17 19:51:35 +00:00
// UnmarshalTOML decodes the parsed data to the object
func (f *File) UnmarshalTOML(data interface{}) error {
dataOK, ok := data.(map[string]interface{})
if !ok {
return errors.New("bad files for file output plugin")
}
files, ok := dataOK["files"].([]interface{})
if !ok {
return errors.New("not an array for file output plugin")
}
for _, fi := range files {
fl := fi.(string)
if fl == "stdout" {
f.Files = append(f.Files, FileConfig{
Typ: "stdout",
})
continue
}
f.Files = append(f.Files, FileConfig{
Path: fl,
})
}
return nil
}