github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/command/api_version_warning.go (about)

     1  package command
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     5  	"code.cloudfoundry.org/cli/version"
     6  	"github.com/blang/semver"
     7  )
     8  
     9  func WarnIfCLIVersionBelowAPIDefinedMinimum(WarnIfCLIVersionBelowAPIDefinedMinmum Config, ui UI) error {
    10  	minVer := WarnIfCLIVersionBelowAPIDefinedMinmum.MinCLIVersion()
    11  	currentVer := WarnIfCLIVersionBelowAPIDefinedMinmum.BinaryVersion()
    12  
    13  	isOutdated, err := checkVersionOutdated(currentVer, minVer)
    14  	if err != nil {
    15  		return err
    16  	}
    17  
    18  	if isOutdated {
    19  		ui.DisplayWarning("Cloud Foundry API version {{.APIVersion}} requires CLI version {{.MinCLIVersion}}. You are currently on version {{.BinaryVersion}}. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads",
    20  			map[string]interface{}{
    21  				"APIVersion":    WarnIfCLIVersionBelowAPIDefinedMinmum.APIVersion(),
    22  				"MinCLIVersion": minVer,
    23  				"BinaryVersion": currentVer,
    24  			})
    25  		ui.DisplayNewline()
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  func WarnIfAPIVersionBelowSupportedMinimum(apiVersion string, ui UI) error {
    32  	isOutdated, err := checkVersionOutdated(apiVersion, ccversion.MinV2ClientVersion)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	if isOutdated {
    38  		ui.DisplayWarning("Your API version is no longer supported. Upgrade to a newer version of the API. Please refer to https://github.com/cloudfoundry/cli/wiki/Versioning-Policy#cf-cli-minimum-supported-version")
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  func checkVersionOutdated(current string, minimum string) (bool, error) {
    45  	if current == version.DefaultVersion || minimum == "" {
    46  		return false, nil
    47  	}
    48  
    49  	currentSemver, err := semver.Make(current)
    50  	if err != nil {
    51  		return false, err
    52  	}
    53  
    54  	minimumSemver, err := semver.Make(minimum)
    55  	if err != nil {
    56  		return false, err
    57  	}
    58  
    59  	if currentSemver.Compare(minimumSemver) == -1 {
    60  		return true, nil
    61  	}
    62  
    63  	return false, nil
    64  }