all repos

mugit @ a940f43f0e7d974f75672988227623e2e1fa9a08

🐮 git server that your cow will love

mugit/internal/config/validate.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
ssh!: switch to sshd, 2 months ago
1
package config
2
3
import (
4
	"errors"
5
	"fmt"
6
	"regexp"
7
	"strings"
8
)
9
10
var validUserNameRe = regexp.MustCompile("^[a-z_][a-z0-9_-]{0,31}$")
11
12
func (c Config) validate() error {
13
	var errs []error
14
15
	if c.Meta.Host == "" {
16
		errs = append(errs, errors.New("meta.host is required"))
17
	}
18
19
	if strings.HasPrefix(c.Meta.Host, "http") {
20
		errs = append(errs, errors.New("meta.host shouldn't include protocol"))
21
	}
22
23
	if !isDirExists(c.Repo.Dir) {
24
		errs = append(errs, fmt.Errorf("repo.dir seems to be an invalid path"))
25
	}
26
27
	if err := checkPort(c.Server.Port); err != nil {
28
		errs = append(errs, fmt.Errorf("server.port %w", err))
29
	}
30
31
	if c.SSH.Enable {
32
		if !validUserNameRe.MatchString(c.SSH.User) {
33
			errs = append(errs, fmt.Errorf("ssh.user must be correct linux user name(^[a-z_][a-z0-9_-]{0,31}$)"))
34
		}
35
	}
36
37
	return errors.Join(errs...)
38
}
39
40
func checkPort(port int) error {
41
	if port < 1 || port > 65535 {
42
		return fmt.Errorf("must be between 1 and 65535, got %d", port)
43
	}
44
	return nil
45
}