ensure/main.go

40 lines
999 B
Go

package ensure
// What we need here is a way to define resources one would like to have.
// Each resource must be checked if it needs to be handled or if it already
// exists.
// The question is, who should do that? Is it the responsibility of the user
// to make sure that resources exist or should it be done "magically"?
type (
// Enforced checks if state is already enforced in the environment. When it
// is, it must return true.
Enforced func() bool
// State represents a wanted state that should be pushed to the environment.
Ensurer interface {
// Ensure will modify the environment so that it represents the wanted
// state.
Ensure() error
}
checkEnsurer struct {
Is Enforced
State Ensurer
}
)
// WithCheck creates an Ensurer instance that runs is before Ensure.
func WithCheck(is Enforced, en Ensurer) Ensurer {
return &checkEnsurer{
Is: is,
State: en,
}
}
func (c *checkEnsurer) Ensure() error {
if !c.Is() {
return c.State.Ensure()
}
return nil
}