]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/triggers.go
Use SPDX license identifiers
[safcm/safcm.git] / cmd / safcm / config / triggers.go
1 // Config: parse triggers.yaml
2
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 // Copyright (C) 2021-2024  Simon Ruderich
5
6 package config
7
8 import (
9         "fmt"
10         "os"
11         "path/filepath"
12
13         "gopkg.in/yaml.v2"
14
15         "ruderich.org/simon/safcm"
16 )
17
18 func LoadTriggers(group string, files map[string]*safcm.File) error {
19         path := filepath.Join(group, "triggers.yaml")
20
21         var triggers map[string][]string
22         x, err := os.ReadFile(path)
23         if err != nil {
24                 if os.IsNotExist(err) {
25                         return nil
26                 }
27                 return err
28         }
29         err = yaml.UnmarshalStrict(x, &triggers)
30         if err != nil {
31                 return fmt.Errorf("%s: failed to load: %v", path, err)
32         }
33
34         for p, x := range triggers {
35                 f, ok := files[p]
36                 if !ok {
37                         return fmt.Errorf("%s: %q does not exist in files/",
38                                 path, p)
39                 }
40                 f.TriggerCommands = x
41         }
42
43         return nil
44 }