Validate checks invariants and sets defaults in-place.
()
| 128 | |
| 129 | // Validate checks invariants and sets defaults in-place. |
| 130 | func (c *Config) Validate() error { |
| 131 | if c.Version == 0 { |
| 132 | c.Version = 1 |
| 133 | } |
| 134 | if c.Version != 1 { |
| 135 | return fmt.Errorf("config: unsupported version %d (want 1)", c.Version) |
| 136 | } |
| 137 | if len(c.Profiles) == 0 { |
| 138 | return errors.New("config: no profiles defined") |
| 139 | } |
| 140 | names := make(map[string]struct{}, len(c.Profiles)) |
| 141 | for i := range c.Profiles { |
| 142 | p := &c.Profiles[i] |
| 143 | if p.Name == "" { |
| 144 | return fmt.Errorf("config: profile[%d]: name is required", i) |
| 145 | } |
| 146 | if _, dup := names[p.Name]; dup { |
| 147 | return fmt.Errorf("config: duplicate profile name %q", p.Name) |
| 148 | } |
| 149 | names[p.Name] = struct{}{} |
| 150 | if err := p.validate(); err != nil { |
| 151 | return fmt.Errorf("config: profile %q: %w", p.Name, err) |
| 152 | } |
| 153 | } |
| 154 | if c.Active == "" { |
| 155 | c.Active = c.Profiles[0].Name |
| 156 | } |
| 157 | if _, ok := names[c.Active]; !ok { |
| 158 | return fmt.Errorf("config: active profile %q not found", c.Active) |
| 159 | } |
| 160 | if c.Log.Level == "" { |
| 161 | c.Log.Level = "info" |
| 162 | } |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | func (p *Profile) validate() error { |
| 167 | if p.Listen == "" { |