Repository navigation
Expand file tree
/
Copy pathpersistentcache.go
More file actions
65 lines (58 loc) · 1.8 KB
/
Copy pathpersistentcache.go
File metadata and controls
65 lines (58 loc) · 1.8 KB
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-present Datadog, Inc.
package persistentcache
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/StackVista/stackstate-agent/pkg/config"
)
// Invalid characters to clean up
var invalidChars = regexp.MustCompile("[^a-zA-Z0-9_-]")
// Return a file where to store the data. We split the key by ":", using the
// first prefix as directory, if present. This is useful for integrations, which
// use the check_id formed with $check_name:$hash
func getFileForKey(key string) (string, error) {
parent := config.Datadog.GetString("run_path")
paths := strings.SplitN(key, ":", 2)
cleanedPath := invalidChars.ReplaceAllString(paths[0], "")
if len(paths) == 1 {
// If there is no colon, just return the key
return filepath.Join(parent, cleanedPath), nil
}
// Otherwise, create the directory with a prefix
err := os.MkdirAll(filepath.Join(parent, cleanedPath), 0700)
if err != nil {
return "", err
}
cleanedFile := invalidChars.ReplaceAllString(paths[1], "")
return filepath.Join(parent, cleanedPath, cleanedFile), nil
}
// Write stores data on disk in the run directory.
func Write(key, value string) error {
path, err := getFileForKey(key)
if err != nil {
return err
}
return ioutil.WriteFile(path, []byte(value), 0600)
}
// Read returns a value previously stored, or the empty string.
func Read(key string) (string, error) {
path, err := getFileForKey(key)
if err != nil {
return "", err
}
_, err = os.Stat(path)
if os.IsNotExist(err) {
return "", nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return string(content), nil
}