...

Source file src/github.com/linkerd/linkerd2/pkg/version/channelversion.go

Documentation: github.com/linkerd/linkerd2/pkg/version

     1  package version
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // channelVersion is a low-level struct for handling release channels in a
    10  // structured way. It has no dependencies on the rest of the version package.
    11  type channelVersion struct {
    12  	channel  string
    13  	version  string
    14  	hotpatch *int64
    15  	original string
    16  }
    17  
    18  // hotpatchSuffix is the suffix applied to channel names to indicate that the
    19  // version string includes a hotpatch number (e.g. dev-0.1.2-3)
    20  const hotpatchSuffix = "Hotpatch"
    21  
    22  func (cv channelVersion) String() string {
    23  	return cv.original
    24  }
    25  
    26  // updateChannel returns the channel name to check for updates. if there's no
    27  // hotpatch number set, then it returns the channel name itself. otherwise it
    28  // returns the channel name suffixed with "Hotpatch" to indicate that a separate
    29  // update channel should be used.
    30  func (cv channelVersion) updateChannel() string {
    31  	if cv.hotpatch != nil {
    32  		return cv.channel + hotpatchSuffix
    33  	}
    34  	return cv.channel
    35  }
    36  
    37  // versionWithHotpatch returns the version string, suffixed with the hotpatch
    38  // number if it exists.
    39  func (cv channelVersion) versionWithHotpatch() string {
    40  	if cv.hotpatch == nil {
    41  		return cv.version
    42  	}
    43  	return fmt.Sprintf("%s-%d", cv.version, *cv.hotpatch)
    44  }
    45  
    46  func (cv channelVersion) hotpatchEqual(other channelVersion) bool {
    47  	if cv.hotpatch == nil && other.hotpatch == nil {
    48  		return true
    49  	}
    50  	if cv.hotpatch == nil || other.hotpatch == nil {
    51  		return false
    52  	}
    53  	return *cv.hotpatch == *other.hotpatch
    54  }
    55  
    56  // parseChannelVersion parses a build string into a channelVersion struct. it
    57  // expects the channel and version to be separated by a hyphen (e.g. dev-0.1.2).
    58  // the version may additionally include a hotpatch number, which is separated
    59  // from the base version by another hyphen (e.g. dev-0.1.2-3). if the version is
    60  // suffixed with any other non-numeric build info strings (e.g. dev-0.1.2-foo),
    61  // those strings are ignored.
    62  func parseChannelVersion(cv string) (channelVersion, error) {
    63  	parts := strings.Split(cv, "-")
    64  	if len(parts) < 2 {
    65  		return channelVersion{}, fmt.Errorf("unsupported version format: %s", cv)
    66  	}
    67  
    68  	channel := parts[0]
    69  	version := parts[1]
    70  	var hotpatch *int64
    71  
    72  	for _, part := range parts[2:] {
    73  		if i, err := strconv.ParseInt(part, 10, 64); err == nil {
    74  			hotpatch = &i
    75  			break
    76  		}
    77  	}
    78  
    79  	return channelVersion{channel, version, hotpatch, cv}, nil
    80  }
    81  
    82  // IsReleaseChannel returns true if the channel of the version is "edge" or
    83  // "stable".
    84  func IsReleaseChannel(version string) (bool, error) {
    85  	cv, err := parseChannelVersion(version)
    86  	if err != nil {
    87  		return false, err
    88  	}
    89  	return cv.channel == "edge" || cv.channel == "stable", nil
    90  }
    91  

View as plain text