LoadYAMLPolicy returns (nil, nil) when path is empty or file is absent, so callers can pass the result straight into Sources.YAMLRules. A present file yields one or more rules (see yaml.Parse).
(path string)
| 96 | // so callers can pass the result straight into Sources.YAMLRules. A |
| 97 | // present file yields one or more rules (see yaml.Parse). |
| 98 | func LoadYAMLPolicy(path string) ([]*platform.Rule, error) { |
| 99 | if path == "" { |
| 100 | return nil, nil |
| 101 | } |
| 102 | if _, err := vfs.Stat(path); err != nil { |
| 103 | if errors.Is(err, os.ErrNotExist) { |
| 104 | return nil, nil |
| 105 | } |
| 106 | return nil, fmt.Errorf("stat policy yaml %q: %w", path, err) |
| 107 | } |
| 108 | data, err := vfs.ReadFile(path) |
| 109 | if err != nil { |
| 110 | return nil, fmt.Errorf("read policy yaml %q: %w", path, err) |
| 111 | } |
| 112 | rules, err := pyaml.Parse(data) |
| 113 | if err != nil { |
| 114 | return nil, fmt.Errorf("policy yaml %q: %w", path, err) |
| 115 | } |
| 116 | return rules, nil |
| 117 | } |