github.com/databricks/cli@v0.203.0/bundle/config/mutator/select_environment.go (about)

     1  package mutator
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/databricks/cli/bundle"
     8  )
     9  
    10  type selectEnvironment struct {
    11  	name string
    12  }
    13  
    14  // SelectEnvironment merges the specified environment into the root configuration.
    15  func SelectEnvironment(name string) bundle.Mutator {
    16  	return &selectEnvironment{
    17  		name: name,
    18  	}
    19  }
    20  
    21  func (m *selectEnvironment) Name() string {
    22  	return fmt.Sprintf("SelectEnvironment(%s)", m.name)
    23  }
    24  
    25  func (m *selectEnvironment) Apply(_ context.Context, b *bundle.Bundle) error {
    26  	if b.Config.Environments == nil {
    27  		return fmt.Errorf("no environments defined")
    28  	}
    29  
    30  	// Get specified environment
    31  	env, ok := b.Config.Environments[m.name]
    32  	if !ok {
    33  		return fmt.Errorf("%s: no such environment", m.name)
    34  	}
    35  
    36  	// Merge specified environment into root configuration structure.
    37  	err := b.Config.MergeEnvironment(env)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	// Store specified environment in configuration for reference.
    43  	b.Config.Bundle.Environment = m.name
    44  
    45  	// Clear environments after loading.
    46  	b.Config.Environments = nil
    47  	return nil
    48  }