Parse converts a platform string into a struct
(platStr string)
| 61 | |
| 62 | // Parse converts a platform string into a struct |
| 63 | func Parse(platStr string) (Platform, error) { |
| 64 | // args are a regclient specific way to extend the platform string |
| 65 | platArgs := strings.SplitN(platStr, ",", 2) |
| 66 | // split on slash, validate each component |
| 67 | platSplit := strings.Split(platArgs[0], "/") |
| 68 | for i, part := range platSplit { |
| 69 | if !partRE.MatchString(part) { |
| 70 | return Platform{}, fmt.Errorf("invalid platform component %s in %s%.0w", part, platStr, errs.ErrParsingFailed) |
| 71 | } |
| 72 | platSplit[i] = strings.ToLower(part) |
| 73 | } |
| 74 | plat := &Platform{} |
| 75 | if len(platSplit) == 1 && knownArch(platSplit[0]) { |
| 76 | // special case of architecture only |
| 77 | plat.Architecture = platSplit[0] |
| 78 | } else if len(platSplit) >= 1 { |
| 79 | plat.OS = platSplit[0] |
| 80 | } |
| 81 | if len(platSplit) >= 2 { |
| 82 | plat.Architecture = platSplit[1] |
| 83 | } |
| 84 | if len(platSplit) >= 3 { |
| 85 | plat.Variant = platSplit[2] |
| 86 | } |
| 87 | if len(platArgs) > 1 { |
| 88 | kvMap, err := strparse.SplitCSKV(platArgs[1]) |
| 89 | if err != nil { |
| 90 | return Platform{}, fmt.Errorf("failed to split platform args in %s: %w", platStr, err) |
| 91 | } |
| 92 | for k, v := range kvMap { |
| 93 | k := strings.TrimSpace(k) |
| 94 | v := strings.TrimSpace(v) |
| 95 | switch strings.ToLower(k) { |
| 96 | case "osver", "osversion": |
| 97 | plat.OSVersion = v |
| 98 | default: |
| 99 | return Platform{}, fmt.Errorf("unsupported platform arg type, %s in %s%.0w", k, platStr, errs.ErrParsingFailed) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | // gather local platform details |
| 104 | platLocal := Local() |
| 105 | // normalize and extrapolate missing fields |
| 106 | if platStr == "local" { |
| 107 | *plat = platLocal |
| 108 | } else if plat.OS == "local" || plat.OS == "" { |
| 109 | plat.OS = platLocal.OS |
| 110 | } |
| 111 | plat.normalize() |
| 112 | switch plat.OS { |
| 113 | case "linux", "darwin", "windows": |
| 114 | // expand short references to local platform with architecture and variant |
| 115 | if Compatible(Platform{OS: platLocal.OS}, Platform{OS: plat.OS}) && len(platSplit) < 2 { |
| 116 | if plat.Architecture == "" { |
| 117 | plat.Architecture = platLocal.Architecture |
| 118 | } |
| 119 | if plat.Architecture == platLocal.Architecture && plat.Variant == "" { |
| 120 | plat.Variant = platLocal.Variant |
searching dependent graphs…