summaryrefslogtreecommitdiff
path: root/plugins/reload/config.go
blob: 9bce6b2581a7668c37bd93088d8df175b0eaa4f0 (plain)
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
54
55
56
57
58
package reload

import (
	"time"

	"github.com/spiral/errors"
)

// Config is a Reload configuration point.
type Config struct {
	// Interval is a global refresh interval
	Interval time.Duration

	// Patterns is a global file patterns to watch. It will be applied to every directory in project
	Patterns []string

	// Services is set of services which would be reloaded in case of FS changes
	Services map[string]ServiceConfig
}

type ServiceConfig struct {
	// Enabled indicates that service must be watched, doest not required when any other option specified
	Enabled bool

	// Recursive is options to use nested files from root folder
	Recursive bool

	// Patterns is per-service specific files to watch
	Patterns []string

	// Dirs is per-service specific dirs which will be combined with Patterns
	Dirs []string

	// Ignore is set of files which would not be watched
	Ignore []string
}

// InitDefaults sets missing values to their default values.
func InitDefaults(c *Config) {
	c.Interval = time.Second
	c.Patterns = []string{".php"}
}

// Valid validates the configuration.
func (c *Config) Valid() error {
	const op = errors.Op("reload_plugin_valid")
	if c.Interval < time.Second {
		return errors.E(op, errors.Str("too short interval"))
	}

	if c.Services == nil {
		return errors.E(op, errors.Str("should add at least 1 service"))
	} else if len(c.Services) == 0 {
		return errors.E(op, errors.Str("service initialized, however, no config added"))
	}

	return nil
}