github.com/containerd/nerdctl@v1.7.7/pkg/platformutil/platformutil.go (about)

     1  /*
     2     Copyright The containerd 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 platformutil
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/containerd/nerdctl/pkg/strutil"
    23  	"github.com/containerd/platforms"
    24  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    25  )
    26  
    27  // NewMatchComparerFromOCISpecPlatformSlice returns MatchComparer.
    28  // If platformz is empty, NewMatchComparerFromOCISpecPlatformSlice returns All (not DefaultStrict).
    29  func NewMatchComparerFromOCISpecPlatformSlice(platformz []ocispec.Platform) platforms.MatchComparer {
    30  	if len(platformz) == 0 {
    31  		return platforms.All
    32  	}
    33  	return platforms.Ordered(platformz...)
    34  }
    35  
    36  // NewMatchComparer returns MatchComparer.
    37  // If all is true, NewMatchComparer always returns All, regardless to the value of ss.
    38  // If all is false and ss is empty, NewMatchComparer returns DefaultStrict (not Default).
    39  // Otherwise NewMatchComparer returns Ordered MatchComparer.
    40  func NewMatchComparer(all bool, ss []string) (platforms.MatchComparer, error) {
    41  	if all {
    42  		return platforms.All, nil
    43  	}
    44  	if len(ss) == 0 {
    45  		// return DefaultStrict, not Default
    46  		return platforms.DefaultStrict(), nil
    47  	}
    48  	op, err := NewOCISpecPlatformSlice(false, ss)
    49  	return platforms.Ordered(op...), err
    50  }
    51  
    52  // NewOCISpecPlatformSlice returns a slice of ocispec.Platform
    53  // If all is true, NewOCISpecPlatformSlice always returns an empty slice, regardless to the value of ss.
    54  // If all is false and ss is empty, NewOCISpecPlatformSlice returns DefaultSpec.
    55  // Otherwise NewOCISpecPlatformSlice returns the slice that correspond to ss.
    56  func NewOCISpecPlatformSlice(all bool, ss []string) ([]ocispec.Platform, error) {
    57  	if all {
    58  		return nil, nil
    59  	}
    60  	if dss := strutil.DedupeStrSlice(ss); len(dss) > 0 {
    61  		var op []ocispec.Platform
    62  		for _, s := range dss {
    63  			p, err := platforms.Parse(s)
    64  			if err != nil {
    65  				return nil, fmt.Errorf("invalid platform: %q", s)
    66  			}
    67  			op = append(op, p)
    68  		}
    69  		return op, nil
    70  	}
    71  	return []ocispec.Platform{platforms.DefaultSpec()}, nil
    72  }
    73  
    74  func NormalizeString(s string) (string, error) {
    75  	if s == "" {
    76  		return platforms.DefaultString(), nil
    77  	}
    78  	parsed, err := platforms.Parse(s)
    79  	if err != nil {
    80  		return "", err
    81  	}
    82  	normalized := platforms.Normalize(parsed)
    83  	return platforms.Format(normalized), nil
    84  }