CollectYAMLFiles returns YAML file paths from the given path. If path is a file, it returns that file. If a directory, it walks recursively.
(path string)
| 130 | // CollectYAMLFiles returns YAML file paths from the given path. |
| 131 | // If path is a file, it returns that file. If a directory, it walks recursively. |
| 132 | func CollectYAMLFiles(path string) ([]string, error) { |
| 133 | info, err := os.Stat(path) |
| 134 | if err != nil { |
| 135 | return nil, fmt.Errorf("accessing path %q: %w", path, err) |
| 136 | } |
| 137 | |
| 138 | if !info.IsDir() { |
| 139 | return []string{path}, nil |
| 140 | } |
| 141 | |
| 142 | var files []string |
| 143 | err = filepath.WalkDir(path, func(p string, d os.DirEntry, err error) error { |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | if d.IsDir() { |
| 148 | return nil |
| 149 | } |
| 150 | ext := strings.ToLower(filepath.Ext(p)) |
| 151 | if ext == ".yaml" || ext == ".yml" { |
| 152 | files = append(files, p) |
| 153 | } |
| 154 | return nil |
| 155 | }) |
| 156 | if err != nil { |
| 157 | return nil, fmt.Errorf("walking directory %q: %w", path, err) |
| 158 | } |
| 159 | |
| 160 | return files, nil |
| 161 | } |
| 162 | |
| 163 | // SplitYAMLDocuments splits a potentially multi-document YAML file into individual documents, |
| 164 | // extracting kind and name from each. |