Skip to content

Commit 1834adc

Browse files
committed
crane: Add "did you mean" to ls and catalog
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]>
1 parent a07d1ca commit 1834adc

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

cmd/crane/cmd/catalog.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import (
1818
"context"
1919
"fmt"
2020
"io"
21+
"os"
2122
"path"
2223

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

cmd/crane/cmd/list.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import (
1818
"context"
1919
"fmt"
2020
"io"
21+
"os"
2122
"strings"
2223

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

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

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

0 commit comments

Comments
 (0)