Small Changes

This commit is contained in:
suhasgumma
2022-12-09 23:44:04 +05:30
parent f72cb215d7
commit f64200f42f
8 changed files with 39 additions and 24 deletions
+10 -16
View File
@@ -6,6 +6,7 @@ import (
"container/list"
"fmt"
"io/ioutil"
"math"
"os"
logger "github.com/kubescape/go-logger"
@@ -132,7 +133,7 @@ func getFixInfo(originalList, fixedList *[]NodeInfo) (*[]ContentToAdd, *[]Conten
}
for fixedListTracker < len(*fixedList) {
fixInfoMetadata.originalListTracker = len(*originalList) - 1
fixInfoMetadata.originalListTracker = -1
fixInfoMetadata.fixedListTracker = fixedListTracker
fixedListTracker = addLinesToInsert(fixInfoMetadata)
}
@@ -156,7 +157,14 @@ func addLinesToRemove(fixInfoMetadata *FixInfoMetadata) int {
// Adds the lines to insert and returns the updated fixedListTracker
func addLinesToInsert(fixInfoMetadata *FixInfoMetadata) int {
currentDFSNode := (*fixInfoMetadata.fixedList)[fixInfoMetadata.fixedListTracker]
lineToInsert := (*fixInfoMetadata.originalList)[fixInfoMetadata.originalListTracker].node.Line - 1
var lineToInsert int
if fixInfoMetadata.originalListTracker == -1 {
lineToInsert = int(math.Inf(1))
} else {
lineToInsert = (*fixInfoMetadata.originalList)[fixInfoMetadata.originalListTracker].node.Line - 1
}
contentToInsert := getContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker)
newTracker := updateTracker(fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker)
@@ -184,20 +192,6 @@ func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) {
return updatedOriginalTracker, updatedFixedTracker
}
// Line numbers are readjusted such that there are no empty lines or comment lines before them
func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) {
for contentIdx, content := range *contentToAdd {
line := content.Line
for idx := line - 1; idx >= 0; idx-- {
if isEmptyLineOrComment((*linesSlice)[idx]) {
(*contentToAdd)[contentIdx].Line -= 1
} else {
break
}
}
}
}
func applyFixesToFile(filePath string, contentToAdd *[]ContentToAdd, linesToRemove *[]ContentToRemove, contentAtHead string) error {
linesSlice, err := getLinesSlice(filePath)
+21
View File
@@ -292,6 +292,27 @@ func truncateContentAtHead(filePath string) (string, error) {
return contentAtHead, nil
}
func adjustContentLines(contentToAdd *[]ContentToAdd, linesSlice *[]string) {
for contentIdx, content := range *contentToAdd {
line := content.Line
// Update Line number to last line if their value is math.Inf
if line == int(math.Inf(1)) {
(*contentToAdd)[contentIdx].Line = len(*linesSlice)
continue
}
// Adjust line numbers such that there are no "empty lines or comment lines of next nodes" before them
for idx := line - 1; idx >= 0; idx-- {
if isEmptyLineOrComment((*linesSlice)[idx]) {
(*contentToAdd)[contentIdx].Line -= 1
} else {
break
}
}
}
}
// Get the lines of existing yaml in a slice
func getLinesSlice(filePath string) ([]string, error) {
lineSlice := make([]string, 0)
@@ -11,4 +11,4 @@ spec:
- name: nginx_container
image: nginx
securityContext:
allowPrivilegeEscalation: false
allowPrivilegeEscalation: false
@@ -7,9 +7,9 @@ metadata:
name: insert_list
spec:
containers:
- name: nginx1
- name: nginx_container
image: nginx
securityContext:
capabilities:
drop:
- NET_RAW
- NET_RAW
@@ -7,10 +7,10 @@ metadata:
name: insert_list
spec:
containers:
- name: nginx1
- name: nginx_container
image: nginx
securityContext:
capabilities:
drop:
- NET_RAW
- SYS_ADM
- SYS_ADM
@@ -15,4 +15,4 @@ spec:
# Testing if comments are retained as intended
securityContext:
runAsRoot: false
runAsRoot: false
@@ -13,4 +13,4 @@ spec:
capabilities:
drop:
- "NET_RAW"
- "SYS_ADM"
- "SYS_ADM"
@@ -7,7 +7,7 @@ metadata:
name: insert_list
spec:
containers:
- name: nginx1
- name: nginx_container
image: nginx
securityContext:
capabilities: