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