github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/pluginaction/uninstall.go (about)

     1  package pluginaction
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  )
    10  
    11  //go:generate counterfeiter . PluginUninstaller
    12  
    13  type PluginUninstaller interface {
    14  	Run(pluginPath string, command string) error
    15  }
    16  
    17  func (actor Actor) UninstallPlugin(uninstaller PluginUninstaller, name string) error {
    18  	plugin, exist := actor.config.GetPlugin(name)
    19  	if !exist {
    20  		return actionerror.PluginNotFoundError{PluginName: name}
    21  	}
    22  
    23  	var binaryErr error
    24  
    25  	if actor.FileExists(plugin.Location) {
    26  		err := uninstaller.Run(plugin.Location, "CLI-MESSAGE-UNINSTALL")
    27  		if err != nil {
    28  			switch err.(type) {
    29  			case *exec.ExitError:
    30  				binaryErr = actionerror.PluginExecuteError{
    31  					Err: err,
    32  				}
    33  			case *os.PathError:
    34  				binaryErr = actionerror.PluginExecuteError{
    35  					Err: err,
    36  				}
    37  			default:
    38  				return err
    39  			}
    40  		}
    41  
    42  		// No test for sleeping for 500 ms for parity with pre-refactored behavior.
    43  		time.Sleep(500 * time.Millisecond)
    44  
    45  		err = os.Remove(plugin.Location)
    46  		if err != nil && !os.IsNotExist(err) {
    47  			if _, isPathError := err.(*os.PathError); isPathError {
    48  				binaryErr = actionerror.PluginBinaryRemoveFailedError{
    49  					Err: err,
    50  				}
    51  			} else {
    52  				return err
    53  			}
    54  		}
    55  	}
    56  
    57  	actor.config.RemovePlugin(name)
    58  	err := actor.config.WritePluginConfig()
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	return binaryErr
    64  }