all repos

mugit @ 0238022

🐮 git server that your cow will love

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
package config

import (
	"errors"
	"fmt"
	"strings"
)

func (c Config) validate() error {
	var errs []error

	if c.Meta.Host == "" {
		errs = append(errs, errors.New("meta.host is required"))
	}

	if strings.HasPrefix(c.Meta.Host, "http") {
		errs = append(errs, errors.New("meta.host shouldn't include protocol"))
	}

	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"))
		}
	}

	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
}