github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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  type APIVersionTooHighError struct{}
    10  
    11  func (a APIVersionTooHighError) Error() string {
    12  	return ""
    13  }
    14  
    15  func WarnIfCLIVersionBelowAPIDefinedMinimum(config Config, apiVersion string, ui UI) error {
    16  	minVer := config.MinCLIVersion()
    17  	currentVer := config.BinaryVersion()
    18  
    19  	isOutdated, err := CheckVersionOutdated(currentVer, minVer)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	if isOutdated {
    25  		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",
    26  			map[string]interface{}{
    27  				"APIVersion":    apiVersion,
    28  				"MinCLIVersion": minVer,
    29  				"BinaryVersion": currentVer,
    30  			})
    31  	}
    32  
    33  	return nil
    34  }
    35  
    36  func WarnIfAPIVersionBelowSupportedMinimum(apiVersion string, ui UI) error {
    37  	isOutdated, err := CheckVersionOutdated(apiVersion, ccversion.MinSupportedV2ClientVersion)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	if isOutdated {
    43  		ui.DisplayWarning("Your CF API version ({{.APIVersion}}) is no longer supported. "+
    44  			"Upgrade to a newer version of the API (minimum version {{.MinSupportedVersion}}). Please refer to "+
    45  			"https://github.com/cloudfoundry/cli/wiki/Versioning-Policy#cf-cli-minimum-supported-version",
    46  			map[string]interface{}{
    47  				"APIVersion":          apiVersion,
    48  				"MinSupportedVersion": ccversion.MinSupportedV2ClientVersion,
    49  			})
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func FailIfAPIVersionAboveMaxServiceProviderVersion(apiVersion string) error {
    56  	isTooNew, err := checkVersionNewerThan(apiVersion, ccversion.MaxVersionServiceProviderV2)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	if isTooNew {
    62  		return APIVersionTooHighError{}
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func checkVersionNewerThan(current, maximum string) (bool, error) {
    69  	currentSemver, err := semver.Make(current)
    70  	if err != nil {
    71  		return false, err
    72  	}
    73  
    74  	maximumSemver, err := semver.Make(maximum)
    75  	if err != nil {
    76  		return false, err
    77  	}
    78  
    79  	if currentSemver.Compare(maximumSemver) == 1 {
    80  		return true, nil
    81  	}
    82  
    83  	return false, nil
    84  }
    85  
    86  func CheckVersionOutdated(current string, minimum string) (bool, error) {
    87  	if current == version.DefaultVersion || minimum == "" {
    88  		return false, nil
    89  	}
    90  
    91  	currentSemver, err := semver.Make(current)
    92  	if err != nil {
    93  		return false, err
    94  	}
    95  
    96  	minimumSemver, err := semver.Make(minimum)
    97  	if err != nil {
    98  		return false, err
    99  	}
   100  
   101  	if currentSemver.Compare(minimumSemver) == -1 {
   102  		return true, nil
   103  	}
   104  
   105  	return false, nil
   106  }