github.com/cloudfoundry/cli@v7.1.0+incompatible/actor/pluginaction/list.go (about)

     1  package pluginaction
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"github.com/blang/semver"
     6  )
     7  
     8  type OutdatedPlugin struct {
     9  	Name           string
    10  	CurrentVersion string
    11  	LatestVersion  string
    12  }
    13  
    14  func (actor Actor) GetOutdatedPlugins() ([]OutdatedPlugin, error) {
    15  	var outdatedPlugins []OutdatedPlugin
    16  
    17  	repoPlugins := map[string]string{}
    18  	for _, repo := range actor.config.PluginRepositories() {
    19  		repository, err := actor.client.GetPluginRepository(repo.URL)
    20  		if err != nil {
    21  			return nil, actionerror.GettingPluginRepositoryError{Name: repo.Name, Message: err.Error()}
    22  		}
    23  
    24  		for _, plugin := range repository.Plugins {
    25  			existingVersion, exist := repoPlugins[plugin.Name]
    26  			if exist {
    27  				if lessThan(existingVersion, plugin.Version) {
    28  					repoPlugins[plugin.Name] = plugin.Version
    29  				}
    30  			} else {
    31  				repoPlugins[plugin.Name] = plugin.Version
    32  			}
    33  		}
    34  	}
    35  
    36  	for _, installedPlugin := range actor.config.Plugins() {
    37  		repoVersion, exist := repoPlugins[installedPlugin.Name]
    38  		if exist && lessThan(installedPlugin.Version.String(), repoVersion) {
    39  			outdatedPlugins = append(outdatedPlugins, OutdatedPlugin{
    40  				Name:           installedPlugin.Name,
    41  				CurrentVersion: installedPlugin.Version.String(),
    42  				LatestVersion:  repoVersion,
    43  			})
    44  		}
    45  	}
    46  
    47  	return outdatedPlugins, nil
    48  }
    49  
    50  func lessThan(version1 string, version2 string) bool {
    51  	v1, err := semver.Make(version1)
    52  	if err != nil {
    53  		return false
    54  	}
    55  
    56  	v2, err := semver.Make(version2)
    57  	if err != nil {
    58  		return false
    59  	}
    60  
    61  	return v1.LT(v2)
    62  }