summaryrefslogtreecommitdiffstats
path: root/internal/command
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:40:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:40:59 +0000
commitbc4e624732bd51c0dd1e9529cf228e8c23127732 (patch)
treed95dab8960e9d02d3b95f8653074ad2e54ca207c /internal/command
parentInitial commit. (diff)
downloadicingadb-bc4e624732bd51c0dd1e9529cf228e8c23127732.tar.xz
icingadb-bc4e624732bd51c0dd1e9529cf228e8c23127732.zip
Adding upstream version 1.1.1.upstream/1.1.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'internal/command')
-rw-r--r--internal/command/command.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/internal/command/command.go b/internal/command/command.go
new file mode 100644
index 0000000..a4c76ab
--- /dev/null
+++ b/internal/command/command.go
@@ -0,0 +1,58 @@
+package command
+
+import (
+ "fmt"
+ "github.com/icinga/icingadb/internal"
+ "github.com/icinga/icingadb/pkg/config"
+ "github.com/icinga/icingadb/pkg/icingadb"
+ "github.com/icinga/icingadb/pkg/icingaredis"
+ "github.com/icinga/icingadb/pkg/logging"
+ goflags "github.com/jessevdk/go-flags"
+ "github.com/pkg/errors"
+ "os"
+)
+
+// Command provides factories for creating Redis and Database connections from Config.
+type Command struct {
+ Flags *config.Flags
+ Config *config.Config
+}
+
+// New creates and returns a new Command, parses CLI flags and YAML the config, and initializes the logger.
+func New() *Command {
+ flags, err := config.ParseFlags()
+ if err != nil {
+ var cliErr *goflags.Error
+ if errors.As(err, &cliErr) && cliErr.Type == goflags.ErrHelp {
+ os.Exit(0)
+ }
+
+ os.Exit(2)
+ }
+
+ if flags.Version {
+ internal.Version.Print()
+ os.Exit(0)
+ }
+
+ cfg, err := config.FromYAMLFile(flags.Config)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(2)
+ }
+
+ return &Command{
+ Flags: flags,
+ Config: cfg,
+ }
+}
+
+// Database creates and returns a new icingadb.DB connection from config.Config.
+func (c Command) Database(l *logging.Logger) (*icingadb.DB, error) {
+ return c.Config.Database.Open(l)
+}
+
+// Redis creates and returns a new icingaredis.Client connection from config.Config.
+func (c Command) Redis(l *logging.Logger) (*icingaredis.Client, error) {
+ return c.Config.Redis.NewClient(l)
+}