github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/plugin/add_plugin_repo_command.go (about)

     1  package plugin
     2  
     3  import (
     4  	"github.com/liamawhite/cli-with-i18n/actor/pluginaction"
     5  	"github.com/liamawhite/cli-with-i18n/command"
     6  	"github.com/liamawhite/cli-with-i18n/command/flag"
     7  	"github.com/liamawhite/cli-with-i18n/command/plugin/shared"
     8  )
     9  
    10  //go:generate counterfeiter . AddPluginRepoActor
    11  
    12  type AddPluginRepoActor interface {
    13  	AddPluginRepository(repoName string, repoURL string) error
    14  }
    15  
    16  type AddPluginRepoCommand struct {
    17  	RequiredArgs      flag.AddPluginRepoArgs `positional-args:"yes"`
    18  	usage             interface{}            `usage:"CF_NAME add-plugin-repo REPO_NAME URL\n\nEXAMPLES:\n   CF_NAME add-plugin-repo ExampleRepo https://example.com/repo"`
    19  	relatedCommands   interface{}            `related_commands:"install-plugin, list-plugin-repos"`
    20  	SkipSSLValidation bool                   `short:"k" hidden:"true" description:"Skip SSL certificate validation"`
    21  	UI                command.UI
    22  	Config            command.Config
    23  	Actor             AddPluginRepoActor
    24  }
    25  
    26  func (cmd *AddPluginRepoCommand) Setup(config command.Config, ui command.UI) error {
    27  	cmd.UI = ui
    28  	cmd.Config = config
    29  	cmd.Actor = pluginaction.NewActor(config, shared.NewClient(config, ui, cmd.SkipSSLValidation))
    30  	return nil
    31  }
    32  
    33  func (cmd AddPluginRepoCommand) Execute(args []string) error {
    34  	err := cmd.Actor.AddPluginRepository(cmd.RequiredArgs.PluginRepoName, cmd.RequiredArgs.PluginRepoURL)
    35  	switch e := err.(type) {
    36  	case pluginaction.RepositoryAlreadyExistsError:
    37  		cmd.UI.DisplayTextWithFlavor("{{.RepositoryURL}} already registered as {{.RepositoryName}}",
    38  			map[string]interface{}{
    39  				"RepositoryName": e.Name,
    40  				"RepositoryURL":  e.URL,
    41  			})
    42  	case nil:
    43  		cmd.UI.DisplayTextWithFlavor("{{.RepositoryURL}} added as {{.RepositoryName}}",
    44  			map[string]interface{}{
    45  				"RepositoryName": cmd.RequiredArgs.PluginRepoName,
    46  				"RepositoryURL":  cmd.RequiredArgs.PluginRepoURL,
    47  			})
    48  	default:
    49  		return shared.HandleError(err)
    50  	}
    51  
    52  	return nil
    53  }