github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/manifest/version.go (about) 1 package manifest 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 fsterr "github.com/fastly/cli/pkg/errors" 9 ) 10 11 // Version represents the currently supported schema for the fastly.toml 12 // manifest file that determines the configuration for a Compute service. 13 // 14 // NOTE: the File object has a field called ManifestVersion which this type is 15 // assigned. The reason we don't name this type ManifestVersion is to appease 16 // the static analysis linter which complains re: stutter in the import 17 // manifest.ManifestVersion. 18 type Version int 19 20 // UnmarshalText manages multiple scenarios where historically the manifest 21 // version was a string value and not an integer. 22 // 23 // Example mappings: 24 // 25 // "0.1.0" -> 1 26 // "1" -> 1 27 // 1 -> 1 28 // "1.0.0" -> 1 29 // 0.1 -> 1 30 // "0.2.0" -> 1 31 // "2.0.0" -> 2 32 // 33 // We also constrain the version so that if a user has a manifest_version 34 // defined as "99.0.0" then we won't accidentally store it as the integer 99 35 // but instead will return an error because it exceeds the current 36 // ManifestLatestVersion version. 37 func (v *Version) UnmarshalText(txt []byte) error { 38 s := string(txt) 39 40 // Presumes semver value (e.g. 1.0.0, 0.1.0 or 0.1) 41 // Major is converted to integer if != zero. 42 // Otherwise if Major == zero, then ignore Minor/Patch and set to latest version. 43 var ( 44 err error 45 version int 46 ) 47 if strings.Contains(s, ".") { 48 segs := strings.Split(s, ".") 49 s = segs[0] 50 if s == "0" { 51 s = strconv.Itoa(ManifestLatestVersion) 52 } 53 } 54 55 version, err = strconv.Atoi(s) 56 if err != nil { 57 return fmt.Errorf("error parsing manifest_version '%s': %w", s, err) 58 } 59 60 if version > ManifestLatestVersion { 61 return fsterr.ErrUnrecognisedManifestVersion 62 } 63 64 *v = Version(version) 65 return nil 66 }