github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/command/plugin/shared/errors.go (about)

     1  package shared
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type JSONSyntaxError struct {
     9  	Err error
    10  }
    11  
    12  func (e JSONSyntaxError) Error() string {
    13  	return "Invalid JSON content from server: {{.Err}}"
    14  }
    15  
    16  func (e JSONSyntaxError) Translate(translate func(string, ...interface{}) string) string {
    17  	return translate(e.Error(), map[string]interface{}{
    18  		"Err": e.Err.Error(),
    19  	})
    20  }
    21  
    22  // PluginInstallationCancelled is used to ignore the scenario when the user
    23  // responds with 'no' when prompted to install plugin and exit 0.
    24  type PluginInstallationCancelled struct {
    25  }
    26  
    27  func (_ PluginInstallationCancelled) Error() string {
    28  	return "Plugin installation cancelled"
    29  }
    30  
    31  type PluginNotFoundInRepositoryError struct {
    32  	BinaryName     string
    33  	PluginName     string
    34  	RepositoryName string
    35  }
    36  
    37  func (e PluginNotFoundInRepositoryError) Error() string {
    38  	return "Plugin {{.PluginName}} not found in repository {{.RepositoryName}}.\nUse '{{.BinaryName}} repo-plugins -r {{.RepositoryName}}' to list plugins available in the repo."
    39  }
    40  
    41  func (e PluginNotFoundInRepositoryError) Translate(translate func(string, ...interface{}) string) string {
    42  	return translate(e.Error(), map[string]interface{}{
    43  		"PluginName":     e.PluginName,
    44  		"RepositoryName": e.RepositoryName,
    45  		"BinaryName":     e.BinaryName,
    46  	})
    47  }
    48  
    49  type PluginNotFoundError struct {
    50  	PluginName string
    51  }
    52  
    53  func (e PluginNotFoundError) Error() string {
    54  	return "Plugin {{.PluginName}} does not exist."
    55  }
    56  
    57  func (e PluginNotFoundError) Translate(translate func(string, ...interface{}) string) string {
    58  	return translate(e.Error(), map[string]interface{}{
    59  		"PluginName": e.PluginName,
    60  	})
    61  }
    62  
    63  type PluginNotFoundOnDiskOrInAnyRepositoryError struct {
    64  	PluginName string
    65  	BinaryName string
    66  }
    67  
    68  func (e PluginNotFoundOnDiskOrInAnyRepositoryError) Error() string {
    69  	return "Plugin {{.PluginName}} not found on disk or in any registered repo.\nUse '{{.BinaryName}} repo-plugins' to list plugins available in the repos."
    70  }
    71  
    72  func (e PluginNotFoundOnDiskOrInAnyRepositoryError) Translate(translate func(string, ...interface{}) string) string {
    73  	return translate(e.Error(), map[string]interface{}{
    74  		"PluginName": e.PluginName,
    75  		"BinaryName": e.BinaryName,
    76  	})
    77  }
    78  
    79  // NoCompatibleBinaryError is returned when a repository contains a specified
    80  // plugin but not for the specified platform
    81  type NoCompatibleBinaryError struct {
    82  }
    83  
    84  func (e NoCompatibleBinaryError) Error() string {
    85  	return "Plugin requested has no binary available for your platform."
    86  }
    87  
    88  func (e NoCompatibleBinaryError) Translate(translate func(string, ...interface{}) string) string {
    89  	return translate(e.Error())
    90  }
    91  
    92  type NoPluginRepositoriesError struct{}
    93  
    94  func (_ NoPluginRepositoriesError) Error() string {
    95  	return "No plugin repositories registered to search for plugin updates."
    96  }
    97  
    98  func (e NoPluginRepositoriesError) Translate(translate func(string, ...interface{}) string) string {
    99  	return translate(e.Error())
   100  }
   101  
   102  // GettingPluginRepositoryError is returned when there's an error
   103  // accessing the plugin repository
   104  type GettingPluginRepositoryError struct {
   105  	Name    string
   106  	Message string
   107  }
   108  
   109  func (_ GettingPluginRepositoryError) Error() string {
   110  	return "Could not get plugin repository '{{.RepositoryName}}'\n{{.ErrorMessage}}"
   111  }
   112  
   113  func (e GettingPluginRepositoryError) Translate(translate func(string, ...interface{}) string) string {
   114  	return translate(e.Error(), map[string]interface{}{"RepositoryName": e.Name, "ErrorMessage": e.Message})
   115  }
   116  
   117  // RepositoryNameTakenError is returned when adding a plugin repository
   118  // fails due to a repository already existing with the same name
   119  type RepositoryNameTakenError struct {
   120  	Name string
   121  }
   122  
   123  func (_ RepositoryNameTakenError) Error() string {
   124  	return "Plugin repo named '{{.RepositoryName}}' already exists, please use another name."
   125  }
   126  
   127  func (e RepositoryNameTakenError) Translate(translate func(string, ...interface{}) string) string {
   128  	return translate(e.Error(), map[string]interface{}{"RepositoryName": e.Name})
   129  }
   130  
   131  type AddPluginRepositoryError struct {
   132  	Name    string
   133  	URL     string
   134  	Message string
   135  }
   136  
   137  func (_ AddPluginRepositoryError) Error() string {
   138  	return "Could not add repository '{{.RepositoryName}}' from {{.RepositoryURL}}: {{.Message}}"
   139  }
   140  
   141  func (e AddPluginRepositoryError) Translate(translate func(string, ...interface{}) string) string {
   142  	return translate(e.Error(), map[string]interface{}{
   143  		"RepositoryName": e.Name,
   144  		"RepositoryURL":  e.URL,
   145  		"Message":        e.Message,
   146  	})
   147  }
   148  
   149  // FileNotFoundError is returned when a local plugin binary is not found during
   150  // installation.
   151  type FileNotFoundError struct {
   152  	Path string
   153  }
   154  
   155  func (_ FileNotFoundError) Error() string {
   156  	return "File not found locally, make sure the file exists at given path {{.FilePath}}"
   157  }
   158  
   159  func (e FileNotFoundError) Translate(translate func(string, ...interface{}) string) string {
   160  	return translate(e.Error(), map[string]interface{}{
   161  		"FilePath": e.Path,
   162  	})
   163  }
   164  
   165  // PluginInvalidError is returned with a plugin is invalid because it is
   166  // missing a name or has 0 commands.
   167  type PluginInvalidError struct {
   168  	Err error
   169  }
   170  
   171  func (e PluginInvalidError) Error() string {
   172  	baseErrString := "File is not a valid cf CLI plugin binary."
   173  
   174  	if e.Err != nil {
   175  		return fmt.Sprintf("%s\n%s", e.Err, baseErrString)
   176  	}
   177  
   178  	return baseErrString
   179  }
   180  
   181  func (e PluginInvalidError) Translate(translate func(string, ...interface{}) string) string {
   182  	return translate(e.Error())
   183  }
   184  
   185  // PluginCommandConflictError is returned when a plugin command name conflicts
   186  // with a native or existing plugin command name.
   187  type PluginCommandsConflictError struct {
   188  	PluginName     string
   189  	PluginVersion  string
   190  	CommandNames   []string
   191  	CommandAliases []string
   192  }
   193  
   194  // PluginBinaryRemoveFailedError is returned when the removal of a plugin
   195  // binary fails.
   196  type PluginBinaryRemoveFailedError struct {
   197  	Err error
   198  }
   199  
   200  func (e PluginBinaryRemoveFailedError) Error() string {
   201  	return "The plugin has been uninstalled but removing the plugin binary failed.\nRemove it manually or subsequent installations of the plugin may fail\n{{.Err}}"
   202  }
   203  
   204  func (e PluginBinaryRemoveFailedError) Translate(translate func(string, ...interface{}) string) string {
   205  	return translate(e.Error(), map[string]interface{}{
   206  		"Err": e.Err,
   207  	})
   208  }
   209  
   210  // PluginBinaryUninstallError is returned when running the plugin's uninstall
   211  // hook fails.
   212  type PluginBinaryUninstallError struct {
   213  	Err error
   214  }
   215  
   216  func (e PluginBinaryUninstallError) Error() string {
   217  	return "The plugin's uninstall method returned an unexpected error.\nThe plugin uninstall will proceed. Contact the plugin author if you need help.\n{{.Err}}"
   218  }
   219  
   220  func (e PluginBinaryUninstallError) Translate(translate func(string, ...interface{}) string) string {
   221  	return translate(e.Error(), map[string]interface{}{
   222  		"Err": e.Err,
   223  	})
   224  }
   225  
   226  func (e PluginCommandsConflictError) Error() string {
   227  	switch {
   228  	case len(e.CommandNames) > 0 && len(e.CommandAliases) > 0:
   229  		return "Plugin {{.PluginName}} v{{.PluginVersion}} could not be installed as it contains commands with names and aliases that are already used: {{.CommandNamesAndAliases}}."
   230  	case len(e.CommandNames) > 0:
   231  		return "Plugin {{.PluginName}} v{{.PluginVersion}} could not be installed as it contains commands with names that are already used: {{.CommandNames}}."
   232  	case len(e.CommandAliases) > 0:
   233  		return "Plugin {{.PluginName}} v{{.PluginVersion}} could not be installed as it contains commands with aliases that are already used: {{.CommandAliases}}."
   234  	default:
   235  		return "Plugin {{.PluginName}} v{{.PluginVersion}} could not be installed as it contains commands with names or aliases that are already used."
   236  	}
   237  }
   238  
   239  func (e PluginCommandsConflictError) Translate(translate func(string, ...interface{}) string) string {
   240  	return translate(e.Error(), map[string]interface{}{
   241  		"PluginName":             e.PluginName,
   242  		"PluginVersion":          e.PluginVersion,
   243  		"CommandNames":           strings.Join(e.CommandNames, ", "),
   244  		"CommandAliases":         strings.Join(e.CommandAliases, ", "),
   245  		"CommandNamesAndAliases": strings.Join(append(e.CommandNames, e.CommandAliases...), ", "),
   246  	})
   247  }
   248  
   249  // PluginAlreadyInstalledError is returned when the plugin has the same name as
   250  // an installed plugin.
   251  type PluginAlreadyInstalledError struct {
   252  	BinaryName string
   253  	Name       string
   254  	Version    string
   255  }
   256  
   257  func (_ PluginAlreadyInstalledError) Error() string {
   258  	return "Plugin {{.Name}} {{.Version}} could not be installed. A plugin with that name is already installed.\nTIP: Use '{{.BinaryName}} install-plugin -f' to force a reinstall."
   259  }
   260  
   261  func (e PluginAlreadyInstalledError) Translate(translate func(string, ...interface{}) string) string {
   262  	return translate(e.Error(), map[string]interface{}{
   263  		"BinaryName": e.BinaryName,
   264  		"Name":       e.Name,
   265  		"Version":    e.Version,
   266  	})
   267  }
   268  
   269  type DownloadPluginHTTPError struct {
   270  	Message string
   271  }
   272  
   273  func (_ DownloadPluginHTTPError) Error() string {
   274  	return "Download attempt failed; server returned {{.ErrorMessage}}\nUnable to install; plugin is not available from the given URL."
   275  }
   276  
   277  func (e DownloadPluginHTTPError) Translate(translate func(string, ...interface{}) string) string {
   278  	return translate(e.Error(), map[string]interface{}{
   279  		"ErrorMessage": e.Message,
   280  	})
   281  }
   282  
   283  type FetchingPluginInfoFromRepositoriesError struct {
   284  	Message        string
   285  	RepositoryName string
   286  }
   287  
   288  func (_ FetchingPluginInfoFromRepositoriesError) Error() string {
   289  	return "Plugin list download failed; repository {{.RepositoryName}} returned {{.ErrorMessage}}."
   290  }
   291  
   292  func (e FetchingPluginInfoFromRepositoriesError) Translate(translate func(string, ...interface{}) string) string {
   293  	return translate(e.Error(), map[string]interface{}{
   294  		"RepositoryName": e.RepositoryName,
   295  		"ErrorMessage":   e.Message,
   296  	})
   297  }
   298  
   299  type RepositoryNotRegisteredError struct {
   300  	Name string
   301  }
   302  
   303  func (_ RepositoryNotRegisteredError) Error() string {
   304  	return "Plugin repository {{.Name}} not found.\nUse 'cf list-plugin-repos' to list registered repos."
   305  }
   306  
   307  func (e RepositoryNotRegisteredError) Translate(translate func(string, ...interface{}) string) string {
   308  	return translate(e.Error(), map[string]interface{}{
   309  		"Name": e.Name,
   310  	})
   311  }