Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 17 additions & 3 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,23 @@ func AddDefaultGroupToConfig(config Config) Config {
}

func SanitizeCommand(cmdStr string) ([]string, error) {
// Remove trailing backslashes
cmdStr = strings.ReplaceAll(cmdStr, "\\ \n", " ")
cmdStr = strings.ReplaceAll(cmdStr, "\\\n", " ")
var cleanedLines []string
for _, line := range strings.Split(cmdStr, "\n") {
trimmed := strings.TrimSpace(line)
// Skip comment lines
if strings.HasPrefix(trimmed, "#") {
continue
}
// Handle trailing backslashes by replacing with space
if strings.HasSuffix(trimmed, "\\") {
cleanedLines = append(cleanedLines, strings.TrimSuffix(trimmed, "\\")+" ")
} else {
cleanedLines = append(cleanedLines, line)
}
}

// put it back together
cmdStr = strings.Join(cleanedLines, "\n")

Comment on lines 231 to 249
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Good implementation for handling comments and line continuations

The refactored implementation properly addresses the issue by processing the command string line-by-line, which is more robust than a global replacement approach. The function now correctly:

  1. Skips lines that are comments (start with #)
  2. Handles trailing backslashes as line continuations
  3. Preserves original lines that don't need special handling

However, there's one inconsistency in how whitespace is handled:

- cleanedLines = append(cleanedLines, line)
+ cleanedLines = append(cleanedLines, trimmed)

For non-continuation lines, you're preserving the original line with its whitespace, but for continuation lines, you're using the trimmed version. This inconsistency could lead to unexpected behavior with whitespace. Since you're trimming the whitespace for checking comments and backslashes, it would be better to consistently use the trimmed version for all lines.

📝 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
func SanitizeCommand(cmdStr string) ([]string, error) {
// Remove trailing backslashes
cmdStr = strings.ReplaceAll(cmdStr, "\\ \n", " ")
cmdStr = strings.ReplaceAll(cmdStr, "\\\n", " ")
var cleanedLines []string
for _, line := range strings.Split(cmdStr, "\n") {
trimmed := strings.TrimSpace(line)
// Skip comment lines
if strings.HasPrefix(trimmed, "#") {
continue
}
// Handle trailing backslashes by replacing with space
if strings.HasSuffix(trimmed, "\\") {
cleanedLines = append(cleanedLines, strings.TrimSuffix(trimmed, "\\")+" ")
} else {
cleanedLines = append(cleanedLines, line)
}
}
// put it back together
cmdStr = strings.Join(cleanedLines, "\n")
func SanitizeCommand(cmdStr string) ([]string, error) {
var cleanedLines []string
for _, line := range strings.Split(cmdStr, "\n") {
trimmed := strings.TrimSpace(line)
// Skip comment lines
if strings.HasPrefix(trimmed, "#") {
continue
}
// Handle trailing backslashes by replacing with space
if strings.HasSuffix(trimmed, "\\") {
cleanedLines = append(cleanedLines, strings.TrimSuffix(trimmed, "\\")+" ")
} else {
- cleanedLines = append(cleanedLines, line)
+ cleanedLines = append(cleanedLines, trimmed)
}
}
// put it back together
cmdStr = strings.Join(cleanedLines, "\n")
🤖 Prompt for AI Agents
In proxy/config.go around lines 231 to 249, the SanitizeCommand function
inconsistently handles whitespace by appending trimmed lines for continuation
lines but original lines for others. To fix this, always use the trimmed version
of each line when appending to cleanedLines, ensuring consistent whitespace
handling throughout the function.

// Split the command into arguments
var args []string
Expand Down
6 changes: 6 additions & 0 deletions proxy/config_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ func TestConfig_SanitizeCommand(t *testing.T) {
-a "double quotes" \
--arg2 'single quotes'
-s
# comment 1
--arg3 123 \
# comment 2
--arg4 '"string in string"'
# this will get stripped out as well as the white space above
-c "'single quoted'"
`)
assert.NoError(t, err)
Expand Down
9 changes: 8 additions & 1 deletion proxy/config_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ import (
func TestConfig_SanitizeCommand(t *testing.T) {
// does not support single quoted strings like in config_posix_test.go
args, err := SanitizeCommand(`python model1.py \
-a "double quotes" \

-a "double quotes" \
-s
--arg3 123 \

# comment 2
--arg4 '"string in string"'



# this will get stripped out as well as the white space above
-c "'single quoted'"
`)
assert.NoError(t, err)
Expand Down