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