FIX: Fix bug version parse
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type BinaryVersion struct {
|
||||
@@ -24,45 +23,25 @@ func ParseBinaryVersion(path string) (*BinaryVersion, error) {
|
||||
}
|
||||
|
||||
content := string(data)
|
||||
fmt.Println(content)
|
||||
|
||||
lastDash := strings.LastIndex(content, "-")
|
||||
if lastDash == -1 {
|
||||
return nil, errors.New("no dash found in version string")
|
||||
}
|
||||
|
||||
secondLastDash := strings.LastIndex(content[:lastDash], "-")
|
||||
if secondLastDash == -1 {
|
||||
return nil, errors.New("only one dash found in version string")
|
||||
}
|
||||
|
||||
versionSlice := content[secondLastDash+1 : lastDash]
|
||||
re := regexp.MustCompile(`^([A-Za-z]+)([\d\.]+)$`)
|
||||
matches := re.FindStringSubmatch(versionSlice)
|
||||
if len(matches) < 3 {
|
||||
re := regexp.MustCompile(`([A-Za-z]+)(\d+)(?:\.(\d+))?(?:\.(\d+))?`)
|
||||
matches := re.FindStringSubmatch(content)
|
||||
if len(matches) < 2 {
|
||||
return nil, errors.New("invalid version format")
|
||||
}
|
||||
|
||||
binaryVersion := BinaryVersion{
|
||||
Name: matches[1],
|
||||
}
|
||||
numbers := strings.Split(matches[2], ".")
|
||||
|
||||
if len(numbers) > 0 {
|
||||
binaryVersion.Major, err = strconv.Atoi(numbers[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if matches[2] != "" {
|
||||
binaryVersion.Major, _ = strconv.Atoi(matches[2])
|
||||
}
|
||||
if len(numbers) > 1 {
|
||||
binaryVersion.Minor, err = strconv.Atoi(numbers[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(matches) > 3 && matches[3] != "" {
|
||||
binaryVersion.Minor, _ = strconv.Atoi(matches[3])
|
||||
}
|
||||
if len(numbers) > 2 {
|
||||
binaryVersion.Patch, err = strconv.Atoi(numbers[2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(matches) > 4 && matches[4] != "" {
|
||||
binaryVersion.Patch, _ = strconv.Atoi(matches[4])
|
||||
}
|
||||
|
||||
binaryVersion.Data = data
|
||||
|
||||
Reference in New Issue
Block a user