-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis_config_parser.go
103 lines (83 loc) · 1.95 KB
/
redis_config_parser.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package phpredishandler
import (
"os"
"path/filepath"
"strconv"
"strings"
"github.com/paketo-buildpacks/packit/v2/fs"
)
type RedisConfig struct {
Hostname string
Port int
Password string
}
type RedisConfigParser struct {
}
func NewRedisConfigParser() RedisConfigParser {
return RedisConfigParser{}
}
func (p RedisConfigParser) Parse(dir string) (RedisConfig, error) {
hostFilepath := filepath.Join(dir, "host")
hostnameFilepath := filepath.Join(dir, "hostname")
hostFileExists, err := fs.Exists(hostFilepath)
if err != nil {
return RedisConfig{}, err
}
hostname := "127.0.0.1"
if hostFileExists {
hostnameBytes, err := os.ReadFile(hostFilepath)
if err != nil {
return RedisConfig{}, err
}
hostname = strings.TrimSpace(string(hostnameBytes))
} else {
hostnameFileExists, err := fs.Exists(hostnameFilepath)
if err != nil {
// untested
return RedisConfig{}, err
}
if hostnameFileExists {
hostnameBytes, err := os.ReadFile(hostnameFilepath)
if err != nil {
return RedisConfig{}, err
}
hostname = strings.TrimSpace(string(hostnameBytes))
}
}
portFilepath := filepath.Join(dir, "port")
portFileExists, err := fs.Exists(portFilepath)
if err != nil {
// untested
return RedisConfig{}, err
}
port := 6379
if portFileExists {
portBytes, err := os.ReadFile(portFilepath)
if err != nil {
return RedisConfig{}, err
}
port, err = strconv.Atoi(strings.TrimSpace(string(portBytes)))
if err != nil {
return RedisConfig{}, err
}
}
passwordFilepath := filepath.Join(dir, "password")
passwordFileExists, err := fs.Exists(passwordFilepath)
if err != nil {
// untested
return RedisConfig{}, err
}
password := ""
if passwordFileExists {
passwordBytes, err := os.ReadFile(passwordFilepath)
if err != nil {
return RedisConfig{}, err
}
password = strings.TrimSpace(string(passwordBytes))
}
return RedisConfig{
Hostname: hostname,
Port: port,
Password: password,
}, nil
}