all repos

mugit @ afcecfc

🐮 git server that your cow will love

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
support custom ssh user name (for future switch to ssh), 3 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 err := checkPort(c.SSH.Port); err != nil {
33
			errs = append(errs, fmt.Errorf("ssh.port %w", err))
34
		}
35
36
		if c.SSH.Port == c.Server.Port {
37
			errs = append(errs, fmt.Errorf("ssh.port must differ from server.port (both are %d)", c.Server.Port))
38
		}
39
40
		if !validUserNameRe.MatchString(c.SSH.User) {
41
			errs = append(errs, fmt.Errorf("ssh.user must be correct linux user name(^[a-z_][a-z0-9_-]{0,31}$)"))
42
		}
43
44
		if !isFileExists(c.SSH.HostKey) {
45
			errs = append(errs, fmt.Errorf("ssh.host_key seems to be an invalid path"))
46
		}
47
	}
48
49
	return errors.Join(errs...)
50
}
51
52
func checkPort(port int) error {
53
	if port < 1 || port > 65535 {
54
		return fmt.Errorf("must be between 1 and 65535, got %d", port)
55
	}
56
	return nil
57
}