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 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 { checkEnsurer struct {
Is Enforced State CheckEnsurer
State Ensurer
} }
) )
// WithCheck creates an Ensurer instance that runs is before Ensure. // WithCheck creates an Ensurer instance that runs is before Ensure.
func WithCheck(is Enforced, en Ensurer) Ensurer { func WithCheck(en CheckEnsurer) Ensurer {
return &checkEnsurer{ return &checkEnsurer{
Is: is,
State: en, State: en,
} }
} }
func (c *checkEnsurer) Ensure() error { func (c *checkEnsurer) Ensure() error {
if !c.Is() { if !c.State.Is() {
return c.State.Ensure() return c.State.Ensure()
} }
return nil return nil