Skip to content

Commit

Permalink
Add --force flag to kit remove
Browse files Browse the repository at this point in the history
Add flag --force to the remove subcommand. If specified, it will result
in modelkits being deleted even if other tags refer to them.
  • Loading branch information
amisevsk committed Feb 29, 2024
1 parent 4d9d785 commit 4f21b69
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions pkg/cmd/remove/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ Description:
The model to be removed may be specifed either by a tag or by a digest. If
specified by digest, that modelkit will be removed along with any tags that
might refer to it. If specified by tag, the modelkit will only be removed if
no other tags refer to it; otherwise it is only untagged.`
might refer to it. If specified by tag (and the --force flag is not used),
the modelkit will only be removed if no other tags refer to it; otherwise
it is only untagged.`

examples = ` kit remove my-registry.com/my-org/my-repo:my-tag
kit remove my-registry.com/my-org/my-repo@sha256:<digest>
kit remove my-registry.com/my-org/my-repo:tag1,tag2,tag3`
)

type removeOptions struct {
configHome string
modelRef *registry.Reference
extraTags []string
configHome string
forceDelete bool
modelRef *registry.Reference
extraTags []string
}

func (opts *removeOptions) complete(ctx context.Context, args []string) error {
Expand Down Expand Up @@ -63,6 +65,7 @@ func RemoveCommand() *cobra.Command {
Run: runCommand(opts),
}
cmd.Args = cobra.ExactArgs(1)
cmd.Flags().BoolVarP(&opts.forceDelete, "force", "f", false, "remove manifest even if other tags refer to it")
return cmd
}

Expand All @@ -76,7 +79,7 @@ func runCommand(opts *removeOptions) func(*cobra.Command, []string) {
if err != nil {
output.Fatalf("Failed to read local storage: %s", storageRoot)
}
desc, err := removeModel(cmd.Context(), localStore, opts.modelRef)
desc, err := removeModel(cmd.Context(), localStore, opts.modelRef, opts.forceDelete)
if err != nil {
output.Fatalf("Failed to remove: %s", err)
}
Expand All @@ -85,11 +88,12 @@ func runCommand(opts *removeOptions) func(*cobra.Command, []string) {
for _, tag := range opts.extraTags {
ref := *opts.modelRef
ref.Reference = tag
desc, err := removeModel(cmd.Context(), localStore, &ref)
desc, err := removeModel(cmd.Context(), localStore, &ref, opts.forceDelete)
if err != nil {
output.Errorf("Failed to remove: %s", err)
} else {
output.Infof("Removed %s (digest %s)", ref.String(), desc.Digest)
}
output.Infof("Removed %s (digest %s)", ref.String(), desc.Digest)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"oras.land/oras-go/v2/registry"
)

func removeModel(ctx context.Context, store repo.LocalStorage, ref *registry.Reference) (ocispec.Descriptor, error) {
func removeModel(ctx context.Context, store repo.LocalStorage, ref *registry.Reference, forceDelete bool) (ocispec.Descriptor, error) {
desc, err := oras.Resolve(ctx, store, ref.Reference, oras.ResolveOptions{})
if err != nil {
if err == errdef.ErrNotFound {
Expand All @@ -23,7 +23,7 @@ func removeModel(ctx context.Context, store repo.LocalStorage, ref *registry.Ref
}

// If reference passed in is a digest, remove the manifest ignoring any tags the manifest might have
if err := ref.ValidateReferenceAsDigest(); err == nil {
if err := ref.ValidateReferenceAsDigest(); err == nil || forceDelete {
output.Debugf("Deleting manifest with digest %s", ref.Reference)
if err := store.Delete(ctx, desc); err != nil {
return ocispec.DescriptorEmptyJSON, fmt.Errorf("failed to delete model: %ws", err)
Expand Down

0 comments on commit 4f21b69

Please sign in to comment.