github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/requirements/min_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 MinAPIVersionRequirement struct {
    13  	config          coreconfig.Reader
    14  	feature         string
    15  	requiredVersion semver.Version
    16  }
    17  
    18  func NewMinAPIVersionRequirement(
    19  	config coreconfig.Reader,
    20  	feature string,
    21  	requiredVersion semver.Version,
    22  ) MinAPIVersionRequirement {
    23  	return MinAPIVersionRequirement{
    24  		config:          config,
    25  		feature:         feature,
    26  		requiredVersion: requiredVersion,
    27  	}
    28  }
    29  
    30  func (r MinAPIVersionRequirement) 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.LT(r.requiredVersion) {
    43  		return errors.New(T(`{{.Feature}} requires CF API version {{.RequiredVersion}} or higher. Your target is {{.APIVersion}}.`,
    44  			map[string]interface{}{
    45  				"APIVersion":      r.config.APIVersion(),
    46  				"Feature":         r.feature,
    47  				"RequiredVersion": r.requiredVersion.String(),
    48  			}))
    49  	}
    50  
    51  	return nil
    52  }