~nvkv/amitm

ref: e0de9939dec0cfada35fb6950b16efeba3d48dc2 amitm/internal/config/v1/config.go -rw-r--r-- 668 bytes
e0de9939Semyon Novikov Wrap LICENSE file at 80th column to make it nice 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
}