aboutsummaryrefslogtreecommitdiff
path: root/config/config.go
blob: 62e7136af1611ac3fd446fd3c8e123152c82a9b8 (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
59
60
61
62
package config

import (
	"fmt"
	"os"
	"regexp"
	"time"
)

type Config struct {
	Domain   string
	Handlers []Handler
	DbPath   string
}

type Handler struct {
	Name              string
	AvatarUrl         string
	AvatarContentType string
	Exec              string
	Rpc               string
	Summary           string
	DedupWindow       time.Duration
	Interval          time.Duration
}

func (c *Config) Validate() error {
	if c.Domain == "" {
		return fmt.Errorf("domain cant be blank")
	}
	if c.DbPath == "" {
		return fmt.Errorf("dbPath cant be blank")
	}
	// TODO do we care?
	if len(c.Handlers) == 0 {
		return fmt.Errorf("no [[handlers]] defined, nothing to do")
	}

	r, _ := regexp.Compile("[A-Za-z0-9]+")

	for _, handler := range c.Handlers {
		if handler.Name == "" {
			return fmt.Errorf("handler name cant be blank")
		}

		matches := r.FindAllString(handler.Name, -1)
		if len(matches) != 1 {
			return fmt.Errorf("handler names can only be [A-Za-z0-9]")
		}
		if handler.Interval == 0 {
			return fmt.Errorf("handler %s needs interval", handler.Name)
		}
		if handler.Exec == "" {
			return fmt.Errorf("handler %s exec cant be blank", handler.Name)
		}
		if _, err := os.Stat(handler.Exec); err != nil {
			return fmt.Errorf("handler %s exec %s does not exist or is not readable", handler.Name, handler.Exec)
		}

	}
	return nil
}