github.com/rajatvaryani/mattermost-server@v5.11.1+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.11.0",
    17  	"5.10.0",
    18  	"5.9.0",
    19  	"5.8.0",
    20  	"5.7.0",
    21  	"5.6.0",
    22  	"5.5.0",
    23  	"5.4.0",
    24  	"5.3.0",
    25  	"5.2.0",
    26  	"5.1.0",
    27  	"5.0.0",
    28  	"4.10.0",
    29  	"4.9.0",
    30  	"4.8.1",
    31  	"4.8.0",
    32  	"4.7.2",
    33  	"4.7.1",
    34  	"4.7.0",
    35  	"4.6.0",
    36  	"4.5.0",
    37  	"4.4.0",
    38  	"4.3.0",
    39  	"4.2.0",
    40  	"4.1.0",
    41  	"4.0.0",
    42  	"3.10.0",
    43  	"3.9.0",
    44  	"3.8.0",
    45  	"3.7.0",
    46  	"3.6.0",
    47  	"3.5.0",
    48  	"3.4.0",
    49  	"3.3.0",
    50  	"3.2.0",
    51  	"3.1.0",
    52  	"3.0.0",
    53  	"2.2.0",
    54  	"2.1.0",
    55  	"2.0.0",
    56  	"1.4.0",
    57  	"1.3.0",
    58  	"1.2.1",
    59  	"1.2.0",
    60  	"1.1.0",
    61  	"1.0.0",
    62  	"0.7.1",
    63  	"0.7.0",
    64  	"0.6.0",
    65  	"0.5.0",
    66  }
    67  
    68  var CurrentVersion string = versions[0]
    69  var BuildNumber string
    70  var BuildDate string
    71  var BuildHash string
    72  var BuildHashEnterprise string
    73  var BuildEnterpriseReady string
    74  var versionsWithoutHotFixes []string
    75  
    76  func init() {
    77  	versionsWithoutHotFixes = make([]string, 0, len(versions))
    78  	seen := make(map[string]string)
    79  
    80  	for _, version := range versions {
    81  		maj, min, _ := SplitVersion(version)
    82  		verStr := fmt.Sprintf("%v.%v.0", maj, min)
    83  
    84  		if seen[verStr] == "" {
    85  			versionsWithoutHotFixes = append(versionsWithoutHotFixes, verStr)
    86  			seen[verStr] = verStr
    87  		}
    88  	}
    89  }
    90  
    91  func SplitVersion(version string) (int64, int64, int64) {
    92  	parts := strings.Split(version, ".")
    93  
    94  	major := int64(0)
    95  	minor := int64(0)
    96  	patch := int64(0)
    97  
    98  	if len(parts) > 0 {
    99  		major, _ = strconv.ParseInt(parts[0], 10, 64)
   100  	}
   101  
   102  	if len(parts) > 1 {
   103  		minor, _ = strconv.ParseInt(parts[1], 10, 64)
   104  	}
   105  
   106  	if len(parts) > 2 {
   107  		patch, _ = strconv.ParseInt(parts[2], 10, 64)
   108  	}
   109  
   110  	return major, minor, patch
   111  }
   112  
   113  func GetPreviousVersion(version string) string {
   114  	verMajor, verMinor, _ := SplitVersion(version)
   115  	verStr := fmt.Sprintf("%v.%v.0", verMajor, verMinor)
   116  
   117  	for index, v := range versionsWithoutHotFixes {
   118  		if v == verStr && len(versionsWithoutHotFixes) > index+1 {
   119  			return versionsWithoutHotFixes[index+1]
   120  		}
   121  	}
   122  
   123  	return ""
   124  }
   125  
   126  func IsCurrentVersion(versionToCheck string) bool {
   127  	currentMajor, currentMinor, _ := SplitVersion(CurrentVersion)
   128  	toCheckMajor, toCheckMinor, _ := SplitVersion(versionToCheck)
   129  
   130  	if toCheckMajor == currentMajor && toCheckMinor == currentMinor {
   131  		return true
   132  	} else {
   133  		return false
   134  	}
   135  }
   136  
   137  func IsPreviousVersionsSupported(versionToCheck string) bool {
   138  	toCheckMajor, toCheckMinor, _ := SplitVersion(versionToCheck)
   139  	versionToCheckStr := fmt.Sprintf("%v.%v.0", toCheckMajor, toCheckMinor)
   140  
   141  	// Current Supported
   142  	if versionsWithoutHotFixes[0] == versionToCheckStr {
   143  		return true
   144  	}
   145  
   146  	// Current - 1 Supported
   147  	if versionsWithoutHotFixes[1] == versionToCheckStr {
   148  		return true
   149  	}
   150  
   151  	// Current - 2 Supported
   152  	if versionsWithoutHotFixes[2] == versionToCheckStr {
   153  		return true
   154  	}
   155  
   156  	// Current - 3 Supported
   157  	if versionsWithoutHotFixes[3] == versionToCheckStr {
   158  		return true
   159  	}
   160  
   161  	return false
   162  }