1package config
2
3import (
4 "fmt"
5 "io"
6 "strconv"
7
8 "git.gabrielgio.me/cerrado/pkg/u"
9 "git.sr.ht/~emersion/go-scfg"
10)
11
12type (
13 Scan struct {
14 Path string
15 Public bool
16 }
17
18 Configuration struct {
19 Scan *Scan
20 }
21)
22
23func Parse(r io.Reader) (*Configuration, error) {
24 block, err := scfg.Read(r)
25 if err != nil {
26 return nil, err
27 }
28
29 config := defaultConfiguration()
30
31 err = setScan(block, config.Scan)
32 if err != nil {
33 return nil, err
34 }
35
36 return config, nil
37}
38
39func defaultConfiguration() *Configuration {
40 return &Configuration{
41 Scan: &Scan{
42 Public: true,
43 Path: "",
44 },
45 }
46}
47
48func setScan(block scfg.Block, scan *Scan) error {
49 scanDir := block.Get("scan")
50 err := setString(scanDir, &scan.Path)
51 if err != nil {
52 return err
53 }
54
55 public := scanDir.Children.Get("public")
56 return setBool(public, &scan.Public)
57}
58
59func setBool(dir *scfg.Directive, field *bool) error {
60 if dir != nil {
61
62 p1, _ := u.First(dir.Params)
63 v, err := strconv.ParseBool(p1)
64 if err != nil {
65 return fmt.Errorf("Error parsing bool param of %s: %w", dir.Name, err)
66 }
67 *field = v
68 }
69 return nil
70}
71
72func setString(dir *scfg.Directive, field *string) error {
73 if dir != nil {
74 *field, _ = u.First(dir.Params)
75 }
76 return nil
77}