41 lines
980 B
Go
41 lines
980 B
Go
package version
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func (v Version) String() string {
|
|
return fmt.Sprintf("%d.%d.%d.%d", v.Major, v.Minor, v.Patch, v.Build)
|
|
}
|
|
|
|
func NewVersionFromString(versionString string) (*Version, error) {
|
|
parts := strings.Split(versionString, ".")
|
|
if len(parts) != 5 { // Update length check for new field
|
|
return nil, fmt.Errorf("invalid version string: %s", versionString)
|
|
}
|
|
|
|
major, err := strconv.Atoi(parts[0])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid major version: %s", parts[0])
|
|
}
|
|
|
|
minor, err := strconv.Atoi(parts[1])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid minor version: %s", parts[1])
|
|
}
|
|
|
|
patch, err := strconv.Atoi(parts[2])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid patch version: %s", parts[2])
|
|
}
|
|
|
|
build, err := strconv.Atoi(parts[3])
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid build version: %s", parts[3])
|
|
}
|
|
|
|
return &Version{major, minor, patch, build}, nil // Initialize new field
|
|
}
|