2022-12-15 20:28:14 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2022-12-15 20:28:14 +00:00
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
)
|
|
|
|
|
|
|
|
type selectDefaultEnvironment struct{}
|
|
|
|
|
|
|
|
// SelectDefaultEnvironment merges the default environment into the root configuration.
|
|
|
|
func SelectDefaultEnvironment() bundle.Mutator {
|
|
|
|
return &selectDefaultEnvironment{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *selectDefaultEnvironment) Name() string {
|
|
|
|
return "SelectDefaultEnvironment"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *selectDefaultEnvironment) Apply(_ context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) {
|
|
|
|
if len(b.Config.Environments) == 0 {
|
|
|
|
return nil, fmt.Errorf("no environments defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
// One environment means there's only one default.
|
|
|
|
names := maps.Keys(b.Config.Environments)
|
|
|
|
if len(names) == 1 {
|
|
|
|
return []bundle.Mutator{SelectEnvironment(names[0])}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multiple environments means we look for the `default` flag.
|
|
|
|
var defaults []string
|
2022-12-22 14:31:32 +00:00
|
|
|
for name, env := range b.Config.Environments {
|
|
|
|
if env != nil && env.Default {
|
2022-12-15 20:28:14 +00:00
|
|
|
defaults = append(defaults, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is invalid to have multiple environments with the `default` flag set.
|
|
|
|
if len(defaults) > 1 {
|
|
|
|
return nil, fmt.Errorf("multiple environments are marked as default (%s)", strings.Join(defaults, ", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no environment has the `default` flag set, ask the user to specify one.
|
|
|
|
if len(defaults) == 0 {
|
|
|
|
return nil, fmt.Errorf("please specify environment")
|
|
|
|
}
|
|
|
|
|
|
|
|
// One default remaining.
|
|
|
|
return []bundle.Mutator{SelectEnvironment(defaults[0])}, nil
|
|
|
|
}
|