Skip to content

Commit c0f8fb4

Browse files
authored
ContainerizationOCI: dedupe platform normalizing logic (#683)
We had a couple spots we were using this logic, probably better to shove it in a function. The other thing this fixes is today .current was only checking for arm64 which on a linux host uname with return aarch64, so we'll fatalError trying to use it today..
1 parent 47518e0 commit c0f8fb4

1 file changed

Lines changed: 19 additions & 24 deletions

File tree

Sources/ContainerizationOCI/Platform.swift

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ import Foundation
2121

2222
/// Platform describes the platform which the image in the manifest runs on.
2323
public struct Platform: Sendable, Equatable {
24+
/// Normalizes a raw architecture string (e.g. from uname) to its OCI equivalent.
25+
static func normalizeArch(_ raw: String) -> (arch: String, variant: String?) {
26+
switch raw {
27+
case "aarch64", "arm64":
28+
return ("arm64", "v8")
29+
case "x86_64", "x86-64", "amd64":
30+
return ("amd64", nil)
31+
case "arm", "armhf", "armel":
32+
return ("arm", "v7")
33+
default:
34+
return (raw, nil)
35+
}
36+
}
37+
2438
public static var current: Self {
2539
var systemInfo = utsname()
2640
uname(&systemInfo)
@@ -29,14 +43,8 @@ public struct Platform: Sendable, Equatable {
2943
String(cString: $0)
3044
}
3145
}
32-
switch arch {
33-
case "arm64":
34-
return .init(arch: "arm64", os: "linux", variant: "v8")
35-
case "x86_64":
36-
return .init(arch: "amd64", os: "linux")
37-
default:
38-
fatalError("unsupported arch \(arch)")
39-
}
46+
let normalized = normalizeArch(arch)
47+
return .init(arch: normalized.arch, os: "linux", variant: normalized.variant)
4048
}
4149

4250
/// The computed description, for example, `linux/arm64/v8`.
@@ -48,18 +56,9 @@ public struct Platform: Sendable, Equatable {
4856
return "\(os)/\(architecture)"
4957
}
5058

51-
/// The CPU architecture, for example, `amd64` or `ppc64`.
59+
/// The CPU architecture, for example, `amd64` or `arm64`.
5260
public var architecture: String {
53-
switch _rawArch {
54-
case "arm64", "aarch64":
55-
return "arm64"
56-
case "x86_64", "x86-64", "amd64":
57-
return "amd64"
58-
case "386", "ppc64le", "i386", "s390x", "riscv64":
59-
return _rawArch
60-
default:
61-
return _rawArch
62-
}
61+
Self.normalizeArch(_rawArch).arch
6362
}
6463

6564
/// The operating system, for example, `linux` or `windows`.
@@ -108,11 +107,7 @@ public struct Platform: Sendable, Equatable {
108107
throw ContainerizationError(.invalidArgument, message: "missing OS in \(platform)")
109108
}
110109
switch osValue {
111-
case "linux":
112-
_rawOS = osValue.description
113-
case "darwin":
114-
_rawOS = osValue.description
115-
case "windows":
110+
case "linux", "windows", "darwin":
116111
_rawOS = osValue.description
117112
default:
118113
throw ContainerizationError(.invalidArgument, message: "unknown OS in \(osValue)")

0 commit comments

Comments
 (0)