git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/useragent/version.go (about)

     1  package useragent
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  type VersionNo struct {
    10  	Major int
    11  	Minor int
    12  	Patch int
    13  }
    14  
    15  func parseVersion(ver string, verno *VersionNo) {
    16  	var err error
    17  	parts := strings.Split(ver, ".")
    18  	if len(parts) > 0 {
    19  		if verno.Major, err = strconv.Atoi(parts[0]); err != nil {
    20  			return
    21  		}
    22  	}
    23  	if len(parts) > 1 {
    24  		if verno.Minor, err = strconv.Atoi(parts[1]); err != nil {
    25  			return
    26  		}
    27  		if len(parts) > 2 {
    28  			if verno.Patch, err = strconv.Atoi(parts[2]); err != nil {
    29  				return
    30  			}
    31  		}
    32  	}
    33  }
    34  
    35  // VersionNoShort return version string in format <Major>.<Minor>
    36  func (ua UserAgent) VersionNoShort() string {
    37  	if ua.VersionNo.Major == 0 && ua.VersionNo.Minor == 0 && ua.VersionNo.Patch == 0 {
    38  		return ""
    39  	}
    40  	return fmt.Sprintf("%d.%d", ua.VersionNo.Major, ua.VersionNo.Minor)
    41  }
    42  
    43  // VersionNoFull returns version string in format <Major>.<Minor>.<Patch>
    44  func (ua UserAgent) VersionNoFull() string {
    45  	if ua.VersionNo.Major == 0 && ua.VersionNo.Minor == 0 && ua.VersionNo.Patch == 0 {
    46  		return ""
    47  	}
    48  	return fmt.Sprintf("%d.%d.%d", ua.VersionNo.Major, ua.VersionNo.Minor, ua.VersionNo.Patch)
    49  }
    50  
    51  // OSVersionNoShort returns OS version string in format <Major>.<Minor>
    52  func (ua UserAgent) OSVersionNoShort() string {
    53  	if ua.OSVersionNo.Major == 0 && ua.OSVersionNo.Minor == 0 && ua.OSVersionNo.Patch == 0 {
    54  		return ""
    55  	}
    56  	return fmt.Sprintf("%d.%d", ua.OSVersionNo.Major, ua.OSVersionNo.Minor)
    57  }
    58  
    59  // OSVersionNoFull returns OS version string in format <Major>.<Minor>.<Patch>
    60  func (ua UserAgent) OSVersionNoFull() string {
    61  	if ua.OSVersionNo.Major == 0 && ua.OSVersionNo.Minor == 0 && ua.OSVersionNo.Patch == 0 {
    62  		return ""
    63  	}
    64  	return fmt.Sprintf("%d.%d.%d", ua.OSVersionNo.Major, ua.OSVersionNo.Minor, ua.OSVersionNo.Patch)
    65  }