Skip to content

Commit

Permalink
*: switch away from fmtcompat for non-%w errors
Browse files Browse the repository at this point in the history
For the super-simple case of string errors that don't contain %w, we can
safely use fmt.Errorf or errors.New.

Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar committed Nov 28, 2024
1 parent 9e25793 commit 0817daa
Show file tree
Hide file tree
Showing 26 changed files with 101 additions and 85 deletions.
11 changes: 6 additions & 5 deletions cmd/umoci/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main
import (
"context"
"errors"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -127,12 +128,12 @@ func fromImage(image ispec.Image) (ispec.ImageConfig, mutate.Meta) {
func parseKV(input string) (string, string, error) {
parts := strings.SplitN(input, "=", 2)
if len(parts) != 2 {
return "", "", fmtcompat.Errorf("must contain '=': %s", input)
return "", "", fmt.Errorf("must contain '=': %s", input)
}

name, value := parts[0], parts[1]
if name == "" {
return "", "", fmtcompat.Errorf("must have non-empty name: %s", input)
return "", "", fmt.Errorf("must have non-empty name: %s", input)
}
return name, value, nil
}
Expand Down Expand Up @@ -160,11 +161,11 @@ func config(ctx *cli.Context) error {
return fmtcompat.Errorf("get descriptor: %w", err)
}
if len(fromDescriptorPaths) == 0 {
return fmtcompat.Errorf("tag not found: %s", fromName)
return fmt.Errorf("tag not found: %s", fromName)
}
if len(fromDescriptorPaths) != 1 {
// TODO: Handle this more nicely.
return fmtcompat.Errorf("tag is ambiguous: %s", fromName)
return fmt.Errorf("tag is ambiguous: %s", fromName)
}

mutator, err := mutate.New(engine, fromDescriptorPaths[0])
Expand Down Expand Up @@ -213,7 +214,7 @@ func config(ctx *cli.Context) error {
case "config.entrypoint":
g.ClearConfigEntrypoint()
default:
return fmtcompat.Errorf("unknown key to --clear: %s", key)
return fmt.Errorf("unknown key to --clear: %s", key)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/umoci/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"errors"
"fmt"
"os"

"github.com/apex/log"
Expand Down Expand Up @@ -56,7 +57,7 @@ func initLayout(ctx *cli.Context) error {

if _, err := os.Stat(imagePath); !errors.Is(err, os.ErrNotExist) {
if err == nil {
err = fmtcompat.Errorf("path already exists: %s", imagePath)
err = fmt.Errorf("path already exists: %s", imagePath)
}
return fmtcompat.Errorf("image layout creation: %w", err)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/umoci/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main

import (
"context"
"fmt"
"time"

"github.com/apex/log"
Expand Down Expand Up @@ -87,11 +88,11 @@ Some examples:
numArgs = 1
}
if ctx.NArg() != numArgs {
return fmtcompat.Errorf("invalid number of positional arguments: expected %d", numArgs)
return fmt.Errorf("invalid number of positional arguments: expected %d", numArgs)
}
for idx, args := range ctx.Args() {
if args == "" {
return fmtcompat.Errorf("invalid positional argument %d: arguments cannot be empty", idx)
return fmt.Errorf("invalid positional argument %d: arguments cannot be empty", idx)
}
}

Expand Down Expand Up @@ -134,11 +135,11 @@ func insert(ctx *cli.Context) error {
return fmtcompat.Errorf("get descriptor: %w", err)
}
if len(descriptorPaths) == 0 {
return fmtcompat.Errorf("tag not found: %s", fromName)
return fmt.Errorf("tag not found: %s", fromName)
}
if len(descriptorPaths) != 1 {
// TODO: Handle this more nicely.
return fmtcompat.Errorf("tag is ambiguous: %s", fromName)
return fmt.Errorf("tag is ambiguous: %s", fromName)
}

// Create the mutator.
Expand Down
5 changes: 3 additions & 2 deletions cmd/umoci/raw-add-layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main
import (
"context"
"errors"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -94,11 +95,11 @@ func rawAddLayer(ctx *cli.Context) error {
return fmtcompat.Errorf("get descriptor: %w", err)
}
if len(fromDescriptorPaths) == 0 {
return fmtcompat.Errorf("tag not found: %s", fromName)
return fmt.Errorf("tag not found: %s", fromName)
}
if len(fromDescriptorPaths) != 1 {
// TODO: Handle this more nicely.
return fmtcompat.Errorf("tag is ambiguous: %s", fromName)
return fmt.Errorf("tag is ambiguous: %s", fromName)
}
meta.From = fromDescriptorPaths[0]

Expand Down
9 changes: 5 additions & 4 deletions cmd/umoci/raw-runtime-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main
import (
"context"
"errors"
"fmt"
"os"

"github.com/apex/log"
Expand Down Expand Up @@ -98,11 +99,11 @@ func rawConfig(ctx *cli.Context) error {
return fmtcompat.Errorf("get descriptor: %w", err)
}
if len(fromDescriptorPaths) == 0 {
return fmtcompat.Errorf("tag not found: %s", fromName)
return fmt.Errorf("tag not found: %s", fromName)
}
if len(fromDescriptorPaths) != 1 {
// TODO: Handle this more nicely.
return fmtcompat.Errorf("tag is ambiguous: %s", fromName)
return fmt.Errorf("tag is ambiguous: %s", fromName)
}
meta.From = fromDescriptorPaths[0]

Expand All @@ -113,14 +114,14 @@ func rawConfig(ctx *cli.Context) error {
defer manifestBlob.Close()

if manifestBlob.Descriptor.MediaType != ispec.MediaTypeImageManifest {
return fmtcompat.Errorf("invalid --image tag: descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", manifestBlob.Descriptor.MediaType)
return fmt.Errorf("invalid --image tag: descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", manifestBlob.Descriptor.MediaType)
}

// Get the manifest.
manifest, ok := manifestBlob.Data.(ispec.Manifest)
if !ok {
// Should _never_ be reached.
return fmtcompat.Errorf("[internal error] unknown manifest blob type: %s", manifestBlob.Descriptor.MediaType)
return fmt.Errorf("[internal error] unknown manifest blob type: %s", manifestBlob.Descriptor.MediaType)
}

// Generate the configuration.
Expand Down
9 changes: 5 additions & 4 deletions cmd/umoci/raw-unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main
import (
"context"
"errors"
"fmt"

"github.com/apex/log"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -96,11 +97,11 @@ func rawUnpack(ctx *cli.Context) error {
return fmtcompat.Errorf("get descriptor: %w", err)
}
if len(fromDescriptorPaths) == 0 {
return fmtcompat.Errorf("tag is not found: %s", fromName)
return fmt.Errorf("tag is not found: %s", fromName)
}
if len(fromDescriptorPaths) != 1 {
// TODO: Handle this more nicely.
return fmtcompat.Errorf("tag is ambiguous: %s", fromName)
return fmt.Errorf("tag is ambiguous: %s", fromName)
}
meta.From = fromDescriptorPaths[0]

Expand All @@ -111,7 +112,7 @@ func rawUnpack(ctx *cli.Context) error {
defer manifestBlob.Close()

if manifestBlob.Descriptor.MediaType != ispec.MediaTypeImageManifest {
return fmtcompat.Errorf("invalid --image tag: descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", manifestBlob.Descriptor.MediaType)
return fmt.Errorf("invalid --image tag: descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", manifestBlob.Descriptor.MediaType)
}

log.WithFields(log.Fields{
Expand All @@ -124,7 +125,7 @@ func rawUnpack(ctx *cli.Context) error {
manifest, ok := manifestBlob.Data.(ispec.Manifest)
if !ok {
// Should _never_ be reached.
return fmtcompat.Errorf("[internal error] unknown manifest blob type: %s", manifestBlob.Descriptor.MediaType)
return fmt.Errorf("[internal error] unknown manifest blob type: %s", manifestBlob.Descriptor.MediaType)
}

log.Warnf("unpacking rootfs ...")
Expand Down
3 changes: 2 additions & 1 deletion cmd/umoci/repack.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main
import (
"context"
"errors"
"fmt"
"time"

"github.com/apex/log"
Expand Down Expand Up @@ -108,7 +109,7 @@ func repack(ctx *cli.Context) error {
}).Debugf("umoci: loaded Meta metadata")

if meta.From.Descriptor().MediaType != ispec.MediaTypeImageManifest {
return fmtcompat.Errorf("invalid saved from descriptor: descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", meta.From.Descriptor().MediaType)
return fmt.Errorf("invalid saved from descriptor: descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", meta.From.Descriptor().MediaType)
}

// Get a reference to the CAS.
Expand Down
7 changes: 4 additions & 3 deletions cmd/umoci/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"os"

ispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -80,17 +81,17 @@ func stat(ctx *cli.Context) error {
return fmtcompat.Errorf("get descriptor: %w", err)
}
if len(manifestDescriptorPaths) == 0 {
return fmtcompat.Errorf("tag not found: %s", tagName)
return fmt.Errorf("tag not found: %s", tagName)
}
if len(manifestDescriptorPaths) != 1 {
// TODO: Handle this more nicely.
return fmtcompat.Errorf("tag is ambiguous: %s", tagName)
return fmt.Errorf("tag is ambiguous: %s", tagName)
}
manifestDescriptor := manifestDescriptorPaths[0].Descriptor()

// FIXME: Implement support for manifest lists.
if manifestDescriptor.MediaType != ispec.MediaTypeImageManifest {
return fmtcompat.Errorf("invalid saved from descriptor: descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", manifestDescriptor.MediaType)
return fmt.Errorf("invalid saved from descriptor: descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", manifestDescriptor.MediaType)
}

// Get stat information.
Expand Down
4 changes: 2 additions & 2 deletions cmd/umoci/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func tagAdd(ctx *cli.Context) error {
return fmtcompat.Errorf("get descriptor: %w", err)
}
if len(descriptorPaths) == 0 {
return fmtcompat.Errorf("tag not found: %s", fromName)
return fmt.Errorf("tag not found: %s", fromName)
}
if len(descriptorPaths) != 1 {
// TODO: Handle this more nicely.
return fmtcompat.Errorf("tag is ambiguous: %s", fromName)
return fmt.Errorf("tag is ambiguous: %s", fromName)
}
descriptor := descriptorPaths[0].Descriptor()

Expand Down
19 changes: 10 additions & 9 deletions cmd/umoci/utils_ux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package main

import (
"errors"
"fmt"
"strings"

"github.com/opencontainers/umoci/oci/casext"
"github.com/opencontainers/umoci/pkg/fmtcompat"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -72,7 +73,7 @@ func uxHistory(cmd cli.Command) cli.Command {
if name := flag.GetName(); name == "no-history" {
continue
} else if ctx.IsSet(name) {
return fmtcompat.Errorf("--no-history and --%s may not be specified together", name)
return fmt.Errorf("--no-history and --%s may not be specified together", name)
}
}
}
Expand Down Expand Up @@ -102,10 +103,10 @@ func uxTag(cmd cli.Command) cli.Command {
if ctx.IsSet("tag") {
tag := ctx.String("tag")
if !casext.IsValidReferenceName(tag) {
return fmtcompat.Errorf("invalid --tag: tag contains invalid characters: %q", tag)
return fmt.Errorf("invalid --tag: tag contains invalid characters: %q", tag)
}
if tag == "" {
return fmtcompat.Errorf("invalid --tag: tag is empty")
return errors.New("invalid --tag: tag is empty")
}
ctx.App.Metadata["--tag"] = tag
}
Expand Down Expand Up @@ -149,15 +150,15 @@ func uxImage(cmd cli.Command) cli.Command {

// Verify directory value.
if dir == "" {
return fmtcompat.Errorf("invalid --image: path is empty")
return errors.New("invalid --image: path is empty")
}

// Verify tag value.
if !casext.IsValidReferenceName(tag) {
return fmtcompat.Errorf("invalid --image: tag contains invalid characters: %q", tag)
return fmt.Errorf("invalid --image: tag contains invalid characters: %q", tag)
}
if tag == "" {
return fmtcompat.Errorf("invalid --image: tag is empty")
return errors.New("invalid --image: tag is empty")
}

ctx.App.Metadata["--image-path"] = dir
Expand Down Expand Up @@ -190,10 +191,10 @@ func uxLayout(cmd cli.Command) cli.Command {

// Verify directory value.
if strings.Contains(layout, ":") {
return fmtcompat.Errorf("invalid --layout: path contains ':' character: %q", layout)
return fmt.Errorf("invalid --layout: path contains ':' character: %q", layout)
}
if layout == "" {
return fmtcompat.Errorf("invalid --layout: path is empty")
return errors.New("invalid --layout: path is empty")
}

ctx.App.Metadata["--image-path"] = layout
Expand Down
6 changes: 3 additions & 3 deletions mutate/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (m *Mutator) cache(ctx context.Context) error {
manifest, ok := blob.Data.(ispec.Manifest)
if !ok {
// Should _never_ be reached.
return fmtcompat.Errorf("[internal error] unknown manifest blob type: %s", blob.Descriptor.MediaType)
return fmt.Errorf("[internal error] unknown manifest blob type: %s", blob.Descriptor.MediaType)
}

// Make a copy of the manifest.
Expand All @@ -127,7 +127,7 @@ func (m *Mutator) cache(ctx context.Context) error {
config, ok := blob.Data.(ispec.Image)
if !ok {
// Should _never_ be reached.
return fmtcompat.Errorf("[internal error] unknown config blob type: %s", blob.Descriptor.MediaType)
return fmt.Errorf("[internal error] unknown config blob type: %s", blob.Descriptor.MediaType)
}

// Make a copy of the config and configDescriptor.
Expand All @@ -142,7 +142,7 @@ func (m *Mutator) cache(ctx context.Context) error {
func New(engine cas.Engine, src casext.DescriptorPath) (*Mutator, error) {
// We currently only support changing a given manifest through a walk.
if mt := src.Descriptor().MediaType; mt != ispec.MediaTypeImageManifest {
return nil, fmtcompat.Errorf("unsupported source type: %s", mt)
return nil, fmt.Errorf("unsupported source type: %s", mt)
}

return &Mutator{
Expand Down
3 changes: 1 addition & 2 deletions mutate/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/opencontainers/umoci/oci/cas"
casdir "github.com/opencontainers/umoci/oci/cas/dir"
"github.com/opencontainers/umoci/oci/casext"
"github.com/opencontainers/umoci/pkg/fmtcompat"
)

// These come from just running the code.
Expand Down Expand Up @@ -542,7 +541,7 @@ func walkDescriptorRoot(ctx context.Context, engine casext.Engine, root ispec.De
}

if foundPath == nil {
return casext.DescriptorPath{}, fmtcompat.Errorf("count not find manifest from %s", root.Digest)
return casext.DescriptorPath{}, fmt.Errorf("count not find manifest from %s", root.Digest)
}
return *foundPath, nil
}
Expand Down
3 changes: 2 additions & 1 deletion oci/cas/dir/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -67,7 +68,7 @@ func blobPath(digest digest.Digest) (string, error) {
hash := digest.Hex()

if algo != cas.BlobAlgorithm {
return "", fmtcompat.Errorf("unsupported algorithm: %q", algo)
return "", fmt.Errorf("unsupported algorithm: %q", algo)
}

return filepath.Join(blobDirectory, algo.String(), hash), nil
Expand Down
Loading

0 comments on commit 0817daa

Please sign in to comment.