2019-12-13 18:08:26 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-01-01 15:58:27 +00:00
|
|
|
|
|
|
|
"bloat/model"
|
2019-12-13 18:08:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
ListenAddress string
|
|
|
|
ClientName string
|
|
|
|
ClientScope string
|
|
|
|
ClientWebsite string
|
|
|
|
StaticDirectory string
|
2020-01-28 20:49:58 +00:00
|
|
|
TemplatesPath string
|
2019-12-13 18:08:26 +00:00
|
|
|
DatabasePath string
|
2019-12-25 16:38:47 +00:00
|
|
|
CustomCSS string
|
2019-12-26 11:25:29 +00:00
|
|
|
PostFormats []model.PostFormat
|
2020-01-28 20:49:58 +00:00
|
|
|
LogFile string
|
2019-12-13 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) IsValid() bool {
|
|
|
|
if len(c.ListenAddress) < 1 ||
|
|
|
|
len(c.ClientName) < 1 ||
|
|
|
|
len(c.ClientScope) < 1 ||
|
|
|
|
len(c.ClientWebsite) < 1 ||
|
|
|
|
len(c.StaticDirectory) < 1 ||
|
2020-01-28 20:49:58 +00:00
|
|
|
len(c.TemplatesPath) < 1 ||
|
2019-12-13 18:08:26 +00:00
|
|
|
len(c.DatabasePath) < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(r io.Reader) (c *config, err error) {
|
2020-01-28 20:49:58 +00:00
|
|
|
c = new(config)
|
2019-12-13 18:08:26 +00:00
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
|
|
|
|
if len(line) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
index := strings.IndexRune(line, '#')
|
|
|
|
if index == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
index = strings.IndexRune(line, '=')
|
|
|
|
if index < 1 {
|
|
|
|
return nil, errors.New("invalid config key")
|
|
|
|
}
|
|
|
|
|
|
|
|
key := strings.TrimSpace(line[:index])
|
|
|
|
val := strings.TrimSpace(line[index+1 : len(line)])
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "listen_address":
|
|
|
|
c.ListenAddress = val
|
|
|
|
case "client_name":
|
|
|
|
c.ClientName = val
|
|
|
|
case "client_scope":
|
|
|
|
c.ClientScope = val
|
|
|
|
case "client_website":
|
|
|
|
c.ClientWebsite = val
|
|
|
|
case "static_directory":
|
|
|
|
c.StaticDirectory = val
|
2020-01-28 20:49:58 +00:00
|
|
|
case "templates_path":
|
|
|
|
c.TemplatesPath = val
|
2019-12-13 18:08:26 +00:00
|
|
|
case "database_path":
|
|
|
|
c.DatabasePath = val
|
2019-12-25 16:38:47 +00:00
|
|
|
case "custom_css":
|
|
|
|
c.CustomCSS = val
|
2019-12-26 11:25:29 +00:00
|
|
|
case "post_formats":
|
|
|
|
vals := strings.Split(val, ",")
|
|
|
|
var formats []model.PostFormat
|
|
|
|
for _, v := range vals {
|
|
|
|
pair := strings.Split(v, ":")
|
|
|
|
if len(pair) != 2 {
|
|
|
|
return nil, errors.New("invalid config key " + key)
|
|
|
|
}
|
|
|
|
n := strings.TrimSpace(pair[0])
|
|
|
|
t := strings.TrimSpace(pair[1])
|
|
|
|
if len(n) < 1 || len(t) < 1 {
|
|
|
|
return nil, errors.New("invalid config key " + key)
|
|
|
|
}
|
|
|
|
formats = append(formats, model.PostFormat{
|
|
|
|
Name: n,
|
|
|
|
Type: t,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
c.PostFormats = formats
|
2020-01-28 20:49:58 +00:00
|
|
|
case "log_file":
|
|
|
|
c.LogFile = val
|
2019-12-13 18:08:26 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("invliad config key " + key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseFile(file string) (c *config, err error) {
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
info, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil, errors.New("invalid config file")
|
|
|
|
}
|
|
|
|
|
|
|
|
return Parse(f)
|
|
|
|
}
|