MCPcopy
hub / github.com/regclient/regclient / Parse

Function Parse

types/platform/platform.go:63–129  ·  view source on GitHub ↗

Parse converts a platform string into a struct

(platStr string)

Source from the content-addressed store, hash-verified

61
62// Parse converts a platform string into a struct
63func 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

Callers 15

TestManifestFunction · 0.92
ImageCheckBaseMethod · 0.92
ImageConfigMethod · 0.92
imagePlatformInListFunction · 0.92
ReferrerListMethod · 0.92
TestNewFunction · 0.92
WithAnnotationFunction · 0.92
WithEnvFunction · 0.92
WithLabelFunction · 0.92
TestModFunction · 0.92
TestProcessFunction · 0.92
getPlatformDigestMethod · 0.92

Calls 6

normalizeMethod · 0.95
SplitCSKVFunction · 0.92
knownArchFunction · 0.85
CompatibleFunction · 0.85
variantCompatibleFunction · 0.85
LocalFunction · 0.70

Tested by 6

TestManifestFunction · 0.74
TestNewFunction · 0.74
TestModFunction · 0.74
TestProcessFunction · 0.74
TestReferrerFunction · 0.74
TestPlatformParseFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…