Skip to content

Commit

Permalink
komega: add package global functions
Browse files Browse the repository at this point in the history
allows to use it in a similar way to Gomega
  • Loading branch information
schrej committed Jan 24, 2022
1 parent e91169f commit 33e6134
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/envtest/komega/default.go
@@ -0,0 +1,57 @@
package komega

import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Default is the Komega used by the package global functions.
// NOTE: Replace it with a custom instance or use WithDefaultClient.
// Otherwise the package global functions will panic.
var Default Komega = &komega{}

// WithDefaultClient replaces the default Komega with a new one that uses the given client.
func WithDefaultClient(c client.Client) {
Default = &komega{client: c}
}

func checkDefaultClient() {
if Default.(*komega).client == nil {
panic("Default Komega's client is not set. Use WithDefaultClient to set it.")
}
}

// Get fetches an object until the forwarded error matches.
func Get(obj client.Object) func() error {
checkDefaultClient()
return Default.Get(obj)
}

// List fetches a list until the forwarded error matches.
func List(obj client.ObjectList, opts ...client.ListOption) func() error {
checkDefaultClient()
return Default.List(obj, opts...)
}

// Update tries to update an object by applying the updateFunc until the forwarded error matches.
func Update(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) func() error {
checkDefaultClient()
return Default.Update(obj, f, opts...)
}

// UpdateStatus tries to update an object's status by applying the updateFunc until the forwarded error matches.
func UpdateStatus(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) func() error {
checkDefaultClient()
return Default.UpdateStatus(obj, f, opts...)
}

// Object
func Object(obj client.Object) func() client.Object {
checkDefaultClient()
return Default.Object(obj)
}

// ObjectList
func ObjectList(obj client.ObjectList, opts ...client.ListOption) func() client.ObjectList {
checkDefaultClient()
return Default.ObjectList(obj, opts...)
}

0 comments on commit 33e6134

Please sign in to comment.