-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource.go
87 lines (77 loc) · 3.3 KB
/
resource.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
package cloudconfigclient
import (
"errors"
)
const (
defaultApplicationName = "default"
defaultApplicationProfile = "default"
)
var useDefaultLabel = map[string]string{"useDefaultLabel": "true"}
// Resource interface describes how to retrieve files from the Config Server.
type Resource interface {
// GetFile retrieves the specified file from the provided directory from the Config Server's default branch.
//
// The file will be deserialized into the specified interface type.
GetFile(directory string, file string, interfaceType interface{}) error
// GetFileFromBranch retrieves the specified file from the provided branch in the provided directory.
//
// The file will be deserialized into the specified interface type.
GetFileFromBranch(branch string, directory string, file string, interfaceType interface{}) error
// GetFileRaw retrieves the file from the default branch as a byte slice.
GetFileRaw(directory string, file string) ([]byte, error)
// GetFileFromBranchRaw retrieves the file from the specified branch as a byte slice.
GetFileFromBranchRaw(branch string, directory string, file string) ([]byte, error)
}
// GetFile retrieves the specified file from the provided directory from the Config Server's default branch.
//
// The file will be deserialized into the specified interface type.
func (c *Client) GetFile(directory string, file string, interfaceType interface{}) error {
return c.getFile([]string{defaultApplicationName, defaultApplicationProfile, directory, file}, useDefaultLabel, interfaceType)
}
// GetFileFromBranch retrieves the specified file from the provided branch in the provided directory.
//
// The file will be deserialized into the specified interface type.
func (c *Client) GetFileFromBranch(branch string, directory string, file string, interfaceType interface{}) error {
return c.getFile([]string{defaultApplicationName, defaultApplicationProfile, branch, directory, file}, nil, interfaceType)
}
func (c *Client) getFile(paths []string, params map[string]string, interfaceType interface{}) error {
fileFound := false
for _, client := range c.clients {
if err := client.GetResource(paths, params, interfaceType); err != nil {
if errors.Is(err, ErrResourceNotFound) {
continue
}
return err
}
fileFound = true
}
if !fileFound {
return errors.New("failed to find file in the Config Server")
}
return nil
}
// GetFileRaw retrieves the file from the default branch as a byte slice.
func (c *Client) GetFileRaw(directory string, file string) ([]byte, error) {
return c.getFileRaw([]string{defaultApplicationName, defaultApplicationProfile, directory, file}, useDefaultLabel)
}
// GetFileFromBranchRaw retrieves the file from the specified branch as a byte slice.
func (c *Client) GetFileFromBranchRaw(branch string, directory string, file string) ([]byte, error) {
return c.getFileRaw([]string{defaultApplicationName, defaultApplicationProfile, branch, directory, file}, nil)
}
func (c *Client) getFileRaw(paths []string, params map[string]string) (b []byte, err error) {
fileFound := false
for _, client := range c.clients {
b, err = client.GetResourceRaw(paths, params)
if err != nil {
if errors.Is(err, ErrResourceNotFound) {
continue
}
return
}
fileFound = true
}
if !fileFound {
err = errors.New("failed to find file in the Config Server")
}
return
}