-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.go
53 lines (48 loc) · 1.15 KB
/
platform.go
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
package option
import (
"fmt"
"runtime"
"strings"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
// Platform option struct.
type Platform struct {
// request platform in the form of `os[/arch][/variant][:os_version]
platform string
Platform *ocispec.Platform
}
// parse parses the input platform flag to an oci platform type.
func (opts *Platform) Parse() error {
if opts.platform == "" {
return nil
}
// OS[/Arch[/Variant]][:OSVersion]
// If Arch is not provided, will use GOARCH instead
var platformStr string
var p ocispec.Platform
platformStr, p.OSVersion, _ = strings.Cut(opts.platform, ":")
parts := strings.Split(platformStr, "/")
switch len(parts) {
case 3:
p.Variant = parts[2]
fallthrough
case 2:
p.Architecture = parts[1]
case 1:
p.Architecture = runtime.GOARCH
default:
return fmt.Errorf(
"failed to parse platform %q: expected format os[/arch[/variant]]",
opts.platform,
)
}
p.OS = parts[0]
if p.OS == "" {
return fmt.Errorf("invalid platform: OS cannot be empty")
}
if p.Architecture == "" {
return fmt.Errorf("invalid platform: Architecture cannot be empty")
}
opts.Platform = &p
return nil
}