forked from isbm/go-deb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.go
58 lines (49 loc) · 1.08 KB
/
triggers.go
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 deb
import (
"bufio"
"fmt"
"strings"
)
type Trigger struct {
directive string
name string
}
func NewTrigger() *Trigger {
return new(Trigger)
}
func (t *Trigger) Directive() string {
return t.directive
}
func (t *Trigger) Name() string {
return t.name
}
type TriggerFile struct {
triggers []Trigger
}
func NewTriggerFile() *TriggerFile {
tf := new(TriggerFile)
tf.triggers = make([]Trigger, 0)
return tf
}
// Parse triggers file
func (tf *TriggerFile) parse(data []byte) error {
scn := bufio.NewScanner(strings.NewReader(string(data)))
for scn.Scan() {
line := strings.TrimSpace(scn.Text())
if line != "" && !strings.HasPrefix(line, "#") {
dn := strings.SplitN(strings.Split(line, "#")[0], " ", 2) // Trim comments
if len(dn) == 2 {
t := NewTrigger()
t.directive, t.name = dn[0], dn[1]
tf.triggers = append(tf.triggers, *t)
} else {
return fmt.Errorf("Could not parse name and directive in '%v' line.", line)
}
}
}
return nil
}
// Triggers return known parsed triggers
func (tf TriggerFile) Triggers() []Trigger {
return tf.triggers
}