mugit/internal/config/validate.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com config: parse interval directly as time.Duration, 3 months ago
olexsmir@gmail.com config: parse interval directly as time.Duration, 3 months ago
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | func (c Config) validate() error { |
| 10 | var errs []error |
| 11 | |
| 12 | if c.Meta.Host == "" { |
| 13 | errs = append(errs, errors.New("meta.host is required")) |
| 14 | } |
| 15 | |
| 16 | if strings.HasPrefix(c.Meta.Host, "http") { |
| 17 | errs = append(errs, errors.New("meta.host shouldn't include protocol")) |
| 18 | } |
| 19 | |
| 20 | if !isDirExists(c.Repo.Dir) { |
| 21 | errs = append(errs, fmt.Errorf("repo.dir seems to be an invalid path")) |
| 22 | } |
| 23 | |
| 24 | if err := checkPort(c.Server.Port); err != nil { |
| 25 | errs = append(errs, fmt.Errorf("server.port %w", err)) |
| 26 | } |
| 27 | |
| 28 | if c.SSH.Enable { |
| 29 | if err := checkPort(c.SSH.Port); err != nil { |
| 30 | errs = append(errs, fmt.Errorf("ssh.port %w", err)) |
| 31 | } |
| 32 | |
| 33 | if c.SSH.Port == c.Server.Port { |
| 34 | errs = append(errs, fmt.Errorf("ssh.port must differ from server.port (both are %d)", c.Server.Port)) |
| 35 | } |
| 36 | |
| 37 | if !isFileExists(c.SSH.HostKey) { |
| 38 | errs = append(errs, fmt.Errorf("ssh.host_key seems to be an invalid path")) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return errors.Join(errs...) |
| 43 | } |
| 44 | |
| 45 | func checkPort(port int) error { |
| 46 | if port < 1 || port > 65535 { |
| 47 | return fmt.Errorf("must be between 1 and 65535, got %d", port) |
| 48 | } |
| 49 | return nil |
| 50 | } |