2023-08-17 15:22:32 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-09-08 15:37:55 +00:00
|
|
|
"strings"
|
2023-08-17 15:22:32 +00:00
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
2023-09-08 15:37:55 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2023-08-17 15:22:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type selectTarget struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectTarget merges the specified target into the root configuration.
|
|
|
|
func SelectTarget(name string) bundle.Mutator {
|
|
|
|
return &selectTarget{
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *selectTarget) Name() string {
|
|
|
|
return fmt.Sprintf("SelectTarget(%s)", m.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *selectTarget) Apply(_ context.Context, b *bundle.Bundle) error {
|
|
|
|
if b.Config.Targets == nil {
|
|
|
|
return fmt.Errorf("no targets defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get specified target
|
|
|
|
target, ok := b.Config.Targets[m.name]
|
|
|
|
if !ok {
|
2023-09-08 15:37:55 +00:00
|
|
|
return fmt.Errorf("%s: no such target. Available targets: %s", m.name, strings.Join(maps.Keys(b.Config.Targets), ", "))
|
2023-08-17 15:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge specified target into root configuration structure.
|
|
|
|
err := b.Config.MergeTargetOverrides(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store specified target in configuration for reference.
|
|
|
|
b.Config.Bundle.Target = m.name
|
|
|
|
|
|
|
|
// We do this for backward compatibility.
|
|
|
|
// TODO: remove when Environments section is not supported anymore.
|
|
|
|
b.Config.Bundle.Environment = b.Config.Bundle.Target
|
|
|
|
|
|
|
|
// Clear targets after loading.
|
|
|
|
b.Config.Targets = nil
|
|
|
|
b.Config.Environments = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|