github.com/databricks/cli@v0.203.0/bundle/config/mutator/set_variables.go (about) 1 package mutator 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 8 "github.com/databricks/cli/bundle" 9 "github.com/databricks/cli/bundle/config/variable" 10 ) 11 12 const bundleVarPrefix = "BUNDLE_VAR_" 13 14 type setVariables struct{} 15 16 func SetVariables() bundle.Mutator { 17 return &setVariables{} 18 } 19 20 func (m *setVariables) Name() string { 21 return "SetVariables" 22 } 23 24 func setVariable(v *variable.Variable, name string) error { 25 // case: variable already has value initialized, so skip 26 if v.HasValue() { 27 return nil 28 } 29 30 // case: read and set variable value from process environment 31 envVarName := bundleVarPrefix + name 32 if val, ok := os.LookupEnv(envVarName); ok { 33 err := v.Set(val) 34 if err != nil { 35 return fmt.Errorf(`failed to assign value "%s" to variable %s from environment variable %s with error: %w`, val, name, envVarName, err) 36 } 37 return nil 38 } 39 40 // case: Set the variable to its default value 41 if v.HasDefault() { 42 err := v.Set(*v.Default) 43 if err != nil { 44 return fmt.Errorf(`failed to assign default value from config "%s" to variable %s with error: %w`, *v.Default, name, err) 45 } 46 return nil 47 } 48 49 // We should have had a value to set for the variable at this point. 50 // TODO: use cmdio to request values for unassigned variables if current 51 // terminal is a tty. Tracked in https://github.com/databricks/cli/issues/379 52 return fmt.Errorf(`no value assigned to required variable %s. Assignment can be done through the "--var" flag or by setting the %s environment variable`, name, bundleVarPrefix+name) 53 } 54 55 func (m *setVariables) Apply(ctx context.Context, b *bundle.Bundle) error { 56 for name, variable := range b.Config.Variables { 57 err := setVariable(variable, name) 58 if err != nil { 59 return err 60 } 61 } 62 return nil 63 }