]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/config.go
Use SPDX license identifiers
[safcm/safcm.git] / cmd / safcm / config / config.go
1 // Config: parse config.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
12         "gopkg.in/yaml.v2"
13
14         "ruderich.org/simon/safcm"
15 )
16
17 type Config struct {
18         DryRun    bool           `yaml:"-"` // set via command line
19         Quiet     bool           `yaml:"-"` // set via command line
20         LogLevel  safcm.LogLevel `yaml:"-"` // set via command line
21         SshConfig string         `yaml:"-"` // set via command line
22
23         DetectGroups  []string `yaml:"detect_groups"`
24         GroupPriority []string `yaml:"group_priority"`
25
26         SshUser string `yaml:"ssh_user"`
27 }
28
29 func LoadConfig() (*Config, error) {
30         const path = "config.yaml"
31
32         var cfg Config
33         x, err := os.ReadFile(path)
34         if err != nil {
35                 // This file is optional
36                 if os.IsNotExist(err) {
37                         return &cfg, nil
38                 }
39                 return nil, err
40         }
41         err = yaml.UnmarshalStrict(x, &cfg)
42         if err != nil {
43                 return nil, fmt.Errorf("%s: failed to load: %v", path, err)
44         }
45         return &cfg, nil
46 }