2022-11-18 09:57:31 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
2022-11-28 09:59:43 +00:00
|
|
|
"context"
|
2022-11-18 09:57:31 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2022-11-18 09:57:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type selectEnvironment struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectEnvironment merges the specified environment into the root configuration.
|
2022-11-28 09:59:43 +00:00
|
|
|
func SelectEnvironment(name string) bundle.Mutator {
|
2022-11-18 09:57:31 +00:00
|
|
|
return &selectEnvironment{
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *selectEnvironment) Name() string {
|
|
|
|
return fmt.Sprintf("SelectEnvironment(%s)", m.name)
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:59:43 +00:00
|
|
|
func (m *selectEnvironment) Apply(_ context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) {
|
|
|
|
if b.Config.Environments == nil {
|
2022-11-18 09:57:31 +00:00
|
|
|
return nil, fmt.Errorf("no environments defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get specified environment
|
2022-11-28 09:59:43 +00:00
|
|
|
env, ok := b.Config.Environments[m.name]
|
2022-11-18 09:57:31 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%s: no such environment", m.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge specified environment into root configuration structure.
|
2022-11-28 09:59:43 +00:00
|
|
|
err := b.Config.MergeEnvironment(env)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:10:13 +00:00
|
|
|
// Store specified environment in configuration for reference.
|
2022-11-28 09:59:43 +00:00
|
|
|
b.Config.Bundle.Environment = m.name
|
2022-11-28 09:10:13 +00:00
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
// Clear environments after loading.
|
2022-11-28 09:59:43 +00:00
|
|
|
b.Config.Environments = nil
|
2022-11-18 09:57:31 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|