go-KodeStarter/version/version.go

41 lines
980 B
Go
Raw Permalink Normal View History

2023-06-14 23:33:24 -04:00
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])
}
2023-06-17 14:17:02 -04:00
return &Version{major, minor, patch, build}, nil // Initialize new field
}