about summary refs log tree commit diff stats
path: root/internal/config/config.go
blob: 55f5efa4189eda71da1da712f66a4b38cf84769d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package config

import (
	"log/slog"
	"maps"
	"net/url"
	"os"
	"searchix/internal/importer"
	"time"

	"github.com/pelletier/go-toml/v2"
	"github.com/pkg/errors"
)

type URL struct {
	*url.URL
}

func (u *URL) UnmarshalText(text []byte) (err error) {
	u.URL, err = url.Parse(string(text))

	if err != nil {
		return errors.WithMessage(err, "could not parse URL")
	}

	return nil
}

type Config struct {
	DataPath string `toml:"data-path"`
	CSP      CSP    `toml:"content-security-policy"`
	Headers  map[string]string
	Sources  map[string]*importer.Source
}

var defaultConfig = Config{
	DataPath: "./data",
	CSP: CSP{
		DefaultSrc: []string{"'self'"},
	},
	Headers: map[string]string{
		"x-content-type-options": "nosniff",
	},
	Sources: map[string]*importer.Source{
		"nixos": {
			Name:          "NixOS",
			Key:           "nixos",
			Enable:        true,
			Type:          importer.Channel,
			Channel:       "nixpkgs",
			ImportPath:    "nixos/release.nix",
			Attribute:     "options",
			OutputPath:    "share/doc/nixos/options.json",
			FetchTimeout:  5 * time.Minute,
			ImportTimeout: 15 * time.Minute,
			Repo: importer.Repository{
				Type:  "github",
				Owner: "NixOS",
				Repo:  "nixpkgs",
			},
		},
		"darwin": {
			Name:          "Darwin",
			Key:           "darwin",
			Enable:        false,
			Type:          importer.Channel,
			Channel:       "nix-darwin",
			ImportPath:    "release.nix",
			Attribute:     "options",
			OutputPath:    "share/doc/darwin/options.json",
			FetchTimeout:  5 * time.Minute,
			ImportTimeout: 15 * time.Minute,
			Repo: importer.Repository{
				Type:  "github",
				Owner: "LnL7",
				Repo:  "nix-darwin",
			},
		},
		"home-manager": {
			Name:          "Home Manager",
			Key:           "home-manager",
			Enable:        false,
			Channel:       "home-manager",
			Type:          importer.Channel,
			ImportPath:    "default.nix",
			Attribute:     "docs.json",
			OutputPath:    "share/doc/home-manager/options.json",
			FetchTimeout:  5 * time.Minute,
			ImportTimeout: 15 * time.Minute,
			Repo: importer.Repository{
				Type:  "github",
				Owner: "nix-community",
				Repo:  "home-manager",
			},
		},
	},
}

func GetConfig(filename string) (*Config, error) {
	config := defaultConfig
	if filename != "" {
		slog.Debug("reading config", "filename", filename)
		f, err := os.Open(filename)
		if err != nil {
			return nil, errors.Wrap(err, "reading config failed")
		}
		defer f.Close()

		dec := toml.NewDecoder(f)
		err = dec.Decode(&config)
		if err != nil {
			var tomlError *toml.DecodeError
			if errors.As(err, &tomlError) {
				return nil, errors.WithMessage(err, tomlError.Error())
			}

			return nil, errors.Wrap(err, "config error")
		}
	}

	maps.DeleteFunc(config.Sources, func(_ string, v *importer.Source) bool {
		return !v.Enable
	})

	return &config, nil
}