Skip to content

Commit 47045d5

Browse files
loresusopoiana
authored andcommitted
new(pkg/oci): use functional options for creating repositories
Signed-off-by: Lorenzo Susini <[email protected]>
1 parent c189eb0 commit 47045d5

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

pkg/oci/repository.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2022 The Falco Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package oci
16+
17+
import (
18+
"fmt"
19+
"oras.land/oras-go/v2/registry/remote"
20+
"oras.land/oras-go/v2/registry/remote/auth"
21+
)
22+
23+
// Repository is an HTTP client to a remote repository.
24+
type Repository struct {
25+
*remote.Repository
26+
}
27+
28+
// NewRepository returns a new Repository.
29+
//
30+
// Return an error if the given ref is not valid.
31+
func NewRepository(ref string, options ...func(*Repository)) (*Repository, error) {
32+
repo := &Repository{}
33+
var err error
34+
35+
repo.Repository, err = remote.NewRepository(ref)
36+
if err != nil {
37+
return nil, fmt.Errorf("unable to create new repository with ref %s: %w", ref, err)
38+
}
39+
40+
for _, o := range options {
41+
o(repo)
42+
}
43+
44+
return repo, nil
45+
}
46+
47+
// WithClient sets the underlying HTTP client to be used for requests.
48+
func WithClient(client *auth.Client) func(r *Repository) {
49+
return func(r *Repository) {
50+
r.Client = client
51+
}
52+
}
53+
54+
// WithPlainHTTP specifies if requests should be done in plain http.
55+
func WithPlainHTTP(plainHTTP bool) func(r *Repository) {
56+
return func(r *Repository) {
57+
r.PlainHTTP = plainHTTP
58+
}
59+
}

0 commit comments

Comments
 (0)