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