Skip to content
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

Added filename length restriction for debug mode (-srd flag) Issue-5929 #5931

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ func sanitizeFileName(fileName string) string {
}
func (w *StandardWriter) WriteStoreDebugData(host, templateID, eventType string, data string) {
if w.storeResponse {
if len(host) > 60 {
host = host[:57] + "..."
}
if len(templateID) > 100 {
templateID = templateID[:97] + "..."
}
Comment on lines +515 to +520
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Extract magic numbers into named constants.

The hardcoded length limits and truncation points should be defined as package-level constants for better maintainability and clarity.

+const (
+    maxHostLength = 60
+    maxTemplateIDLength = 100
+    ellipsis = "..."
+    ellipsisLength = len(ellipsis)
+)

 func (w *StandardWriter) WriteStoreDebugData(host, templateID, eventType string, data string) {
 	if w.storeResponse {
-		if len(host) > 60 {
-			host = host[:57] + "..."
+		if len(host) > maxHostLength {
+			host = host[:maxHostLength-ellipsisLength] + ellipsis
 		}
-		if len(templateID) > 100 {
-			templateID = templateID[:97] + "..."
+		if len(templateID) > maxTemplateIDLength {
+			templateID = templateID[:maxTemplateIDLength-ellipsisLength] + ellipsis
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if len(host) > 60 {
host = host[:57] + "..."
}
if len(templateID) > 100 {
templateID = templateID[:97] + "..."
}
const (
maxHostLength = 60
maxTemplateIDLength = 100
ellipsis = "..."
ellipsisLength = len(ellipsis)
)
if len(host) > maxHostLength {
host = host[:maxHostLength-ellipsisLength] + ellipsis
}
if len(templateID) > maxTemplateIDLength {
templateID = templateID[:maxTemplateIDLength-ellipsisLength] + ellipsis
}


Comment on lines +515 to +521
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider total filename length and OS limitations.

The current implementation truncates host and templateID independently, but doesn't consider their combined length along with separators and the file extension. This could still result in exceeding OS filename limits.

+const (
+    // Common OS filename length limits
+    maxFilenameLength = 255 // typical limit for many filesystems
+    // Components
+    separatorLength = 1 // "_" separator
+    extensionLength = 4 // ".txt"
+)

 func (w *StandardWriter) WriteStoreDebugData(host, templateID, eventType string, data string) {
 	if w.storeResponse {
+		// Calculate available space for host and templateID
+		availableLength := maxFilenameLength - separatorLength - extensionLength
+		
+		// Distribute available length proportionally
+		hostRatio := 0.4 // 40% for host
+		templateIDRatio := 0.6 // 60% for templateID
+		
+		maxHostLen := int(float64(availableLength) * hostRatio)
+		maxTemplateIDLen := int(float64(availableLength) * templateIDRatio)
+
 		if len(host) > maxHostLength {
 			host = host[:maxHostLength-ellipsisLength] + ellipsis
 		}
 		if len(templateID) > maxTemplateIDLength {
 			templateID = templateID[:maxTemplateIDLength-ellipsisLength] + ellipsis
 		}
+
+		// Ensure minimum length requirements
+		if len(host) < 1 || len(templateID) < 1 {
+			gologger.Warning().Msgf("Host or templateID too short after truncation: %s_%s", host, templateID)
+		}

Committable suggestion skipped: line range outside the PR's diff.

filename := sanitizeFileName(fmt.Sprintf("%s_%s", host, templateID))
subFolder := filepath.Join(w.storeResponseDir, sanitizeFileName(eventType))
if !fileutil.FolderExists(subFolder) {
Expand All @@ -526,7 +533,6 @@ func (w *StandardWriter) WriteStoreDebugData(host, templateID, eventType string,
_, _ = f.WriteString(fmt.Sprintln(data))
f.Close()
}

}

// tryParseCause tries to parse the cause of given error
Expand Down
Loading