@@ -3,14 +3,20 @@ package daemon
3
3
import (
4
4
"errors"
5
5
"github.com/creasty/defaults"
6
- "github.com/goccy/ go-yaml "
6
+ "github.com/icinga/icinga- go-library/config "
7
7
"github.com/icinga/icinga-go-library/database"
8
8
"github.com/icinga/icinga-go-library/logging"
9
+ "github.com/icinga/icinga-go-library/utils"
9
10
"github.com/icinga/icinga-notifications/internal"
10
11
"os"
11
12
"time"
12
13
)
13
14
15
+ const (
16
+ ExitSuccess = 0
17
+ ExitFailure = 1
18
+ )
19
+
14
20
type ConfigFile struct {
15
21
Listen string `yaml:"listen" default:"localhost:5680"`
16
22
DebugPassword string `yaml:"debug-password"`
@@ -31,62 +37,62 @@ func (c *ConfigFile) SetDefaults() {
31
37
// Assert interface compliance.
32
38
var _ defaults.Setter = (* ConfigFile )(nil )
33
39
34
- // config holds the configuration state as a singleton. It is used from LoadConfig and Config
35
- var config * ConfigFile
36
-
37
- // LoadConfig loads the daemon config from given path. Call it only once when starting the daemon.
38
- func LoadConfig (path string ) error {
39
- if config != nil {
40
- return errors .New ("config already set" )
40
+ func (c * ConfigFile ) Validate () error {
41
+ if err := c .Database .Validate (); err != nil {
42
+ return err
41
43
}
42
-
43
- cfg , err := fromFile (path )
44
- if err != nil {
44
+ if err := c .Logging .Validate (); err != nil {
45
45
return err
46
46
}
47
47
48
- config = cfg
49
-
50
48
return nil
51
49
}
52
50
53
- // Config returns the config that was loaded while starting the daemon
54
- func Config () * ConfigFile {
55
- return config
51
+ // Flags defines the CLI flags supported by Icinga Notifications.
52
+ type Flags struct {
53
+ // Version decides whether to just print the version and exit.
54
+ Version bool `long:"version" description:"print version and exit"`
55
+ // Config is the path to the config file
56
+ Config string `short:"c" long:"config" description:"path to config file"`
56
57
}
57
58
58
- func fromFile (path string ) (* ConfigFile , error ) {
59
- f , err := os .Open (path )
60
- if err != nil {
61
- return nil , err
59
+ // daemonConfig holds the configuration state as a singleton.
60
+ // It is initialised by the ParseFlagsAndConfig func and exposed through the Config function.
61
+ var daemonConfig * ConfigFile
62
+
63
+ // Config returns the config that was loaded while starting the daemon.
64
+ // Panics when ParseFlagsAndConfig was not called earlier.
65
+ func Config () * ConfigFile {
66
+ if daemonConfig == nil {
67
+ panic ("ERROR: daemon.config not initialized" )
62
68
}
63
- defer func () { _ = f .Close () }()
64
69
65
- var c ConfigFile
70
+ return daemonConfig
71
+ }
66
72
67
- if err := defaults .Set (& c ); err != nil {
68
- return nil , err
69
- }
73
+ // ParseFlagsAndConfig parses the CLI flags provided to the executable and tries to load the config from the YAML file.
74
+ // Prints any error during parsing or config loading to os.Stderr and exits.
75
+ func ParseFlagsAndConfig () {
76
+ flags := Flags {Config : internal .SysConfDir + "/icinga-notifications/config.yml" }
77
+ if err := config .ParseFlags (& flags ); err != nil {
78
+ if errors .Is (err , config .ErrInvalidArgument ) {
79
+ panic (err )
80
+ }
70
81
71
- d := yaml .NewDecoder (f )
72
- if err := d .Decode (& c ); err != nil {
73
- return nil , err
82
+ utils .PrintErrorThenExit (err , ExitFailure )
74
83
}
75
84
76
- if err := c .Validate (); err != nil {
77
- return nil , err
85
+ if flags .Version {
86
+ internal .Version .Print ("Icinga Notifications" )
87
+ os .Exit (ExitSuccess )
78
88
}
79
89
80
- return & c , nil
81
- }
90
+ daemonConfig = new (ConfigFile )
91
+ if err := config .FromYAMLFile (flags .Config , daemonConfig ); err != nil {
92
+ if errors .Is (err , config .ErrInvalidArgument ) {
93
+ panic (err )
94
+ }
82
95
83
- func (c * ConfigFile ) Validate () error {
84
- if err := c .Database .Validate (); err != nil {
85
- return err
96
+ utils .PrintErrorThenExit (err , ExitFailure )
86
97
}
87
- if err := c .Logging .Validate (); err != nil {
88
- return err
89
- }
90
-
91
- return nil
92
98
}
0 commit comments