Debian Codegraph @

cmd/soci/commands/internal/platform.go

raw symbols SCIP
1 /*
2 Copyright The Soci Snapshotter Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package internal
18
19 import (
20 "context"
21 "fmt"
22
23 "github.com/containerd/containerd/content"
24 "github.com/containerd/containerd/images"
25 "github.com/containerd/containerd/platforms"
26 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
27 "github.com/urfave/cli"
28 )
29
30 const (
31 PlatformFlagKey = "platform"
32 AllPlatformsFlagKey = "all-platforms"
33 )
34
35 var PlatformFlags = []cli.Flag{
36 cli.BoolFlag{
37 Name: AllPlatformsFlagKey,
38 Usage: "",
39 },
40 cli.StringSliceFlag{
41 Name: PlatformFlagKey + ", p",
42 Usage: "",
43 },
44 }
45
46 // GetPlatforms returns the set of platforms from a cli.Context
47 // The order of preference is:
48 // 1) all platforms supported by the image if the `all-plaforms` flag is set
49 // 2) the set of platforms specified by the `platform` flag
50 // 3) the default platform
51 //
52 // This method is not suitable for situations where the default should be all supported platforms (e.g. the `soci index list` command)
53 func GetPlatforms(ctx context.Context, cliContext *cli.Context, img images.Image, cs content.Store) ([]ocispec.Platform, error) {
54 if cliContext.Bool(AllPlatformsFlagKey) {
55 return images.Platforms(ctx, cs, img.Target)
56 }
57 ps := cliContext.StringSlice(PlatformFlagKey)
58 if len(ps) == 0 {
59 return []ocispec.Platform{platforms.DefaultSpec()}, nil
60 }
61 var result []ocispec.Platform
62 for _, p := range ps {
63 platform, err := platforms.Parse(p)
64 if err != nil {
65 return nil, fmt.Errorf("could not parse platform %s: %w", p, err)
66 }
67 result = append(result, platform)
68 }
69 return result, nil
70 }