~nvkv/amitm

ref: e902bc5a4c8d9b6120845f08ff9e2887d0d7fc58 amitm/internal/config/v1/config.go -rw-r--r-- 668 bytes
e902bc5aSemyon Novikov Initial commit 3 months ago
                                                                                
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
package config

import (
	"github.com/pelletier/go-toml"
	"io/ioutil"
)

type PipelineStep struct {
	Exec string
}

type Rule struct {
	Name     string
	Action   string
	Glob     string
	Pipeline []PipelineStep
}

type Config struct {
	actionmap map[string][]*Rule
	Rules     []Rule
}

func ReadConfig(path string) (*Config, error) {
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	cfg := &Config{}
	err = toml.Unmarshal(data, cfg)
	if err != nil {
		return nil, err
	}
	cfg.actionmap = make(map[string][]*Rule)
	for _, rule := range cfg.Rules {
		cfg.actionmap[rule.Action] = append(cfg.actionmap[rule.Action], &rule)
	}
	return cfg, nil
}