]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/commands.go
Use SPDX license identifiers
[safcm/safcm.git] / cmd / safcm / config / commands.go
1 // Config: parse commands.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 LoadCommands(group string) ([]*safcm.Command, error) {
19         path := filepath.Join(group, "commands.yaml")
20
21         var cmds []string
22         x, err := os.ReadFile(path)
23         if err != nil {
24                 if os.IsNotExist(err) {
25                         return nil, nil
26                 }
27                 return nil, err
28         }
29         err = yaml.UnmarshalStrict(x, &cmds)
30         if err != nil {
31                 return nil, fmt.Errorf("%s: failed to load: %v", path, err)
32         }
33
34         var res []*safcm.Command
35         for _, x := range cmds {
36                 res = append(res, &safcm.Command{
37                         OrigGroup: group,
38                         Cmd:       x,
39                 })
40         }
41         return res, nil
42 }