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