code.cloudfoundry.org/cli@v7.1.0+incompatible/cf/commands/pluginrepo/remove_plugin_repo.go (about)

     1  package pluginrepo
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/i18n"
    15  )
    16  
    17  type RemovePluginRepo struct {
    18  	ui     terminal.UI
    19  	config coreconfig.ReadWriter
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&RemovePluginRepo{})
    24  }
    25  
    26  func (cmd *RemovePluginRepo) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "remove-plugin-repo",
    29  		Description: T("Remove a plugin repository"),
    30  		Usage: []string{
    31  			T("CF_NAME remove-plugin-repo REPO_NAME"),
    32  		},
    33  		Examples: []string{
    34  			"CF_NAME remove-plugin-repo PrivateRepo",
    35  		},
    36  		TotalArgs: 1,
    37  	}
    38  }
    39  
    40  func (cmd *RemovePluginRepo) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    41  	if len(fc.Args()) != 1 {
    42  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("remove-plugin-repo"))
    43  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    44  	}
    45  
    46  	reqs := []requirements.Requirement{}
    47  	return reqs, nil
    48  }
    49  
    50  func (cmd *RemovePluginRepo) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    51  	cmd.ui = deps.UI
    52  	cmd.config = deps.Config
    53  	return cmd
    54  }
    55  
    56  func (cmd *RemovePluginRepo) Execute(c flags.FlagContext) error {
    57  	cmd.ui.Say("")
    58  	repoName := strings.Trim(c.Args()[0], " ")
    59  
    60  	if i := cmd.findRepoIndex(repoName); i != -1 {
    61  		cmd.config.UnSetPluginRepo(i)
    62  		cmd.ui.Ok()
    63  		cmd.ui.Say(repoName + T(" removed from list of repositories"))
    64  		cmd.ui.Say("")
    65  	} else {
    66  		return errors.New(repoName + T(" does not exist as a repo"))
    67  	}
    68  	return nil
    69  }
    70  
    71  func (cmd RemovePluginRepo) findRepoIndex(repoName string) int {
    72  	repos := cmd.config.PluginRepos()
    73  	for i, repo := range repos {
    74  		if strings.ToLower(repo.Name) == strings.ToLower(repoName) {
    75  			return i
    76  		}
    77  	}
    78  	return -1
    79  }