github.com/mlmmr/revel-cmd@v0.21.2-0.20191112133115-68d8795776dd/model/version.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/pkg/errors"
     6  	"regexp"
     7  	"strconv"
     8  )
     9  
    10  type Version struct {
    11  	Prefix      string
    12  	Major       int
    13  	Minor       int
    14  	Maintenance int
    15  	Suffix      string
    16  	BuildDate string
    17  	MinGoVersion string
    18  }
    19  
    20  // The compatibility list
    21  var frameworkCompatibleRangeList = [][]string{
    22  	{"0.0.0", "0.20.0"}, // minimum Revel version to use with this version of the tool
    23  	{"0.19.99", "0.30.0"},  // Compatible with Framework V 0.19.99 - 0.30.0
    24  }
    25  
    26  // Parses a version like v1.2.3a or 1.2
    27  var versionRegExp = regexp.MustCompile(`([^\d]*)?([0-9]*)\.([0-9]*)(\.([0-9]*))?(.*)`)
    28  
    29  // Parse the version and return it as a Version object
    30  func ParseVersion(version string) (v *Version, err error) {
    31  
    32  	v = &Version{}
    33  	return v, v.ParseVersion(version)
    34  }
    35  
    36  // Parse the version and return it as a Version object
    37  func (v *Version)ParseVersion(version string) (err error) {
    38  
    39  	parsedResult := versionRegExp.FindAllStringSubmatch(version, -1)
    40  	if len(parsedResult) != 1 {
    41  		err = errors.Errorf("Invalid version %s", version)
    42  		return
    43  	}
    44  	if len(parsedResult[0]) != 7 {
    45  		err = errors.Errorf("Invalid version %s", version)
    46  		return
    47  	}
    48  
    49  	v.Prefix = parsedResult[0][1]
    50  	v.Major = v.intOrZero(parsedResult[0][2])
    51  	v.Minor = v.intOrZero(parsedResult[0][3])
    52  	v.Maintenance = v.intOrZero(parsedResult[0][5])
    53  	v.Suffix = parsedResult[0][6]
    54  
    55  	return
    56  }
    57  // Returns 0 or an int value for the string, errors are returned as 0
    58  func (v *Version) intOrZero(input string) (value int) {
    59  	if input != "" {
    60  		value, _ = strconv.Atoi(input)
    61  	}
    62  	return value
    63  }
    64  
    65  // Returns true if this major revision is compatible
    66  func (v *Version) CompatibleFramework(c *CommandConfig) error {
    67  	for i, rv := range frameworkCompatibleRangeList {
    68  		start, _ := ParseVersion(rv[0])
    69  		end, _ := ParseVersion(rv[1])
    70  		if !v.Newer(start) || v.Newer(end) {
    71  			continue
    72  		}
    73  		// Framework is older then 0.20, turn on historic mode
    74  		if i == 0 {
    75  			c.HistoricMode = true
    76  		}
    77  		return nil
    78  	}
    79  	return errors.New("Tool out of date - do a 'go get -u github.com/mlmmr/revel-cmd/revel'")
    80  }
    81  
    82  // Returns true if this major revision is newer then the passed in
    83  func (v *Version) MajorNewer(o *Version) bool {
    84  	if v.Major != o.Major {
    85  		return v.Major > o.Major
    86  	}
    87  	return false
    88  }
    89  
    90  // Returns true if this major or major and minor revision is newer then the value passed in
    91  func (v *Version) MinorNewer(o *Version) bool {
    92  	if v.Major != o.Major {
    93  		return v.Major > o.Major
    94  	}
    95  	if v.Minor != o.Minor {
    96  		return v.Minor > o.Minor
    97  	}
    98  	return false
    99  }
   100  
   101  // Returns true if the version is newer then the current on
   102  func (v *Version) Newer(o *Version) bool {
   103  	if v.Major != o.Major {
   104  		return v.Major > o.Major
   105  	}
   106  	if v.Minor != o.Minor {
   107  		return v.Minor > o.Minor
   108  	}
   109  	if v.Maintenance != o.Maintenance {
   110  		return v.Maintenance > o.Maintenance
   111  	}
   112  	return false
   113  }
   114  
   115  // Convert the version to a string
   116  func (v *Version) VersionString() string {
   117  	return fmt.Sprintf("%s%d.%d.%d%s", v.Prefix, v.Major, v.Minor, v.Maintenance, v.Suffix)
   118  }
   119  
   120  // Convert the version build date and go version to a string
   121  func (v *Version) String() string {
   122  	return fmt.Sprintf("Version: %s%d.%d.%d%s\nBuild Date: %s\n Minimium Go Version: %s",
   123  		v.Prefix, v.Major, v.Minor, v.Maintenance, v.Suffix, v.BuildDate, v.MinGoVersion)
   124  }