github.com/sleungcy-sap/cli@v7.1.0+incompatible/cf/requirements/max_api_version.go (about)

     1  package requirements
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"github.com/blang/semver"
     8  
     9  	. "code.cloudfoundry.org/cli/cf/i18n"
    10  )
    11  
    12  type MaxAPIVersionRequirement struct {
    13  	config         coreconfig.Reader
    14  	feature        string
    15  	maximumVersion semver.Version
    16  }
    17  
    18  func NewMaxAPIVersionRequirement(
    19  	config coreconfig.Reader,
    20  	feature string,
    21  	maximumVersion semver.Version,
    22  ) MaxAPIVersionRequirement {
    23  	return MaxAPIVersionRequirement{
    24  		config:         config,
    25  		feature:        feature,
    26  		maximumVersion: maximumVersion,
    27  	}
    28  }
    29  
    30  func (r MaxAPIVersionRequirement) Execute() error {
    31  	if r.config.APIVersion() == "" {
    32  		return errors.New(T("Unable to determine CC API Version. Please log in again."))
    33  	}
    34  
    35  	apiVersion, err := semver.Make(r.config.APIVersion())
    36  	if err != nil {
    37  		return errors.New(T("Unable to parse CC API Version '{{.APIVersion}}'", map[string]interface{}{
    38  			"APIVersion": r.config.APIVersion(),
    39  		}))
    40  	}
    41  
    42  	if apiVersion.GT(r.maximumVersion) {
    43  		return errors.New(T(`{{.Feature}} only works up to CF API version {{.MaximumVersion}}. Your target is {{.APIVersion}}.`,
    44  			map[string]interface{}{
    45  				"APIVersion":     r.config.APIVersion(),
    46  				"Feature":        r.feature,
    47  				"MaximumVersion": r.maximumVersion.String(),
    48  			}))
    49  	}
    50  
    51  	return nil
    52  }