From c3771eec7ef5598b3c9ee89f85e6ef52e2ce665f Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Fri, 16 Dec 2022 15:57:23 +0530 Subject: [PATCH] Remove redundant functions and clean code after refactoring --- core/pkg/fixhandler/fixhandler.go | 40 ++++++------- core/pkg/fixhandler/fixhandler_test.go | 2 +- core/pkg/fixhandler/yamlhandler.go | 2 +- core/pkg/fixhandler/yamlhelper.go | 83 +------------------------- 4 files changed, 25 insertions(+), 102 deletions(-) diff --git a/core/pkg/fixhandler/fixhandler.go b/core/pkg/fixhandler/fixhandler.go index f34005d4..b17ed054 100644 --- a/core/pkg/fixhandler/fixhandler.go +++ b/core/pkg/fixhandler/fixhandler.go @@ -245,7 +245,7 @@ func (h *FixHandler) ApplyFix(yamlString, yamlExpression string) (fixedYamlStrin fixedYamlLines := getFixedYamlLines(yamlLines, contentsToAdd, linesToRemove) - fixedYamlString = getFixedYamlString(fixedYamlLines) + fixedYamlString = getStringFromSlice(fixedYamlLines) return fixedYamlString, nil } @@ -297,6 +297,25 @@ func reduceYamlExpressions(resource *ResourceFixInfo) string { return strings.Join(expressions, " | ") } +func fixPathToValidYamlExpression(fixPath, value string, documentIndexInYaml int) string { + isStringValue := true + if _, err := strconv.ParseBool(value); err == nil { + isStringValue = false + } else if _, err := strconv.ParseFloat(value, 64); err == nil { + isStringValue = false + } else if _, err := strconv.Atoi(value); err == nil { + isStringValue = false + } + + // Strings should be quoted + if isStringValue { + value = fmt.Sprintf("\"%s\"", value) + } + + // select document index and add a dot for the root node + return fmt.Sprintf("select(di==%d).%s |= %s", documentIndexInYaml, fixPath, value) +} + func joinStrings(inputStrings ...string) string { return strings.Join(inputStrings, "") } @@ -320,22 +339,3 @@ func writeFixesToFile(filepath, content string) error { return nil } - -func fixPathToValidYamlExpression(fixPath, value string, documentIndexInYaml int) string { - isStringValue := true - if _, err := strconv.ParseBool(value); err == nil { - isStringValue = false - } else if _, err := strconv.ParseFloat(value, 64); err == nil { - isStringValue = false - } else if _, err := strconv.Atoi(value); err == nil { - isStringValue = false - } - - // Strings should be quoted - if isStringValue { - value = fmt.Sprintf("\"%s\"", value) - } - - // select document index and add a dot for the root node - return fmt.Sprintf("select(di==%d).%s |= %s", documentIndexInYaml, fixPath, value) -} diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index 8fadad4c..363149c3 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -151,7 +151,7 @@ func TestApplyFixKeepsIndentation(t *testing.T) { got, _ := h.ApplyFix(string(input), expression) if got != string(want) { - t.Errorf("Fixed file does not match the expected.\nGot: <%s>\nWant:<%s>", got, want) + t.Errorf("Fixed file does not match the expected.\n FilePath: %s \nGot: <%s>\nWant:<%s>", tc.inputFile, got, want) } } diff --git a/core/pkg/fixhandler/yamlhandler.go b/core/pkg/fixhandler/yamlhandler.go index c6977544..1ad233fa 100644 --- a/core/pkg/fixhandler/yamlhandler.go +++ b/core/pkg/fixhandler/yamlhandler.go @@ -196,7 +196,7 @@ func addLinesToInsert(fixInfoMetadata *fixInfoMetadata) (int, int) { currentDFSNode := (*fixInfoMetadata.fixedList)[fixInfoMetadata.fixedListTracker] lineToInsert := getLineToInsert(fixInfoMetadata) - contentToInsert := constructContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) + contentToInsert := getContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) newFixedTracker := updateTracker(fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) diff --git a/core/pkg/fixhandler/yamlhelper.go b/core/pkg/fixhandler/yamlhelper.go index a58ac174..93327b2c 100644 --- a/core/pkg/fixhandler/yamlhelper.go +++ b/core/pkg/fixhandler/yamlhelper.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "log" "math" "os" "strings" @@ -112,7 +111,7 @@ func enocodeIntoYaml(parentNode *yaml.Node, nodeList *[]nodeInfo, tracker int) ( return fmt.Sprintf(`%v`, buf.String()), nil } -func constructContent(parentNode *yaml.Node, nodeList *[]nodeInfo, tracker int) string { +func getContent(parentNode *yaml.Node, nodeList *[]nodeInfo, tracker int) string { content, err := enocodeIntoYaml(parentNode, nodeList, tracker) if err != nil { logger.L().Fatal("Cannot Encode into YAML") @@ -137,30 +136,6 @@ func indentContent(content string, indentationSpaces int) string { return indentedContent } -// Get the lines of existing yaml in a slice -func getLinesSlice(filePath string) ([]string, error) { - lineSlice := make([]string, 0) - - file, err := os.Open(filePath) - if err != nil { - logger.L().Fatal(fmt.Sprintf("Cannot open file %s", filePath)) - return nil, err - } - defer file.Close() - - scanner := bufio.NewScanner(file) - - for scanner.Scan() { - lineSlice = append(lineSlice, scanner.Text()) - } - if err := scanner.Err(); err != nil { - log.Fatal(err) - return nil, err - } - - return lineSlice, err -} - func getLineToInsert(fixInfoMetadata *fixInfoMetadata) int { var lineToInsert int // Check if lineToInsert is last line @@ -344,7 +319,7 @@ func replaceSingleLineSequence(fixInfoMetadata *fixInfoMetadata, line int) (int, fixedListTracker := getFirstNodeInLine(fixInfoMetadata.fixedList, line) currentDFSNode := (*fixInfoMetadata.fixedList)[fixedListTracker] - contentToInsert := constructContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixedListTracker) + contentToInsert := getContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixedListTracker) // Remove the Single line *fixInfoMetadata.linesToRemove = append(*fixInfoMetadata.linesToRemove, linesToRemove{ @@ -410,58 +385,6 @@ func getChildrenCount(node *yaml.Node) int { return totalChildren } -// Truncates the comments and empty lines at the top of the file and -// returns the truncated content -func truncateContentAtHead(filePath string) (string, error) { - var contentAtHead string - - linesSlice, err := getLinesSlice(filePath) - - if err != nil { - return "", err - } - - if err := os.Truncate(filePath, 0); err != nil { - return "", err - } - - file, err := os.OpenFile(filePath, os.O_RDWR, 0644) - if err != nil { - return "", err - } - - defer func() error { - if err := file.Close(); err != nil { - return err - } - return nil - }() - - lineIdx := 0 - - for lineIdx < len(linesSlice) { - if isEmptyLineOrComment(linesSlice[lineIdx]) { - contentAtHead += (linesSlice[lineIdx] + "\n") - lineIdx += 1 - } else { - break - } - } - - writer := bufio.NewWriter(file) - - for lineIdx < len(linesSlice) { - _, err = writer.WriteString(linesSlice[lineIdx] + "\n") - if err != nil { - return "", err - } - lineIdx += 1 - } - - writer.Flush() - return contentAtHead, nil -} - // The current node along with it's children is skipped and the tracker is moved to next sibling // of current node. If parent is mapping node, "value" in "key-value" pairs is also skipped. func updateTracker(nodeList *[]nodeInfo, tracker int) int { @@ -478,6 +401,6 @@ func updateTracker(nodeList *[]nodeInfo, tracker int) int { return updatedTracker } -func getFixedYamlString(yamlLines []string) (fixedYamlString string) { +func getStringFromSlice(yamlLines []string) (fixedYamlString string) { return strings.Join(yamlLines, "\n") }