loadRuleFile loads a single rule file with caching.
(filePath string)
| 130 | |
| 131 | // loadRuleFile loads a single rule file with caching. |
| 132 | func (rl *RulesLoader) loadRuleFile(filePath string) *Rule { |
| 133 | info, err := os.Stat(filePath) |
| 134 | if err != nil { |
| 135 | return nil |
| 136 | } |
| 137 | |
| 138 | // Check cache |
| 139 | rl.mu.RLock() |
| 140 | cached, found := rl.cache[filePath] |
| 141 | rl.mu.RUnlock() |
| 142 | |
| 143 | if found && !info.ModTime().After(cached.ModTime) { |
| 144 | return cached |
| 145 | } |
| 146 | |
| 147 | // Read and parse |
| 148 | data, err := os.ReadFile(filePath) //#nosec G304 -- path supplied by user/agent through validated tool surface (boundary check upstream) |
| 149 | if err != nil { |
| 150 | rl.logger.Debug("failed to read rule file", zap.String("path", filePath), zap.Error(err)) |
| 151 | return nil |
| 152 | } |
| 153 | |
| 154 | content := string(data) |
| 155 | rule := &Rule{ |
| 156 | Name: strings.TrimSuffix(filepath.Base(filePath), ".md"), |
| 157 | ModTime: info.ModTime(), |
| 158 | } |
| 159 | |
| 160 | // Parse simple frontmatter for paths |
| 161 | if strings.HasPrefix(content, "---") { |
| 162 | parts := strings.SplitN(content[3:], "---", 2) |
| 163 | if len(parts) == 2 { |
| 164 | frontmatter := parts[0] |
| 165 | rule.Content = strings.TrimSpace(parts[1]) |
| 166 | |
| 167 | // Extract paths from frontmatter |
| 168 | inPathsList := false |
| 169 | for _, line := range strings.Split(frontmatter, "\n") { |
| 170 | trimmed := strings.TrimSpace(line) |
| 171 | |
| 172 | if strings.HasPrefix(trimmed, "paths:") { |
| 173 | inPathsList = true |
| 174 | pathsStr := strings.TrimPrefix(trimmed, "paths:") |
| 175 | pathsStr = strings.TrimSpace(pathsStr) |
| 176 | |
| 177 | // Handle inline array: paths: ["src/**", "lib/**"] |
| 178 | if strings.HasPrefix(pathsStr, "[") { |
| 179 | pathsStr = strings.Trim(pathsStr, "[]") |
| 180 | for _, p := range strings.Split(pathsStr, ",") { |
| 181 | p = strings.TrimSpace(p) |
| 182 | p = strings.Trim(p, `"'`) |
| 183 | if p != "" { |
| 184 | rule.Paths = append(rule.Paths, p) |
| 185 | } |
| 186 | } |
| 187 | inPathsList = false // inline array complete |
| 188 | } |
| 189 | continue |
no test coverage detected