Skip to content

Commit

Permalink
Add function to check if string contains any substrings
Browse files Browse the repository at this point in the history
  • Loading branch information
kromiii committed Jul 2, 2024
1 parent 1ffd0dd commit 33f6d4c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions internal/modifier/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package modifier

import (
"os"
"regexp"
"strings"

"github.com/kromiii/unleash-checker-ai/pkg/openai"
)
Expand All @@ -22,10 +24,26 @@ func (m *Modifier) ModifyFile(filePath string, unusedFlags []string) error {
return err
}

modifiedContent, err := m.openaiClient.ModifyCode(string(content), unusedFlags)
if err != nil {
return err
modifiedContent := string(content)
lines := strings.Split(modifiedContent, "\n")
for i, line := range lines {
if containsAny(line, unusedFlags) {
modifiedLine, err := m.openaiClient.ModifyCode(line, unusedFlags)
if err != nil {
return err
}

lines[i] = modifiedLine
}
}

modifiedContent = strings.Join(lines, "\n")

return os.WriteFile(filePath, []byte(modifiedContent), 0644)
}

func containsAny(s string, substrs []string) bool {
pattern := strings.Join(substrs, "|")
matched, _ := regexp.MatchString(pattern, s)
return matched
}

0 comments on commit 33f6d4c

Please sign in to comment.