github.com/wiselike/revel-cmd@v1.2.1/model/version.go (about)

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