-
Notifications
You must be signed in to change notification settings - Fork 151
/
utils.go
48 lines (42 loc) · 1.37 KB
/
utils.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package oci contains functions for interacting with artifacts stored in OCI registries.
package oci
import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
)
// IsEmptyDescriptor returns true if the given descriptor is empty.
func IsEmptyDescriptor(desc ocispec.Descriptor) bool {
return desc.Digest == "" && desc.Size == 0
}
// RemoveDuplicateDescriptors removes duplicate descriptors from the given list.
func RemoveDuplicateDescriptors(descriptors []ocispec.Descriptor) []ocispec.Descriptor {
keys := make(map[string]bool)
list := []ocispec.Descriptor{}
for _, entry := range descriptors {
if IsEmptyDescriptor(entry) {
continue
}
if _, value := keys[entry.Digest.Encoded()]; !value {
keys[entry.Digest.Encoded()] = true
list = append(list, entry)
}
}
return list
}
// SumDescsSize returns the size of all the descriptors added together
func SumDescsSize(descs []ocispec.Descriptor) int64 {
var sum int64
for _, layer := range descs {
sum += layer.Size
}
return sum
}
// GetDefaultCopyOpts returns the default copy options
func (o *OrasRemote) GetDefaultCopyOpts() oras.CopyOptions {
copyOpts := oras.DefaultCopyOptions
copyOpts.OnCopySkipped = o.printLayerSkipped
copyOpts.PostCopy = o.printLayerCopied
return copyOpts
}