summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/discovery/sd/conffile.go
blob: 73aef17371ed0b08508326b8fd70b5e1fe1050f3 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package sd

import (
	"context"
	"os"

	"github.com/netdata/netdata/go/go.d.plugin/logger"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/multipath"
)

type confFile struct {
	source  string
	content []byte
}

func newConfFileReader(log *logger.Logger, dir multipath.MultiPath) *confFileReader {
	return &confFileReader{
		Logger:   log,
		confDir:  dir,
		confChan: make(chan confFile),
	}
}

type confFileReader struct {
	*logger.Logger

	confDir  multipath.MultiPath
	confChan chan confFile
}

func (c *confFileReader) run(ctx context.Context) {
	files, err := c.confDir.FindFiles(".conf")
	if err != nil {
		c.Error(err)
		return
	}

	if len(files) == 0 {
		return
	}

	var confFiles []confFile

	for _, file := range files {
		bs, err := os.ReadFile(file)
		if err != nil {
			c.Error(err)
			continue
		}
		confFiles = append(confFiles, confFile{
			source:  file,
			content: bs,
		})
	}

	for _, conf := range confFiles {
		select {
		case <-ctx.Done():
		case c.confChan <- conf:
		}
	}

}

func (c *confFileReader) configs() chan confFile {
	return c.confChan
}