-
Notifications
You must be signed in to change notification settings - Fork 11
/
config.go
145 lines (134 loc) · 3.37 KB
/
config.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
)
type ServiceName string
type ServiceType string
type Command string
func (c Command) String() string {
return string(c)
}
func parseLine(line string, s *Service) error {
if strings.HasPrefix(line, "Needs:") {
specified := strings.Split(strings.TrimPrefix(line, "Needs:"), ",")
for _, nd := range specified {
s.Needs = append(s.Needs, ServiceType(strings.TrimSpace(nd)))
}
} else if strings.HasPrefix(line, "Provides:") {
specified := strings.Split(strings.TrimPrefix(line, "Provides:"), ",")
for _, nd := range specified {
s.Provides = append(s.Provides, ServiceType(strings.TrimSpace(nd)))
}
} else if strings.HasPrefix(line, "Startup:") {
if s.Startup != "" {
return fmt.Errorf("Startup already set")
}
s.Startup = Command(strings.TrimSpace(strings.TrimPrefix(line, "Startup:")))
} else if strings.HasPrefix(line, "Shutdown:") {
if s.Shutdown != "" {
return fmt.Errorf("Shutdown already set")
}
s.Shutdown = Command(strings.TrimSpace(strings.TrimPrefix(line, "Shutdown:")))
} else if strings.HasPrefix(line, "# ") {
if s.Name == "" {
s.Name = ServiceName(strings.TrimSpace(strings.TrimPrefix(line, "# ")))
}
}
return nil
}
func parseSetupLine(line string) (autologin string, persist bool) {
if strings.HasPrefix(line, "Autologin:") {
return strings.TrimSpace(strings.TrimPrefix(line, "Autologin:")), false
} else if strings.HasPrefix(line, "Persist:") {
return "", strings.TrimSpace(strings.TrimPrefix(line, "Persist:")) == "true"
}
return "", false
}
// Parses a single config file into the services it provides
func ParseConfig(r io.Reader) (Service, error) {
s := Service{}
var line string
var err error
scanner := bufio.NewReader(r)
for {
line, err = scanner.ReadString('\n')
switch err {
case io.EOF:
if err := parseLine(line, &s); err != nil {
log.Println(err)
}
return s, nil
case nil:
if err := parseLine(line, &s); err != nil {
log.Println(err)
}
default:
return Service{}, err
}
}
}
// Parses all the config in directory dir return a map of
// providers of ServiceTypes from that directory.
func ParseServiceConfigs(dir string) (map[ServiceType][]*Service, error) {
providers := make(map[ServiceType][]*Service)
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
for _, fstat := range files {
if fstat.IsDir() {
// Mostly to skip "." and ".."
continue
}
f, err := os.Open(dir + "/" + fstat.Name())
if err != nil {
log.Println(err)
}
s, err := ParseConfig(f)
f.Close()
if err != nil {
log.Println(err)
continue
}
for _, t := range s.Provides {
providers[t] = append(providers[t], &s)
}
}
return providers, nil
}
// Parses the file file for "Autologin:" or "Persist:" lines.
func ParseSetupConfig(r io.Reader) (autologins []string, persist bool, err error) {
scanner := bufio.NewReader(r)
for {
line, err2 := scanner.ReadString('\n')
switch err2 {
case io.EOF:
autologin, persist2 := parseSetupLine(line)
if autologin != "" {
autologins = append(autologins, autologin)
}
if persist2 {
persist = persist2
}
return
case nil:
autologin, persist2 := parseSetupLine(line)
if autologin != "" {
autologins = append(autologins, autologin)
}
if persist2 {
persist = persist2
}
default:
err = err2
return
}
}
return
}