2022-11-28 09:59:43 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-22 14:54:10 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2022-11-28 09:59:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Mutator is the interface type that mutates a bundle's configuration or internal state.
|
|
|
|
// This makes every mutation or action observable and debuggable.
|
|
|
|
type Mutator interface {
|
|
|
|
// Name returns the mutators name.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// Apply mutates the specified bundle object.
|
|
|
|
// It may return a list of mutators to apply immediately after this mutator.
|
|
|
|
// For example: when processing all configuration files in the tree; each file gets
|
|
|
|
// its own mutator instance.
|
|
|
|
Apply(context.Context, *Bundle) ([]Mutator, error)
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:54:10 +00:00
|
|
|
// applyMutator calls apply on the specified mutator given a bundle.
|
|
|
|
// Any mutators this call returns are applied recursively.
|
|
|
|
func applyMutator(ctx context.Context, b *Bundle, m Mutator) error {
|
|
|
|
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("mutator", m.Name()))
|
|
|
|
|
|
|
|
log.Debugf(ctx, "Apply")
|
|
|
|
ms, err := m.Apply(ctx, b)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(ctx, "Error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply recursively.
|
|
|
|
err = Apply(ctx, b, ms)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:59:43 +00:00
|
|
|
func Apply(ctx context.Context, b *Bundle, ms []Mutator) error {
|
|
|
|
if len(ms) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, m := range ms {
|
2023-03-22 14:54:10 +00:00
|
|
|
err := applyMutator(ctx, b, m)
|
2022-11-28 09:59:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|