1
0
mirror of https://github.com/regclient/regclient.git synced 2025-04-18 22:44:00 +03:00
Brandon Mitchell eea06e2a5c
Refactoring the type package
I feel like I need to explain, this is all to move the descriptor package.
The platform package could not use the predefined errors in types because of a circular dependency from descriptor.
The most appropriate way to reorg this is to move descriptor out of the type package since it was more complex than a self contained type.
When doing that, type aliases were needed to avoid breaking changes to existing users.
Those aliases themselves caused circular dependency loops because of the media types and errors, so those were also pulled out to separate packages.
All of the old values were aliased and deprecated, and to fix the linter, those deprecations were fixed by updating the imports... everywhere.

Signed-off-by: Brandon Mitchell <git@bmitch.net>
2024-03-04 15:43:18 -05:00

96 lines
2.1 KiB
Go

package ocidir
import (
"context"
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/regclient/regclient/scheme"
"github.com/regclient/regclient/types/errs"
"github.com/regclient/regclient/types/mediatype"
"github.com/regclient/regclient/types/ref"
"github.com/regclient/regclient/types/tag"
)
// TagDelete removes a tag from the repository
func (o *OCIDir) TagDelete(ctx context.Context, r ref.Ref) error {
o.mu.Lock()
defer o.mu.Unlock()
return o.tagDelete(ctx, r)
}
func (o *OCIDir) tagDelete(ctx context.Context, r ref.Ref) error {
if r.Tag == "" {
return errs.ErrMissingTag
}
// get index
index, err := o.readIndex(r, true)
if err != nil {
return fmt.Errorf("failed to read index: %w", err)
}
changed := false
for i, desc := range index.Manifests {
if t, ok := desc.Annotations[aOCIRefName]; ok && t == r.Tag {
// remove matching entry from index
index.Manifests = append(index.Manifests[:i], index.Manifests[i+1:]...)
changed = true
}
}
if !changed {
return fmt.Errorf("failed deleting %s: %w", r.CommonName(), errs.ErrNotFound)
}
// push manifest back out
err = o.writeIndex(r, index, true)
if err != nil {
return fmt.Errorf("failed to write index: %w", err)
}
o.refMod(r)
return nil
}
// TagList returns a list of tags from the repository
func (o *OCIDir) TagList(ctx context.Context, r ref.Ref, opts ...scheme.TagOpts) (*tag.List, error) {
// get index
index, err := o.readIndex(r, false)
if err != nil {
return nil, err
}
tl := []string{}
for _, desc := range index.Manifests {
if t, ok := desc.Annotations[aOCIRefName]; ok {
if i := strings.LastIndex(t, ":"); i >= 0 {
t = t[i+1:]
}
found := false
for _, cur := range tl {
if cur == t {
found = true
break
}
}
if !found {
tl = append(tl, t)
}
}
}
sort.Strings(tl)
ib, err := json.Marshal(index)
if err != nil {
return nil, err
}
// return listing from index
t, err := tag.New(
tag.WithRaw(ib),
tag.WithRef(r),
tag.WithMT(mediatype.OCI1ManifestList),
tag.WithLayoutIndex(index),
tag.WithTags(tl),
)
if err != nil {
return nil, err
}
return t, nil
}