github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/strategy/version.go (about)

     1  package strategy
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/cloudfoundry/cli/cf/errors"
     8  	. "github.com/cloudfoundry/cli/cf/i18n"
     9  )
    10  
    11  type Version struct {
    12  	Major int64
    13  	Minor int64
    14  	Patch int64
    15  }
    16  
    17  func ParseVersion(input string) (Version, error) {
    18  	parts := strings.Split(input, ".")
    19  	if len(parts) != 3 {
    20  		return Version{}, errors.NewWithFmt(T("Could not parse version number: {{.Input}}",
    21  			map[string]interface{}{"Input": input}))
    22  	}
    23  
    24  	major, err1 := strconv.ParseInt(parts[0], 10, 64)
    25  	minor, err2 := strconv.ParseInt(parts[1], 10, 64)
    26  	patch, err3 := strconv.ParseInt(parts[2], 10, 64)
    27  	if err1 != nil || err2 != nil || err3 != nil {
    28  		return Version{}, errors.NewWithFmt(T("Could not parse version number: {{.Input}}",
    29  			map[string]interface{}{"Input": input}))
    30  	}
    31  
    32  	return Version{major, minor, patch}, nil
    33  }
    34  
    35  func (version Version) LessThan(other Version) bool {
    36  	if version.Major < other.Major {
    37  		return true
    38  	}
    39  
    40  	if version.Major > other.Major {
    41  		return false
    42  	}
    43  
    44  	if version.Minor < other.Minor {
    45  		return true
    46  	}
    47  
    48  	if version.Minor > other.Minor {
    49  		return false
    50  	}
    51  
    52  	if version.Patch < other.Patch {
    53  		return true
    54  	}
    55  
    56  	return false
    57  }
    58  
    59  func (version Version) GreaterThanOrEqualTo(other Version) bool {
    60  	return !version.LessThan(other)
    61  }