forked from resourced/resourced
/
agent_tag.go
49 lines (40 loc) · 922 Bytes
/
agent_tag.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
package agent
import (
"bufio"
"github.com/resourced/resourced/libstring"
"io/ioutil"
"os"
"path"
"strings"
)
func (a *Agent) setTags() error {
a.Tags = make(map[string]string)
configDir := os.Getenv("RESOURCED_CONFIG_DIR")
if configDir == "" {
return nil
}
configDir = libstring.ExpandTildeAndEnv(configDir)
tagFiles, err := ioutil.ReadDir(path.Join(configDir, "tags"))
if err != nil {
return nil
}
for _, f := range tagFiles {
fullpath := path.Join(configDir, "tags", f.Name())
file, err := os.Open(fullpath)
if err != nil {
continue
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
tagsPerLine := strings.Split(scanner.Text(), ",")
for _, tagKeyValue := range tagsPerLine {
keyValue := strings.Split(tagKeyValue, "=")
if len(keyValue) >= 2 {
a.Tags[keyValue[0]] = strings.Join(keyValue[1:], "=")
}
}
}
}
return nil
}