Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds support for the /v2/_catalog API #548

Merged
merged 10 commits into from
Oct 25, 2019
45 changes: 45 additions & 0 deletions cmd/crane/cmd/catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"log"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/spf13/cobra"
)

func init() { Root.AddCommand(NewCmdCatalog()) }

// NewCmdCatalog creates a new cobra.Command for the repos subcommand.
func NewCmdCatalog() *cobra.Command {
return &cobra.Command{
Use: "catalog",
Short: "List the repos in a registry",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
reg := args[0]
repos, err := crane.Catalog(reg)
if err != nil {
log.Fatalf("reading repos for %s: %v", reg, err)
}

for _, repo := range repos {
fmt.Println(repo)
}
},
}
}
1 change: 1 addition & 0 deletions cmd/crane/doc/crane.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions cmd/crane/doc/crane_catalog.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions hack/update-code-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export GOPATH=$(go env GOPATH)
export PATH="${GOPATH}/bin:${PATH}"


go install ./vendor/k8s.io/code-generator/cmd/deepcopy-gen
go install ./vendor/github.com/maxbrunsfeld/counterfeiter
go install k8s.io/code-generator/cmd/deepcopy-gen
go install github.com/maxbrunsfeld/counterfeiter
go run $PROJECT_ROOT/cmd/crane/help/main.go --dir=$PROJECT_ROOT/cmd/crane/doc/
go generate ./...
49 changes: 49 additions & 0 deletions pkg/crane/catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2019 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package crane

import (
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
)

// Catalog returns the repositories in a registry's catalog.
func Catalog(src string) (res []string, err error) {
reg, err := name.NewRegistry(src)
if err != nil {
return nil, err
}

n := 100
last := ""
for {
page, err := remote.CatalogPage(reg, last, n, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return nil, err
}

if len(page) > 0 {
last = page[len(page)-1]
res = append(res, page...)
}

if len(page) < n {
break
}
}

return res, nil
}
Loading