Skip to content

Commit 33e6134

Browse files
committed
komega: add package global functions
allows to use it in a similar way to Gomega
1 parent e91169f commit 33e6134

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

pkg/envtest/komega/default.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package komega
2+
3+
import (
4+
"sigs.k8s.io/controller-runtime/pkg/client"
5+
)
6+
7+
// Default is the Komega used by the package global functions.
8+
// NOTE: Replace it with a custom instance or use WithDefaultClient.
9+
// Otherwise the package global functions will panic.
10+
var Default Komega = &komega{}
11+
12+
// WithDefaultClient replaces the default Komega with a new one that uses the given client.
13+
func WithDefaultClient(c client.Client) {
14+
Default = &komega{client: c}
15+
}
16+
17+
func checkDefaultClient() {
18+
if Default.(*komega).client == nil {
19+
panic("Default Komega's client is not set. Use WithDefaultClient to set it.")
20+
}
21+
}
22+
23+
// Get fetches an object until the forwarded error matches.
24+
func Get(obj client.Object) func() error {
25+
checkDefaultClient()
26+
return Default.Get(obj)
27+
}
28+
29+
// List fetches a list until the forwarded error matches.
30+
func List(obj client.ObjectList, opts ...client.ListOption) func() error {
31+
checkDefaultClient()
32+
return Default.List(obj, opts...)
33+
}
34+
35+
// Update tries to update an object by applying the updateFunc until the forwarded error matches.
36+
func Update(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) func() error {
37+
checkDefaultClient()
38+
return Default.Update(obj, f, opts...)
39+
}
40+
41+
// UpdateStatus tries to update an object's status by applying the updateFunc until the forwarded error matches.
42+
func UpdateStatus(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) func() error {
43+
checkDefaultClient()
44+
return Default.UpdateStatus(obj, f, opts...)
45+
}
46+
47+
// Object
48+
func Object(obj client.Object) func() client.Object {
49+
checkDefaultClient()
50+
return Default.Object(obj)
51+
}
52+
53+
// ObjectList
54+
func ObjectList(obj client.ObjectList, opts ...client.ListOption) func() client.ObjectList {
55+
checkDefaultClient()
56+
return Default.ObjectList(obj, opts...)
57+
}

0 commit comments

Comments
 (0)