
Now that we have most of the services required for use with containerd, it was found that common patterns were used throughout services. By defining a central `errdefs` package, we ensure that services will map errors to and from grpc consistently and cleanly. One can decorate an error with as much context as necessary, using `pkg/errors` and still have the error mapped correctly via grpc. We make a few sacrifices. At this point, the common errors we use across the repository all map directly to grpc error codes. While this seems positively crazy, it actually works out quite well. The error conditions that were specific weren't super necessary and the ones that were necessary now simply have better context information. We lose the ability to add new codes, but this constraint may not be a bad thing. Effectively, as long as one uses the errors defined in `errdefs`, the error class will be mapped correctly across the grpc boundary and everything will be good. If you don't use those definitions, the error maps to "unknown" and the error message is preserved. Signed-off-by: Stephen J Day <stephen.day@docker.com>
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/containerd/containerd/errdefs"
|
|
"github.com/containerd/containerd/log"
|
|
digest "github.com/opencontainers/go-digest"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
var deleteCommand = cli.Command{
|
|
Name: "delete",
|
|
Aliases: []string{"del", "remove", "rm"},
|
|
Usage: "permanently delete one or more blobs.",
|
|
ArgsUsage: "[flags] [<digest>, ...]",
|
|
Description: `Delete one or more blobs permanently. Successfully deleted
|
|
blobs are printed to stdout.`,
|
|
Flags: []cli.Flag{},
|
|
Action: func(context *cli.Context) error {
|
|
var (
|
|
args = []string(context.Args())
|
|
exitError error
|
|
)
|
|
ctx, cancel := appContext(context)
|
|
defer cancel()
|
|
|
|
client, err := getClient(context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cs := client.ContentStore()
|
|
|
|
for _, arg := range args {
|
|
dgst, err := digest.Parse(arg)
|
|
if err != nil {
|
|
if exitError == nil {
|
|
exitError = err
|
|
}
|
|
log.G(ctx).WithError(err).Errorf("could not delete %v", dgst)
|
|
continue
|
|
}
|
|
|
|
if err := cs.Delete(ctx, dgst); err != nil {
|
|
if !errdefs.IsNotFound(err) {
|
|
if exitError == nil {
|
|
exitError = err
|
|
}
|
|
log.G(ctx).WithError(err).Errorf("could not delete %v", dgst)
|
|
}
|
|
continue
|
|
}
|
|
|
|
fmt.Println(dgst)
|
|
}
|
|
|
|
return exitError
|
|
},
|
|
}
|