Skip to content

Commit ef68dc4

Browse files
committed
Use CLI flags parser from Icinga Go Library
1 parent f3ad7bd commit ef68dc4

File tree

4 files changed

+52
-75
lines changed

4 files changed

+52
-75
lines changed

cmd/icinga-notifications/main.go

+2-31
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package main
22

33
import (
44
"context"
5-
"flag"
6-
"fmt"
75
"github.com/icinga/icinga-go-library/database"
86
"github.com/icinga/icinga-go-library/logging"
97
"github.com/icinga/icinga-go-library/utils"
@@ -16,45 +14,18 @@ import (
1614
"github.com/icinga/icinga-notifications/internal/listener"
1715
"github.com/icinga/icinga-notifications/internal/object"
1816
"github.com/okzk/sdnotify"
19-
"os"
2017
"os/signal"
21-
"runtime"
2218
"syscall"
2319
"time"
2420
)
2521

2622
func main() {
27-
var configPath string
28-
var showVersion bool
29-
30-
flag.StringVar(&configPath, "config", internal.SysConfDir+"/icinga-notifications/config.yml", "path to config file")
31-
flag.BoolVar(&showVersion, "version", false, "print version")
32-
flag.Parse()
33-
34-
if showVersion {
35-
// reuse internal.Version.print() once the project name is configurable
36-
fmt.Println("Icinga Notifications version:", internal.Version.Version)
37-
fmt.Println()
38-
39-
fmt.Println("Build information:")
40-
fmt.Printf(" Go version: %s (%s, %s)\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
41-
if internal.Version.Commit != "" {
42-
fmt.Println(" Git commit:", internal.Version.Commit)
43-
}
44-
return
45-
}
46-
47-
err := daemon.LoadConfig(configPath)
48-
if err != nil {
49-
_, _ = fmt.Fprintln(os.Stderr, "cannot load config:", err)
50-
os.Exit(1)
51-
}
52-
23+
daemon.ParseFlagsAndConfig()
5324
conf := daemon.Config()
5425

5526
logs, err := logging.NewLoggingFromConfig("icinga-notifications", conf.Logging)
5627
if err != nil {
57-
utils.PrintErrorThenExit(err, 1)
28+
utils.PrintErrorThenExit(err, daemon.ExitFailure)
5829
}
5930

6031
logger := logs.GetLogger()

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ require (
66
github.com/creasty/defaults v1.7.0
77
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43
88
github.com/emersion/go-smtp v0.21.3
9-
github.com/goccy/go-yaml v1.11.3
109
github.com/google/uuid v1.6.0
1110
github.com/hashicorp/golang-lru/v2 v2.0.7
12-
github.com/icinga/icinga-go-library v0.2.0
11+
github.com/icinga/icinga-go-library v0.3.0
1312
github.com/jhillyerd/enmime v1.2.0
1413
github.com/jmoiron/sqlx v1.4.0
1514
github.com/okzk/sdnotify v0.0.0-20180710141335-d9becc38acbd
@@ -27,6 +26,7 @@ require (
2726
github.com/davecgh/go-spew v1.1.1 // indirect
2827
github.com/fatih/color v1.13.0 // indirect
2928
github.com/go-sql-driver/mysql v1.8.1 // indirect
29+
github.com/goccy/go-yaml v1.11.3 // indirect
3030
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f // indirect
3131
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect
3232
github.com/jessevdk/go-flags v1.5.0 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
3333
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3434
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
3535
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
36-
github.com/icinga/icinga-go-library v0.2.0 h1:1or5s3KMEJGdhFbMzlN8NPw1NCd/3ntsKLw5et4/9XI=
37-
github.com/icinga/icinga-go-library v0.2.0/go.mod h1:YN7XJN3W0FodD+j4kirO89zk2tgvanXWt1RMV8UgOLo=
36+
github.com/icinga/icinga-go-library v0.3.0 h1:BeoomAiQC5RTRWCNqNkgbdTGxQ7ZFfkruR4HCSn5e0k=
37+
github.com/icinga/icinga-go-library v0.3.0/go.mod h1:YN7XJN3W0FodD+j4kirO89zk2tgvanXWt1RMV8UgOLo=
3838
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 h1:iCHtR9CQyktQ5+f3dMVZfwD2KWJUgm7M0gdL9NGr8KA=
3939
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
4040
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=

internal/daemon/config.go

+46-40
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ package daemon
33
import (
44
"errors"
55
"github.com/creasty/defaults"
6-
"github.com/goccy/go-yaml"
6+
"github.com/icinga/icinga-go-library/config"
77
"github.com/icinga/icinga-go-library/database"
88
"github.com/icinga/icinga-go-library/logging"
9+
"github.com/icinga/icinga-go-library/utils"
910
"github.com/icinga/icinga-notifications/internal"
1011
"os"
1112
"time"
1213
)
1314

15+
const (
16+
ExitSuccess = 0
17+
ExitFailure = 1
18+
)
19+
1420
type ConfigFile struct {
1521
Listen string `yaml:"listen" default:"localhost:5680"`
1622
DebugPassword string `yaml:"debug-password"`
@@ -31,62 +37,62 @@ func (c *ConfigFile) SetDefaults() {
3137
// Assert interface compliance.
3238
var _ defaults.Setter = (*ConfigFile)(nil)
3339

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
4143
}
42-
43-
cfg, err := fromFile(path)
44-
if err != nil {
44+
if err := c.Logging.Validate(); err != nil {
4545
return err
4646
}
4747

48-
config = cfg
49-
5048
return nil
5149
}
5250

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"`
5657
}
5758

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")
6268
}
63-
defer func() { _ = f.Close() }()
6469

65-
var c ConfigFile
70+
return daemonConfig
71+
}
6672

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+
}
7081

71-
d := yaml.NewDecoder(f)
72-
if err := d.Decode(&c); err != nil {
73-
return nil, err
82+
utils.PrintErrorThenExit(err, ExitFailure)
7483
}
7584

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)
7888
}
7989

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+
}
8295

83-
func (c *ConfigFile) Validate() error {
84-
if err := c.Database.Validate(); err != nil {
85-
return err
96+
utils.PrintErrorThenExit(err, ExitFailure)
8697
}
87-
if err := c.Logging.Validate(); err != nil {
88-
return err
89-
}
90-
91-
return nil
9298
}

0 commit comments

Comments
 (0)