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

Add example for listing repositories in registry target #117

Merged
merged 15 commits into from
Mar 25, 2022
79 changes: 79 additions & 0 deletions registry/registry_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright The ORAS Authors.
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 registry_test gives examples code of functions in the registry package.
package registry_test

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

"oras.land/oras-go/v2/registry"
"oras.land/oras-go/v2/registry/remote"
)

func testRegistry() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result := struct {
Repositories []string `json:"repositories"`
}{
Repositories: []string{"public/repo1", "public/repo2", "internal/repo3"},
}
json.NewEncoder(w).Encode(result)
}))
}

var exampleReg *remote.Registry

func TestMain(m *testing.M) {
// Mocking local registry
ts := testRegistry()
defer ts.Close()
exampleUri, err := url.Parse(ts.URL)
if err != nil {
panic(err)
}
exampleReg, err = remote.NewRegistry(exampleUri.Host) // Create a registry via the remote host
if err != nil {
panic(err) // Handle error
}
exampleReg.PlainHTTP = true // Use HTTP
os.Exit(m.Run())
}

// ExampleRepositories gives example snippets for listing respositories in the registry without pagination.
func ExampleRepositories() {
// Example: List repositories in a registry
ctx := context.Background()

repos, err := registry.Repositories(ctx, exampleReg)
qweeah marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err) // Handle error
}

for _, repo := range repos {
fmt.Println(repo)
}
// Output:
// public/repo1
// public/repo2
// internal/repo3
}
74 changes: 74 additions & 0 deletions registry/remote/registry_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright The ORAS Authors.
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 registry_test gives examples code of functions in the remote package.
qweeah marked this conversation as resolved.
Show resolved Hide resolved
package remote_test
qweeah marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

"oras.land/oras-go/v2/registry/remote"
)

var exampleRegistry *remote.Registry

func TestMain(m *testing.M) {
// Mocking local registry
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result := struct {
Repositories []string `json:"repositories"`
}{
Repositories: []string{"public/repo1", "public/repo2", "internal/repo3"},
}
json.NewEncoder(w).Encode(result)
}))
defer ts.Close()
exampleUri, err := url.Parse(ts.URL)
if err != nil {
panic(err)
}
exampleRegistry, err = remote.NewRegistry(exampleUri.Host) // Create a registry via the remote host
if err != nil {
panic(err) // Handle error
}
exampleRegistry.PlainHTTP = true // Use HTTP
qweeah marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(m.Run())
}

// ExampleRegistry_Repositories gives example snippets for listing respositories in the registry with pagination.
func ExampleRegistry_Repositories() {
fn := func(repos []string) error { // Setup a callback function to process returned repository list
for _, repo := range repos {
fmt.Println(repo)
}
return nil
}
qweeah marked this conversation as resolved.
Show resolved Hide resolved

ctx := context.Background()
err := exampleRegistry.Repositories(ctx, fn)
qweeah marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
//handle it
panic(err)
}
// Output:
// public/repo1
// public/repo2
// internal/repo3
}