MCPcopy Create free account
hub / github.com/ddev/ddev / GrepStringInFile

Function GrepStringInFile

pkg/fileutil/files.go:237–246  ·  view source on GitHub ↗

GrepStringInFile is a small hammer for looking for a regex in a file. It should only be used against very modest sized files, as the entire file is read into a string. Returns found, matches, error

(fullPath string, needle string)

Source from the content-addressed store, hash-verified

235// It should only be used against very modest sized files, as the entire file is read
236// into a string. Returns found, matches, error
237func GrepStringInFile(fullPath string, needle string) (bool, []string, error) {
238 fullFileBytes, err := os.ReadFile(fullPath)
239 if err != nil {
240 return false, nil, fmt.Errorf("failed to open file %s, err:%v ", fullPath, err)
241 }
242 fullFileString := string(fullFileBytes)
243 re := regexp.MustCompile(needle)
244 matches := re.FindStringSubmatch(fullFileString)
245 return len(matches) > 0, matches, nil
246}
247
248// ListFilesInDir returns an array of files or directories found in a directory
249func ListFilesInDir(path string) ([]string, error) {

Callers 2

TestDdevAllDatabasesFunction · 0.92
GetDrupalVersionFunction · 0.92

Calls 1

ErrorfMethod · 0.80

Tested by 1

TestDdevAllDatabasesFunction · 0.74