z_mirror/pesticide

git clone https://git.raiguard.me/z_mirror/pesticide

Caleb HeuerRemove the router, simplify project structure9c1a93d

trunk
843 B45 linesraw
1package config
2
3import (
4	"encoding/json"
5	"errors"
6	"os"
7)
8
9type Config struct {
10	Adapters map[string]AdapterConfig
11}
12
13type AdapterConfig struct {
14	Cmd  *string
15	Args json.RawMessage
16	Addr *string
17	ID   string `json:",omitempty"`
18}
19
20func New(path string) Config {
21	file, err := os.ReadFile(path)
22	if err != nil {
23		panic(err)
24	}
25	var config Config
26	if err = json.Unmarshal(file, &config); err != nil {
27		panic(err)
28	}
29
30	if len(config.Adapters) == 0 {
31		panic(errors.New("No adapters were specified"))
32	}
33	for id, adapter := range config.Adapters {
34		if adapter.Addr == nil && adapter.Cmd == nil {
35			panic(errors.New("Adapters must have an address or command to run"))
36		}
37		if adapter.Cmd != nil {
38			expanded := os.ExpandEnv(*adapter.Cmd)
39			adapter.Cmd = &expanded
40		}
41		adapter.ID = id
42		config.Adapters[id] = adapter
43	}
44	return config
45}