-
Notifications
You must be signed in to change notification settings - Fork 1
feat: parse and extract tf variables #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements functionality to parse and extract Terraform variables from modules, adding comprehensive support for detecting variables in the coder/coder system as specified in the description.
- Adds a new
variables()
function that extracts variable definitions from root Terraform modules - Implements a
Variable
type with comprehensive metadata including type, default values, and validation - Integrates variable extraction into the main preview pipeline and includes extensive test coverage
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
variables.go | Implements core variable extraction logic from Terraform modules |
types/terraform.go | Defines the Variable struct with JSON serialization support |
extract/variable.go | Contains detailed variable parsing logic with error handling and type conversion |
preview.go | Integrates variable extraction into the main Preview function output |
preview_test.go | Adds comprehensive test coverage for variable extraction functionality |
testdata files | Provides test cases with various variable configurations |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
if typeAttr, exists := attributes["type"]; exists { | ||
ty, def, err := typeAttr.DecodeVarType() | ||
if err != nil { | ||
var subject hcl.Range | ||
if typeAttr.HCLAttribute() != nil { | ||
subject = typeAttr.HCLAttribute().Range | ||
} | ||
return types.Variable{ | ||
Name: block.Label(), | ||
Diagnostics: types.Diagnostics{&hcl.Diagnostic{ | ||
Severity: hcl.DiagError, | ||
Summary: "Failed to decode variable type for " + block.Label(), | ||
Detail: err.Error(), | ||
Subject: &subject, | ||
}}, | ||
} | ||
} | ||
valType = ty | ||
defaults = def | ||
} | ||
|
||
var val cty.Value | ||
var defSubject hcl.Range | ||
if def, exists := attributes["default"]; exists { | ||
val = def.NullableValue() | ||
defSubject = def.HCLAttribute().Range | ||
} | ||
|
||
if valType != cty.NilType { | ||
// TODO: If this code ever extracts the actual value of the variable, | ||
// then we need to source the value from that, rather than the default. | ||
if defaults != nil { | ||
val = defaults.Apply(val) | ||
} | ||
|
||
canConvert := !val.IsNull() && val.IsWhollyKnown() && valType != cty.NilType | ||
|
||
if canConvert { | ||
typedVal, err := convert.Convert(val, valType) | ||
if err != nil { | ||
return types.Variable{ | ||
Name: block.Label(), | ||
Diagnostics: types.Diagnostics{&hcl.Diagnostic{ | ||
Severity: hcl.DiagError, | ||
Summary: fmt.Sprintf("Failed to convert variable default value to type %q for %q", | ||
valType.FriendlyNameForConstraint(), block.Label()), | ||
Detail: err.Error(), | ||
Subject: &defSubject, | ||
}}, | ||
} | ||
} | ||
val = typedVal | ||
} | ||
} else { | ||
valType = val.Type() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is taken from trivy. Unfortunately it is not exported, so I had to copy paste it.
Required for detecting variables in coder/coder