github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/minimum_version_check.go (about)

     1  package cloudcontroller
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     5  	"github.com/blang/semver"
     6  )
     7  
     8  // MinimumAPIVersionCheck compares `current` to `minimum`.  If `current` is
     9  // older than `minimum` then an error is returned; otherwise, nil is returned.
    10  func MinimumAPIVersionCheck(current string, minimum string) error {
    11  	if minimum == "" {
    12  		return nil
    13  	}
    14  
    15  	currentSemvar, err := semver.Make(current)
    16  	if err != nil {
    17  		return err
    18  	}
    19  
    20  	minimumSemvar, err := semver.Make(minimum)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	if currentSemvar.Compare(minimumSemvar) == -1 {
    26  		return ccerror.MinimumAPIVersionNotMetError{
    27  			CurrentVersion: current,
    28  			MinimumVersion: minimum,
    29  		}
    30  	}
    31  
    32  	return nil
    33  }