mirror of
https://github.com/kubescape/kubescape.git
synced 2026-02-14 18:09:55 +00:00
Simplify dealing with comments and empty lines at the top
This commit is contained in:
@@ -217,13 +217,6 @@ func (h *FixHandler) getFilePathAndIndex(filePathWithIndex string) (filePath str
|
||||
|
||||
func (h *FixHandler) applyFixToFile(filePath, yamlExpression string) (cmdError error) {
|
||||
|
||||
// While obtaining fixedYamlNode, comments and empty lines at the top are ignored. In order to deal with that,
|
||||
// comments and empty lines are removed and they are inserted again when applying fixes to file.
|
||||
contentAtHead, err := truncateContentAtHead(filePath)
|
||||
|
||||
if err != nil {
|
||||
logger.L().Fatal("Error truncating comments and empty lines at head")
|
||||
}
|
||||
originalYamlNode := constructDecodedYaml(filePath)
|
||||
fixedYamlNode := constructFixedYamlNode(filePath, yamlExpression)
|
||||
|
||||
@@ -232,7 +225,7 @@ func (h *FixHandler) applyFixToFile(filePath, yamlExpression string) (cmdError e
|
||||
|
||||
contentToAdd, linesToRemove := getFixInfo(originalList, fixedList)
|
||||
|
||||
err = applyFixesToFile(filePath, contentToAdd, linesToRemove, contentAtHead)
|
||||
err := applyFixesToFile(filePath, contentToAdd, linesToRemove)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ func testDirectoryApplyFixHelper(t *testing.T, yamlExpressions *[]string, direct
|
||||
|
||||
errorMessage := fmt.Sprintf("Content of fixed %s doesn't match content of %s in %s", originalFile, fixedFile, directoryPath)
|
||||
|
||||
assert.Equal(t, string(tempFileContent), string(fixedFileContent), errorMessage)
|
||||
assert.Equal(t, string(fixedFileContent), string(tempFileContent), errorMessage)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,12 @@ func matchNodes(nodeOne, nodeTwo *yaml.Node) int {
|
||||
}
|
||||
|
||||
func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]LinesToRemove) {
|
||||
|
||||
// While obtaining fixedYamlNode, comments and empty lines at the top are ignored.
|
||||
// This causes a difference in Line numbers across the tree structure. In order to
|
||||
// counter this, line numbers are adjusted in fixed list.
|
||||
adjustFixedListLines(originalList, fixedList)
|
||||
|
||||
contentToAdd := make([]ContentToAdd, 0)
|
||||
linesToRemove := make([]LinesToRemove, 0)
|
||||
|
||||
@@ -209,13 +215,14 @@ func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) {
|
||||
return updatedOriginalTracker, updatedFixedTracker
|
||||
}
|
||||
|
||||
func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemove *[]LinesToRemove, contentAtHead string) error {
|
||||
func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemove *[]LinesToRemove) error {
|
||||
// Read contents of the file line by line and store in a list
|
||||
linesSlice, err := getLinesSlice(filePath)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Clear the current content of file
|
||||
if err := os.Truncate(filePath, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -237,9 +244,6 @@ func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemo
|
||||
writer := bufio.NewWriter(file)
|
||||
lineIdx, lineToAddIdx := 1, 0
|
||||
|
||||
// Insert the comments and lines at the head removed initially.
|
||||
writer.WriteString(contentAtHead)
|
||||
|
||||
// Ideally, new node is inserted at line before the next node in DFS order. But, when the previous line contains a
|
||||
// comment or empty line, we need to insert new nodes before them.
|
||||
adjustContentLines(contentToAdd, &linesSlice)
|
||||
|
||||
@@ -45,6 +45,24 @@ func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) {
|
||||
}
|
||||
}
|
||||
|
||||
func adjustFixedListLines(originalList, fixedList *[]NodeInfo) {
|
||||
differenceAtTop := (*originalList)[0].node.Line - (*fixedList)[0].node.Line
|
||||
|
||||
if differenceAtTop <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, node := range *fixedList {
|
||||
// Line numbers should not be changed for new nodes.
|
||||
if node.node.Line != 0 {
|
||||
node.node.Line += differenceAtTop
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func constructDFSOrderHelper(node *yaml.Node, parent *yaml.Node, dfsOrder *[]NodeInfo, index int) {
|
||||
dfsNode := NodeInfo{
|
||||
node: node,
|
||||
|
||||
Reference in New Issue
Block a user