forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_repository.go
76 lines (64 loc) · 2.14 KB
/
plugin_repository.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
package pluginaction
import (
"fmt"
"strings"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/util/configv3"
)
func (actor Actor) AddPluginRepository(repoName string, repoURL string) error {
normalizedURL, err := normalizeURLPath(repoURL)
if err != nil {
return actionerror.AddPluginRepositoryError{
Name: repoName,
URL: repoURL,
Message: err.Error(),
}
}
repoNameLowerCased := strings.ToLower(repoName)
for _, repository := range actor.config.PluginRepositories() {
existingRepoNameLowerCased := strings.ToLower(repository.Name)
switch {
case repoNameLowerCased == existingRepoNameLowerCased && normalizedURL == repository.URL:
return actionerror.RepositoryAlreadyExistsError{Name: repository.Name, URL: repository.URL}
case repoNameLowerCased == existingRepoNameLowerCased && normalizedURL != repository.URL:
return actionerror.RepositoryNameTakenError{Name: repository.Name}
case repoNameLowerCased != existingRepoNameLowerCased:
continue
}
}
_, err = actor.client.GetPluginRepository(normalizedURL)
if err != nil {
return actionerror.AddPluginRepositoryError{
Name: repoName,
URL: normalizedURL,
Message: err.Error(),
}
}
actor.config.AddPluginRepository(repoName, normalizedURL)
return nil
}
func (actor Actor) GetPluginRepository(repositoryName string) (configv3.PluginRepository, error) {
repositoryNameLowered := strings.ToLower(repositoryName)
for _, repository := range actor.config.PluginRepositories() {
if repositoryNameLowered == strings.ToLower(repository.Name) {
return repository, nil
}
}
return configv3.PluginRepository{}, actionerror.RepositoryNotRegisteredError{Name: repositoryName}
}
func (actor Actor) IsPluginRepositoryRegistered(repositoryName string) bool {
for _, repository := range actor.config.PluginRepositories() {
if repositoryName == repository.Name {
return true
}
}
return false
}
func normalizeURLPath(rawURL string) (string, error) {
prefix := ""
if !strings.Contains(rawURL, "://") {
prefix = "https://"
}
normalizedURL := fmt.Sprintf("%s%s", prefix, rawURL)
return strings.TrimSuffix(normalizedURL, "/"), nil
}