ProcessDirectory processa um diretório recursivamente de forma concorrente e segura.
(ctx context.Context, dirPath string, options DirectoryScanOptions)
| 143 | |
| 144 | // ProcessDirectory processa um diretório recursivamente de forma concorrente e segura. |
| 145 | func ProcessDirectory(ctx context.Context, dirPath string, options DirectoryScanOptions) ([]FileInfo, error) { |
| 146 | dirPath, err := ExpandPath(dirPath) |
| 147 | if err != nil { |
| 148 | return nil, fmt.Errorf("erro ao expandir o caminho: %w", err) |
| 149 | } |
| 150 | |
| 151 | fileInfo, err := os.Stat(dirPath) |
| 152 | if err != nil { |
| 153 | return nil, fmt.Errorf("erro ao acessar o caminho: %w", err) |
| 154 | } |
| 155 | |
| 156 | if !fileInfo.IsDir() { |
| 157 | content, err := ReadFileContent(dirPath, options.MaxTotalSize) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | fileType := DetectFileType(dirPath) |
| 162 | file := FileInfo{Path: dirPath, Content: content, Size: fileInfo.Size(), Type: fileType} |
| 163 | if options.OnFileProcessed != nil { |
| 164 | options.OnFileProcessed(file) |
| 165 | } |
| 166 | return []FileInfo{file}, nil |
| 167 | } |
| 168 | |
| 169 | // Carrega padrões de exclusão customizados e os adiciona às opções. |
| 170 | customExcludeDirs, customExcludePatterns := loadIgnorePatterns(dirPath, options.Logger) |
| 171 | options.ExcludeDirs = append(options.ExcludeDirs, customExcludeDirs...) |
| 172 | options.ExcludePatterns = append(options.ExcludePatterns, customExcludePatterns...) |
| 173 | |
| 174 | ctx, cancel := context.WithCancel(ctx) |
| 175 | defer cancel() |
| 176 | |
| 177 | var ( |
| 178 | result = make([]FileInfo, 0, options.MaxFilesToProcess) |
| 179 | totalSize int64 |
| 180 | fileCount int |
| 181 | filesToProcessChan = make(chan string, 100) |
| 182 | resultsChan = make(chan FileInfo, 100) |
| 183 | wgWorkers sync.WaitGroup |
| 184 | workerCount = 4 |
| 185 | ) |
| 186 | |
| 187 | for i := 0; i < workerCount; i++ { |
| 188 | wgWorkers.Add(1) |
| 189 | go func() { |
| 190 | defer wgWorkers.Done() |
| 191 | for { |
| 192 | select { |
| 193 | case path, ok := <-filesToProcessChan: |
| 194 | if !ok { |
| 195 | return |
| 196 | } |
| 197 | content, err := ReadFileContent(path, options.MaxTotalSize) |
| 198 | if err != nil { |
| 199 | options.Logger.Warn("Erro ao ler arquivo, pulando", zap.String("path", path), zap.Error(err)) |
| 200 | continue |
| 201 | } |
| 202 | info, err := os.Stat(path) |