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