-
Notifications
You must be signed in to change notification settings - Fork 17
/
setingesteruuid_unix.go
63 lines (58 loc) · 1.58 KB
/
setingesteruuid_unix.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//go:build !windows
// +build !windows
package config
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/google/renameio"
"github.com/google/uuid"
)
// SetIngesterUUID modifies the configuration file at loc, setting the
// Ingester-UUID parameter to the given UUID. This function allows ingesters
// to assign themselves a UUID if one is not given in the configuration file.
func (ic *IngestConfig) SetIngesterUUID(id uuid.UUID, loc string) (err error) {
if zeroUUID(id) {
return errors.New("UUID is empty")
}
var content string
if content, err = reloadContent(loc); err != nil {
return
}
//crack the config file into lines
lines := strings.Split(content, "\n")
lo := argInGlobalLines(lines, uuidParam)
if lo == -1 {
//UUID value not set, insert immediately after global
gStart, _, ok := globalLineBoundary(lines)
if !ok {
err = ErrGlobalSectionNotFound
return
}
lines, err = insertLine(lines, fmt.Sprintf(`%s="%s"`, uuidParam, id.String()), gStart+1)
} else {
//found it, update it
lines, err = updateLine(lines, uuidParam, fmt.Sprintf(`"%s"`, id), lo)
}
if err != nil {
return
}
ic.Ingester_UUID = id.String()
content = strings.Join(lines, "\n")
err = updateConfigFile(loc, content)
return
}
func updateConfigFile(loc string, content string) error {
if loc == `` {
return errors.New("Configuration was loaded with bytes, cannot update")
}
fout, err := renameio.TempFile(filepath.Dir(loc), loc)
if err != nil {
return err
}
if err := writeFull(fout, []byte(content)); err != nil {
return err
}
return fout.CloseAtomicallyReplace()
}