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