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