switch around the WithCheck function

It should be better with an interface. That way it can be written as

ensure.WithCheck(resource)
This commit is contained in:
Gibheer 2020-04-21 21:23:44 +02:00
parent 5e49842e6f
commit ef72359b2d
1 changed files with 10 additions and 5 deletions

15
main.go
View File

@ -17,22 +17,27 @@ type (
Ensure() error
}
// CheckEnsurer is an etension of ensurer, which also provides an Is function
// to check the state of the resource.
CheckEnsurer interface {
Is() bool
Ensure() error
}
checkEnsurer struct {
Is Enforced
State Ensurer
State CheckEnsurer
}
)
// WithCheck creates an Ensurer instance that runs is before Ensure.
func WithCheck(is Enforced, en Ensurer) Ensurer {
func WithCheck(en CheckEnsurer) Ensurer {
return &checkEnsurer{
Is: is,
State: en,
}
}
func (c *checkEnsurer) Ensure() error {
if !c.Is() {
if !c.State.Is() {
return c.State.Ensure()
}
return nil