github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/pluginaction/plugin_repository.go (about)

     1  package pluginaction
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/util/configv3"
     9  )
    10  
    11  func (actor Actor) AddPluginRepository(repoName string, repoURL string) error {
    12  	normalizedURL, err := normalizeURLPath(repoURL)
    13  	if err != nil {
    14  		return actionerror.AddPluginRepositoryError{
    15  			Name:    repoName,
    16  			URL:     repoURL,
    17  			Message: err.Error(),
    18  		}
    19  	}
    20  
    21  	repoNameLowerCased := strings.ToLower(repoName)
    22  	for _, repository := range actor.config.PluginRepositories() {
    23  		existingRepoNameLowerCased := strings.ToLower(repository.Name)
    24  		switch {
    25  		case repoNameLowerCased == existingRepoNameLowerCased && normalizedURL == repository.URL:
    26  			return actionerror.RepositoryAlreadyExistsError{Name: repository.Name, URL: repository.URL}
    27  		case repoNameLowerCased == existingRepoNameLowerCased && normalizedURL != repository.URL:
    28  			return actionerror.RepositoryNameTakenError{Name: repository.Name}
    29  		case repoNameLowerCased != existingRepoNameLowerCased:
    30  			continue
    31  		}
    32  	}
    33  
    34  	_, err = actor.client.GetPluginRepository(normalizedURL)
    35  	if err != nil {
    36  		return actionerror.AddPluginRepositoryError{
    37  			Name:    repoName,
    38  			URL:     normalizedURL,
    39  			Message: err.Error(),
    40  		}
    41  	}
    42  
    43  	actor.config.AddPluginRepository(repoName, normalizedURL)
    44  	return nil
    45  }
    46  
    47  func (actor Actor) GetPluginRepository(repositoryName string) (configv3.PluginRepository, error) {
    48  	repositoryNameLowered := strings.ToLower(repositoryName)
    49  
    50  	for _, repository := range actor.config.PluginRepositories() {
    51  		if repositoryNameLowered == strings.ToLower(repository.Name) {
    52  			return repository, nil
    53  		}
    54  	}
    55  	return configv3.PluginRepository{}, actionerror.RepositoryNotRegisteredError{Name: repositoryName}
    56  }
    57  
    58  func (actor Actor) IsPluginRepositoryRegistered(repositoryName string) bool {
    59  	for _, repository := range actor.config.PluginRepositories() {
    60  		if repositoryName == repository.Name {
    61  			return true
    62  		}
    63  	}
    64  	return false
    65  }
    66  
    67  func normalizeURLPath(rawURL string) (string, error) {
    68  	prefix := ""
    69  	if !strings.Contains(rawURL, "://") {
    70  		prefix = "https://"
    71  	}
    72  
    73  	normalizedURL := fmt.Sprintf("%s%s", prefix, rawURL)
    74  
    75  	return strings.TrimSuffix(normalizedURL, "/"), nil
    76  }