/
repositoryutils.go
68 lines (58 loc) · 1.69 KB
/
repositoryutils.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
package utils
import (
"errors"
"github.com/buger/jsonparser"
"github.com/jfrog/jfrog-cli-go/jfrog-client/artifactory/auth"
"github.com/jfrog/jfrog-cli-go/jfrog-client/utils"
"github.com/jfrog/jfrog-cli-go/jfrog-client/utils/errorutils"
"net/http"
"github.com/jfrog/jfrog-cli-go/jfrog-client/httpclient"
)
type RepoType int
const (
LOCAL RepoType = iota
REMOTE
VIRTUAL
)
var RepoTypes = []string{
"local",
"remote",
"virtual",
}
func (repoType RepoType) String() string {
return RepoTypes[repoType]
}
func GetRepositories(artDetails auth.ArtifactoryDetails, repoType ...RepoType) ([]string, error) {
repos := []string{}
for _, v := range repoType {
r, err := execGetRepositories(artDetails, v)
if err != nil {
return repos, err
}
if len(r) > 0 {
repos = append(repos, r...)
}
}
return repos, nil
}
func execGetRepositories(artDetails auth.ArtifactoryDetails, repoType RepoType) ([]string, error) {
repos := []string{}
artDetails.SetUrl(utils.AddTrailingSlashIfNeeded(artDetails.GetUrl()))
apiUrl := artDetails.GetUrl() + "api/repositories?type=" + repoType.String()
httpClientsDetails := artDetails.CreateHttpClientDetails()
client := httpclient.NewDefaultHttpClient()
resp, body, _, err := client.SendGet(apiUrl, true, httpClientsDetails)
if err != nil {
return repos, err
}
if resp.StatusCode != http.StatusOK {
return repos, errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + utils.IndentJson(body)))
}
jsonparser.ArrayEach(body, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
val, _, _, err := jsonparser.Get(value, "key")
if err == nil {
repos = append(repos, string(val))
}
})
return repos, nil
}