-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
57 lines (52 loc) · 1.58 KB
/
config.go
File metadata and controls
57 lines (52 loc) · 1.58 KB
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
package main
import (
"fmt"
"regexp"
)
type Config struct {
DeployDir string `json:"deploy_dir"`
Targets []*Target `json:"targets"`
PublicSigningKeyFile string `json:"public_signing_key_file"`
UnsafeSkipSignatureVerification bool `json:"unsafe_skip_signature_verification"`
UpdateInterval int `json:"update_interval"`
}
type Target struct {
Name string
Owner string
Repo string
}
func (c *Config) Validate() error {
if c.DeployDir == "" {
return fmt.Errorf("deploy directory must be set")
}
if c.UpdateInterval <= 0 {
return fmt.Errorf("update interval must be >0")
}
if !c.UnsafeSkipSignatureVerification && c.PublicSigningKeyFile == "" {
return fmt.Errorf("public signing key file must be set if signature verification is enabled")
}
if len(c.Targets) == 0 {
return fmt.Errorf("at least one target must be set")
}
targetNames := make(map[string]bool)
targetNameRegex := regexp.MustCompile(`^[\w-]+$`)
for i, target := range c.Targets {
if target.Name == "" {
return fmt.Errorf("name for target %d must be set", i)
}
if !targetNameRegex.MatchString(target.Name) {
return fmt.Errorf("name for target %d must match pattern %s", i, targetNameRegex.String())
}
if target.Owner == "" {
return fmt.Errorf("owner for target %d must be set", i)
}
if target.Repo == "" {
return fmt.Errorf("repo for target %d must be set", i)
}
if targetNames[target.Name] {
return fmt.Errorf("target %d has duplicate name", i)
}
targetNames[target.Name] = true
}
return nil
}