/
find.go
48 lines (35 loc) · 1.11 KB
/
find.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
package credhub
import (
"encoding/json"
"io"
"net/http"
"net/url"
"code.cloudfoundry.org/credhub-cli/credhub/credentials"
)
// FindByPartialName retrieves a list of stored credential names which contain the search.
func (ch *CredHub) FindByPartialName(nameLike string) (credentials.FindResults, error) {
return ch.findByPathOrNameLike("name-like", nameLike)
}
// FindByPath retrieves a list of stored credential names which are within the specified path.
func (ch *CredHub) FindByPath(path string) (credentials.FindResults, error) {
return ch.findByPathOrNameLike("path", path)
}
func (ch *CredHub) findByPathOrNameLike(key, value string) (credentials.FindResults, error) {
var creds credentials.FindResults
body, err := ch.find(key, value)
if err != nil {
return creds, err
}
err = json.Unmarshal(body, &creds)
return creds, err
}
func (ch *CredHub) find(key, value string) ([]byte, error) {
query := url.Values{}
query.Set(key, value)
resp, err := ch.Request(http.MethodGet, "/api/v1/data", query, nil, true)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}