github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/requirements/cc_api_version.go (about)

     1  package requirements
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/cloudfoundry/cli/cf"
     8  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     9  	. "github.com/cloudfoundry/cli/cf/i18n"
    10  	"github.com/cloudfoundry/cli/cf/terminal"
    11  )
    12  
    13  type CCApiVersionRequirement struct {
    14  	ui          terminal.UI
    15  	config      core_config.Reader
    16  	commandName string
    17  	major       int
    18  	minor       int
    19  	patch       int
    20  }
    21  
    22  func NewCCApiVersionRequirement(ui terminal.UI, config core_config.Reader, commandName string, major, minor, patch int) CCApiVersionRequirement {
    23  	return CCApiVersionRequirement{ui, config, commandName, major, minor, patch}
    24  }
    25  
    26  func (req CCApiVersionRequirement) Execute() bool {
    27  	versions := strings.Split(req.config.ApiVersion(), ".")
    28  
    29  	if len(versions) != 3 {
    30  		return true
    31  	}
    32  
    33  	majorStr := versions[0]
    34  	major, err := strconv.Atoi(majorStr)
    35  	if err != nil {
    36  		return true
    37  	}
    38  
    39  	minorStr := versions[1]
    40  	minor, err := strconv.Atoi(minorStr)
    41  	if err != nil {
    42  		return true
    43  	}
    44  
    45  	patchStr := versions[2]
    46  	patch, err := strconv.Atoi(patchStr)
    47  	if err != nil {
    48  		return true
    49  	}
    50  
    51  	if major > req.major {
    52  		return true
    53  	} else if major < req.major {
    54  		return false
    55  	}
    56  
    57  	if minor > req.minor {
    58  		return true
    59  	} else if minor < req.minor {
    60  		return false
    61  	}
    62  
    63  	if patch >= req.patch {
    64  		return true
    65  	}
    66  
    67  	req.ui.Say(terminal.FailureColor(T("FAILED")))
    68  	req.ui.Say(T("Current CF CLI version {{.Version}}", map[string]interface{}{"Version": cf.Version}))
    69  	req.ui.Say(T("Current CF API version {{.ApiVersion}}", map[string]interface{}{"ApiVersion": req.config.ApiVersion()}))
    70  	req.ui.Say(T("To use the {{.CommandName}} feature, you need to upgrade the CF API to at least {{.MinApiVersionMajor}}.{{.MinApiVersionMinor}}.{{.MinApiVersionPatch}}",
    71  		map[string]interface{}{
    72  			"CommandName":        req.commandName,
    73  			"MinApiVersionMajor": req.major,
    74  			"MinApiVersionMinor": req.minor,
    75  			"MinApiVersionPatch": req.patch,
    76  		}))
    77  
    78  	return false
    79  }