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