|
|
|
|
@ -101,6 +101,12 @@ type UserVariable struct {
|
|
|
|
|
key string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A LocalVariable is a variable that references a local value defined within
|
|
|
|
|
// the current module, via a "locals" block. This looks like "${local.foo}".
|
|
|
|
|
type LocalVariable struct {
|
|
|
|
|
Name string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewInterpolatedVariable(v string) (InterpolatedVariable, error) {
|
|
|
|
|
if strings.HasPrefix(v, "count.") {
|
|
|
|
|
return NewCountVariable(v)
|
|
|
|
|
@ -112,6 +118,8 @@ func NewInterpolatedVariable(v string) (InterpolatedVariable, error) {
|
|
|
|
|
return NewTerraformVariable(v)
|
|
|
|
|
} else if strings.HasPrefix(v, "var.") {
|
|
|
|
|
return NewUserVariable(v)
|
|
|
|
|
} else if strings.HasPrefix(v, "local.") {
|
|
|
|
|
return NewLocalVariable(v)
|
|
|
|
|
} else if strings.HasPrefix(v, "module.") {
|
|
|
|
|
return NewModuleVariable(v)
|
|
|
|
|
} else if !strings.ContainsRune(v, '.') {
|
|
|
|
|
@ -331,6 +339,25 @@ func (v *UserVariable) GoString() string {
|
|
|
|
|
return fmt.Sprintf("*%#v", *v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewLocalVariable(key string) (*LocalVariable, error) {
|
|
|
|
|
name := key[len("local."):]
|
|
|
|
|
if idx := strings.Index(name, "."); idx > -1 {
|
|
|
|
|
return nil, fmt.Errorf("Can't use dot (.) attribute access in local.%s; use square bracket indexing", name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &LocalVariable{
|
|
|
|
|
Name: name,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (v *LocalVariable) FullKey() string {
|
|
|
|
|
return fmt.Sprintf("local.%s", v.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (v *LocalVariable) GoString() string {
|
|
|
|
|
return fmt.Sprintf("*%#v", *v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DetectVariables takes an AST root and returns all the interpolated
|
|
|
|
|
// variables that are detected in the AST tree.
|
|
|
|
|
func DetectVariables(root ast.Node) ([]InterpolatedVariable, error) {
|
|
|
|
|
|