-
Notifications
You must be signed in to change notification settings - Fork 380
Add JSON configuration file support #2391
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
Open
rorychatterton
wants to merge
1
commit into
terraform-linters:master
Choose a base branch
from
wayvz-io:09-24-feature_-_json_configuration_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
config { | ||
format = "compact" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"config": { | ||
"format": "checkstyle" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"config": { | ||
"format": "checkstyle" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package inspection | ||
|
||
import ( | ||
"bytes" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package plugin | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/terraform-linters/tflint/tflint" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// Version constraints for plugin compatibility | ||
var ( | ||
// DefaultSDKVersionConstraints is the minimum SDK version for basic functionality | ||
DefaultSDKVersionConstraints = version.MustConstraints(version.NewConstraint(">= 0.16.0")) | ||
|
||
// JSONConfigSDKVersionConstraints is the minimum SDK version for JSON configuration support | ||
JSONConfigSDKVersionConstraints = version.MustConstraints(version.NewConstraint(">= 0.23.0")) | ||
|
||
// EphemeralMarksMinVersion is when ephemeral marks support was added | ||
EphemeralMarksMinVersion = version.Must(version.NewVersion("0.22.0")) | ||
) | ||
|
||
// CheckTFLintVersionConstraints validates if TFLint version meets the plugin's requirements | ||
func CheckTFLintVersionConstraints(pluginName string, constraints version.Constraints) error { | ||
if !constraints.Check(tflint.Version) { | ||
return fmt.Errorf("Failed to satisfy version constraints; tflint-ruleset-%s requires %s, but TFLint version is %s", pluginName, constraints, tflint.Version) | ||
} | ||
return nil | ||
} | ||
|
||
// CheckSDKVersionSatisfiesConstraints validates if a plugin's SDK version meets the minimum requirements. | ||
// For HCL configs, requires SDK >= 0.16.0. For JSON configs, requires SDK >= 0.23.0. | ||
func CheckSDKVersionSatisfiesConstraints(pluginName string, sdkVersion *version.Version, isJSONConfig bool) error { | ||
// If sdkVersion is nil, the plugin doesn't have SDKVersion endpoint (SDK < 0.14) | ||
if sdkVersion == nil { | ||
return fmt.Errorf(`Plugin "%s" SDK version is incompatible. Compatible versions: %s`, pluginName, DefaultSDKVersionConstraints) | ||
} | ||
|
||
constraints := DefaultSDKVersionConstraints | ||
if isJSONConfig { | ||
constraints = JSONConfigSDKVersionConstraints | ||
} | ||
|
||
if !constraints.Check(sdkVersion) { | ||
if isJSONConfig { | ||
return fmt.Errorf(`Plugin "%s" SDK version (%s) is incompatible with JSON configuration. Minimum required: %s`, pluginName, sdkVersion, JSONConfigSDKVersionConstraints) | ||
} | ||
return fmt.Errorf(`Plugin "%s" SDK version (%s) is incompatible. Compatible versions: %s`, pluginName, sdkVersion, DefaultSDKVersionConstraints) | ||
} | ||
return nil | ||
} | ||
|
||
// SupportsEphemeralMarks checks if the plugin SDK version supports ephemeral marks | ||
func SupportsEphemeralMarks(sdkVersion *version.Version) bool { | ||
if sdkVersion == nil { | ||
return false | ||
} | ||
return sdkVersion.GreaterThanOrEqual(EphemeralMarksMinVersion) | ||
} | ||
|
||
// IsSDKVersionUnimplemented checks if an error indicates the SDK version endpoint is not implemented | ||
func IsSDKVersionUnimplemented(err error) bool { | ||
if st, ok := status.FromError(err); ok { | ||
return st.Code() == codes.Unimplemented | ||
} | ||
return false | ||
} | ||
|
||
// IsVersionConstraintsUnimplemented checks if an error indicates the version constraints endpoint is not implemented | ||
func IsVersionConstraintsUnimplemented(err error) bool { | ||
if st, ok := status.FromError(err); ok { | ||
return st.Code() == codes.Unimplemented | ||
} | ||
return false | ||
} | ||
|
||
// ValidatePluginVersions checks plugin SDK version requirements and returns SDK versions for later use | ||
// Note: TFLint version constraints are checked separately in launchPlugins before ApplyGlobalConfig | ||
func ValidatePluginVersions(rulesetPlugin *Plugin, isJSONConfig bool) (map[string]*version.Version, error) { | ||
sdkVersions := map[string]*version.Version{} | ||
|
||
for name, ruleset := range rulesetPlugin.RuleSets { | ||
// Get SDK version | ||
sdkVersion, err := ruleset.SDKVersion() | ||
if err != nil { | ||
if IsSDKVersionUnimplemented(err) { | ||
// SDKVersion endpoint is available in tflint-plugin-sdk v0.14+. | ||
// Plugin is too old, treat as nil | ||
sdkVersion = nil | ||
} else { | ||
return nil, fmt.Errorf(`Failed to get plugin "%s" SDK version; %w`, name, err) | ||
} | ||
} | ||
|
||
// Check if SDK version meets minimum requirements | ||
if err := CheckSDKVersionSatisfiesConstraints(name, sdkVersion, isJSONConfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
sdkVersions[name] = sdkVersion | ||
} | ||
|
||
return sdkVersions, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.