LoadFile parses and validates the given YAML file into a read-only Config. Callers should never write to or shallow copy the returned Config.
(filename string, agentMode bool, logger *slog.Logger)
| 129 | // LoadFile parses and validates the given YAML file into a read-only Config. |
| 130 | // Callers should never write to or shallow copy the returned Config. |
| 131 | func LoadFile(filename string, agentMode bool, logger *slog.Logger) (*Config, error) { |
| 132 | content, err := os.ReadFile(filename) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | cfg, err := Load(string(content), logger) |
| 137 | if err != nil { |
| 138 | return nil, fmt.Errorf("parsing YAML file %s: %w", filename, err) |
| 139 | } |
| 140 | |
| 141 | if agentMode { |
| 142 | if len(cfg.AlertingConfig.AlertmanagerConfigs) > 0 || len(cfg.AlertingConfig.AlertRelabelConfigs) > 0 { |
| 143 | return nil, errors.New("field alerting is not allowed in agent mode") |
| 144 | } |
| 145 | |
| 146 | if len(cfg.RuleFiles) > 0 { |
| 147 | return nil, errors.New("field rule_files is not allowed in agent mode") |
| 148 | } |
| 149 | |
| 150 | if len(cfg.RemoteReadConfigs) > 0 { |
| 151 | return nil, errors.New("field remote_read is not allowed in agent mode") |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | cfg.SetDirectory(filepath.Dir(filename)) |
| 156 | return cfg, nil |
| 157 | } |
| 158 | |
| 159 | func boolPtr(b bool) *bool { |
| 160 | return &b |
searching dependent graphs…