github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/model/version.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  // This is a list of all the current versions including any patches.
    13  // It should be maintained in chronological order with most current
    14  // release at the front of the list.
    15  var versions = []string{
    16  	"5.4.0",
    17  	"5.3.0",
    18  	"5.2.0",
    19  	"5.1.0",
    20  	"5.0.0",
    21  	"4.10.0",
    22  	"4.9.0",
    23  	"4.8.1",
    24  	"4.8.0",
    25  	"4.7.2",
    26  	"4.7.1",
    27  	"4.7.0",
    28  	"4.6.0",
    29  	"4.5.0",
    30  	"4.4.0",
    31  	"4.3.0",
    32  	"4.2.0",
    33  	"4.1.0",
    34  	"4.0.0",
    35  	"3.10.0",
    36  	"3.9.0",
    37  	"3.8.0",
    38  	"3.7.0",
    39  	"3.6.0",
    40  	"3.5.0",
    41  	"3.4.0",
    42  	"3.3.0",
    43  	"3.2.0",
    44  	"3.1.0",
    45  	"3.0.0",
    46  	"2.2.0",
    47  	"2.1.0",
    48  	"2.0.0",
    49  	"1.4.0",
    50  	"1.3.0",
    51  	"1.2.1",
    52  	"1.2.0",
    53  	"1.1.0",
    54  	"1.0.0",
    55  	"0.7.1",
    56  	"0.7.0",
    57  	"0.6.0",
    58  	"0.5.0",
    59  }
    60  
    61  var CurrentVersion string = versions[0]
    62  var BuildNumber string
    63  var BuildDate string
    64  var BuildHash string
    65  var BuildHashEnterprise string
    66  var BuildEnterpriseReady string
    67  var versionsWithoutHotFixes []string
    68  
    69  func init() {
    70  	versionsWithoutHotFixes = make([]string, 0, len(versions))
    71  	seen := make(map[string]string)
    72  
    73  	for _, version := range versions {
    74  		maj, min, _ := SplitVersion(version)
    75  		verStr := fmt.Sprintf("%v.%v.0", maj, min)
    76  
    77  		if seen[verStr] == "" {
    78  			versionsWithoutHotFixes = append(versionsWithoutHotFixes, verStr)
    79  			seen[verStr] = verStr
    80  		}
    81  	}
    82  }
    83  
    84  func SplitVersion(version string) (int64, int64, int64) {
    85  	parts := strings.Split(version, ".")
    86  
    87  	major := int64(0)
    88  	minor := int64(0)
    89  	patch := int64(0)
    90  
    91  	if len(parts) > 0 {
    92  		major, _ = strconv.ParseInt(parts[0], 10, 64)
    93  	}
    94  
    95  	if len(parts) > 1 {
    96  		minor, _ = strconv.ParseInt(parts[1], 10, 64)
    97  	}
    98  
    99  	if len(parts) > 2 {
   100  		patch, _ = strconv.ParseInt(parts[2], 10, 64)
   101  	}
   102  
   103  	return major, minor, patch
   104  }
   105  
   106  func GetPreviousVersion(version string) string {
   107  	verMajor, verMinor, _ := SplitVersion(version)
   108  	verStr := fmt.Sprintf("%v.%v.0", verMajor, verMinor)
   109  
   110  	for index, v := range versionsWithoutHotFixes {
   111  		if v == verStr && len(versionsWithoutHotFixes) > index+1 {
   112  			return versionsWithoutHotFixes[index+1]
   113  		}
   114  	}
   115  
   116  	return ""
   117  }
   118  
   119  func IsCurrentVersion(versionToCheck string) bool {
   120  	currentMajor, currentMinor, _ := SplitVersion(CurrentVersion)
   121  	toCheckMajor, toCheckMinor, _ := SplitVersion(versionToCheck)
   122  
   123  	if toCheckMajor == currentMajor && toCheckMinor == currentMinor {
   124  		return true
   125  	} else {
   126  		return false
   127  	}
   128  }
   129  
   130  func IsPreviousVersionsSupported(versionToCheck string) bool {
   131  	toCheckMajor, toCheckMinor, _ := SplitVersion(versionToCheck)
   132  	versionToCheckStr := fmt.Sprintf("%v.%v.0", toCheckMajor, toCheckMinor)
   133  
   134  	// Current Supported
   135  	if versionsWithoutHotFixes[0] == versionToCheckStr {
   136  		return true
   137  	}
   138  
   139  	// Current - 1 Supported
   140  	if versionsWithoutHotFixes[1] == versionToCheckStr {
   141  		return true
   142  	}
   143  
   144  	// Current - 2 Supported
   145  	if versionsWithoutHotFixes[2] == versionToCheckStr {
   146  		return true
   147  	}
   148  
   149  	// Current - 3 Supported
   150  	if versionsWithoutHotFixes[3] == versionToCheckStr {
   151  		return true
   152  	}
   153  
   154  	return false
   155  }