mugit/internal/config/validate.go (view raw)
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func (c Config) validate() error { |
| 10 | var errs []error |
| 11 | |
| 12 | if c.Meta.Host == "" { |
| 13 | // TODO: actually it should be a warning, host only used for go-import tag |
| 14 | errs = append(errs, errors.New("meta.host is required")) |
| 15 | } |
| 16 | |
| 17 | if !isDirExists(c.Repo.Dir) { |
| 18 | errs = append(errs, fmt.Errorf("repo.dir seems to be an invalid path")) |
| 19 | } |
| 20 | |
| 21 | if err := checkPort(c.Server.Port); err != nil { |
| 22 | errs = append(errs, fmt.Errorf("server.port %w", err)) |
| 23 | } |
| 24 | |
| 25 | if c.SSH.Enable { |
| 26 | if err := checkPort(c.SSH.Port); err != nil { |
| 27 | errs = append(errs, fmt.Errorf("ssh.port %w", err)) |
| 28 | } |
| 29 | |
| 30 | if c.SSH.Port == c.Server.Port { |
| 31 | errs = append(errs, fmt.Errorf("ssh.port must differ from server.port (both are %d)", c.Server.Port)) |
| 32 | } |
| 33 | |
| 34 | if !isFileExists(c.SSH.HostKey) { |
| 35 | errs = append(errs, fmt.Errorf("ssh.host_key seems to be an invalid path")) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | if c.Mirror.Enable { |
| 40 | if _, err := time.ParseDuration(c.Mirror.Interval); err != nil { |
| 41 | errs = append(errs, fmt.Errorf("mirror.interval: invalid duration format: %w", err)) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return errors.Join(errs...) |
| 46 | } |
| 47 | |
| 48 | func checkPort(port int) error { |
| 49 | if port < 1 || port > 65535 { |
| 50 | return fmt.Errorf("must be between 1 and 65535, got %d", port) |
| 51 | } |
| 52 | return nil |
| 53 | } |