File: platform.go

package info (click to toggle)
golang-github-containers-common 0.62.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,436 kB
  • sloc: makefile: 131; sh: 102
file content (57 lines) | stat: -rw-r--r-- 1,561 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package platform

import (
	"fmt"
	"runtime"

	"github.com/containerd/platforms"
	v1 "github.com/opencontainers/image-spec/specs-go/v1"
	"github.com/sirupsen/logrus"
)

// Normalize normalizes (according to the OCI spec) the specified os,
// arch and variant.  If left empty, the individual item will be normalized.
func Normalize(rawOS, rawArch, rawVariant string) (os, arch, variant string) {
	platformSpec := v1.Platform{
		OS:           rawOS,
		Architecture: rawArch,
		Variant:      rawVariant,
	}
	normalizedSpec := platforms.Normalize(platformSpec)
	if normalizedSpec.Variant == "" && rawVariant != "" {
		normalizedSpec.Variant = rawVariant
	}
	rawPlatform := ToString(normalizedSpec.OS, normalizedSpec.Architecture, normalizedSpec.Variant)
	normalizedPlatform, err := platforms.Parse(rawPlatform)
	if err != nil {
		logrus.Debugf("Error normalizing platform: %v", err)
		return rawOS, rawArch, rawVariant
	}
	logrus.Debugf("Normalized platform %s to %s", rawPlatform, normalizedPlatform)
	os = rawOS
	if rawOS != "" {
		os = normalizedPlatform.OS
	}
	arch = rawArch
	if rawArch != "" {
		arch = normalizedPlatform.Architecture
	}
	variant = rawVariant
	if rawVariant != "" || (rawVariant == "" && normalizedPlatform.Variant != "") {
		variant = normalizedPlatform.Variant
	}
	return os, arch, variant
}

func ToString(os, arch, variant string) string {
	if os == "" {
		os = runtime.GOOS
	}
	if arch == "" {
		arch = runtime.GOARCH
	}
	if variant == "" {
		return fmt.Sprintf("%s/%s", os, arch)
	}
	return fmt.Sprintf("%s/%s/%s", os, arch, variant)
}