Update config
[bloat] / config / config.go
1 package config
2
3 import (
4         "bufio"
5         "errors"
6         "io"
7         "os"
8         "strings"
9
10         "bloat/model"
11 )
12
13 type config struct {
14         ListenAddress        string
15         ClientName           string
16         ClientScope          string
17         ClientWebsite        string
18         StaticDirectory      string
19         TemplatesPath string
20         DatabasePath         string
21         CustomCSS            string
22         PostFormats          []model.PostFormat
23         LogFile              string
24 }
25
26 func (c *config) IsValid() bool {
27         if len(c.ListenAddress) < 1 ||
28                 len(c.ClientName) < 1 ||
29                 len(c.ClientScope) < 1 ||
30                 len(c.ClientWebsite) < 1 ||
31                 len(c.StaticDirectory) < 1 ||
32                 len(c.TemplatesPath) < 1 ||
33                 len(c.DatabasePath) < 1 {
34                 return false
35         }
36         return true
37 }
38
39 func Parse(r io.Reader) (c *config, err error) {
40         c = new(config)
41         scanner := bufio.NewScanner(r)
42         for scanner.Scan() {
43                 line := strings.TrimSpace(scanner.Text())
44
45                 if len(line) < 1 {
46                         continue
47                 }
48
49                 index := strings.IndexRune(line, '#')
50                 if index == 0 {
51                         continue
52                 }
53
54                 index = strings.IndexRune(line, '=')
55                 if index < 1 {
56                         return nil, errors.New("invalid config key")
57                 }
58
59                 key := strings.TrimSpace(line[:index])
60                 val := strings.TrimSpace(line[index+1 : len(line)])
61
62                 switch key {
63                 case "listen_address":
64                         c.ListenAddress = val
65                 case "client_name":
66                         c.ClientName = val
67                 case "client_scope":
68                         c.ClientScope = val
69                 case "client_website":
70                         c.ClientWebsite = val
71                 case "static_directory":
72                         c.StaticDirectory = val
73                 case "templates_path":
74                         c.TemplatesPath = val
75                 case "database_path":
76                         c.DatabasePath = val
77                 case "custom_css":
78                         c.CustomCSS = val
79                 case "post_formats":
80                         vals := strings.Split(val, ",")
81                         var formats []model.PostFormat
82                         for _, v := range vals {
83                                 pair := strings.Split(v, ":")
84                                 if len(pair) != 2 {
85                                         return nil, errors.New("invalid config key " + key)
86                                 }
87                                 n := strings.TrimSpace(pair[0])
88                                 t := strings.TrimSpace(pair[1])
89                                 if len(n) < 1 || len(t) < 1 {
90                                         return nil, errors.New("invalid config key " + key)
91                                 }
92                                 formats = append(formats, model.PostFormat{
93                                         Name: n,
94                                         Type: t,
95                                 })
96                         }
97                         c.PostFormats = formats
98                 case "log_file":
99                         c.LogFile = val
100                 default:
101                         return nil, errors.New("invliad config key " + key)
102                 }
103         }
104
105         return
106 }
107
108 func ParseFile(file string) (c *config, err error) {
109         f, err := os.Open(file)
110         if err != nil {
111                 return
112         }
113         defer f.Close()
114
115         info, err := f.Stat()
116         if err != nil {
117                 return
118         }
119
120         if info.IsDir() {
121                 return nil, errors.New("invalid config file")
122         }
123
124         return Parse(f)
125 }