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)
| 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 |
| 237 | func 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 |
| 249 | func ListFilesInDir(path string) ([]string, error) { |