func()

in rules/terraform_variable_order.go [62:88]


func (r *TerraformVariableOrderRule) checkVariableOrder(runner tflint.Runner, file *hcl.File) error {
	body, ok := file.Body.(*hclsyntax.Body)
	if !ok {
		logger.Debug("skip terraform_variable_order check since it's not hcl file")
		return nil
	}
	blocks := body.Blocks

	requiredVars := r.getSortedVariableNames(blocks, false)
	optionalVars := r.getSortedVariableNames(blocks, true)
	sortedVariableNames := append(requiredVars, optionalVars...)

	variableNames := r.getVariableNames(blocks)
	if reflect.DeepEqual(variableNames, sortedVariableNames) {
		return nil
	}

	firstRange := r.firstVariableRange(blocks)
	sortedVariableHclTxts := r.sortedVariableCodeTxts(blocks, file, sortedVariableNames)
	sortedVariableHclBytes := hclwrite.Format([]byte(strings.Join(sortedVariableHclTxts, "\n\n")))

	return runner.EmitIssue(
		r,
		fmt.Sprintf("Recommended variable order:\n%s", sortedVariableHclBytes),
		*firstRange,
	)
}