Skip to content

Commit

Permalink
crane: Add "did you mean" to ls and catalog
Browse files Browse the repository at this point in the history
People often try to run `crane ls` on a registry and `crane catalog` on
a repo, neither of which will work. Attempt to nudge them in the right
direction if we hit an error.

Signed-off-by: Jon Johnson <[email protected]>
  • Loading branch information
jonjohnsonjr committed Aug 27, 2024
1 parent a07d1ca commit 1834adc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/crane/cmd/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"context"
"fmt"
"io"
"os"
"path"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/spf13/cobra"
Expand All @@ -47,6 +49,9 @@ func NewCmdCatalog(options *[]crane.Option, _ ...string) *cobra.Command {
func catalog(ctx context.Context, w io.Writer, src string, fullRef bool, o crane.Options) error {
reg, err := name.NewRegistry(src, o.Name...)
if err != nil {
if repo, err := name.NewRepository(src, o.Name...); err == nil {
logs.Warn.Printf("did you mean '%s catalog %s'?", os.Args[0], repo.RegistryStr())
}
return fmt.Errorf("parsing reg %q: %w", src, err)
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/crane/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"context"
"fmt"
"io"
"os"
"strings"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/spf13/cobra"
Expand All @@ -47,6 +49,9 @@ func NewCmdList(options *[]crane.Option) *cobra.Command {
func list(ctx context.Context, w io.Writer, src string, fullRef, omitDigestTags bool, o crane.Options) error {
repo, err := name.NewRepository(src, o.Name...)
if err != nil {
if _, err := name.NewRegistry(src, o.Name...); err == nil {
logs.Warn.Printf("did you mean '%s catalog %s'?", os.Args[0], src)
}
return fmt.Errorf("parsing repo %q: %w", src, err)
}

Expand All @@ -57,6 +62,9 @@ func list(ctx context.Context, w io.Writer, src string, fullRef, omitDigestTags

lister, err := puller.Lister(ctx, repo)
if err != nil {
if _, err := name.NewRegistry(src, o.Name...); err == nil {
logs.Warn.Printf("did you mean '%s catalog %s'?", os.Args[0], src)
}
return fmt.Errorf("reading tags for %s: %w", repo, err)
}

Expand Down

0 comments on commit 1834adc

Please sign in to comment.