From bc4e624732bd51c0dd1e9529cf228e8c23127732 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:40:59 +0200 Subject: Adding upstream version 1.1.1. Signed-off-by: Daniel Baumann --- internal/command/command.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ internal/internal.go | 48 +++++++++++++++++++++++++++++++++++++ internal/version.go | 10 ++++++++ 3 files changed, 116 insertions(+) create mode 100644 internal/command/command.go create mode 100644 internal/internal.go create mode 100644 internal/version.go (limited to 'internal') 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) +} diff --git a/internal/internal.go b/internal/internal.go new file mode 100644 index 0000000..7352d40 --- /dev/null +++ b/internal/internal.go @@ -0,0 +1,48 @@ +package internal + +import ( + "encoding/json" + "github.com/pkg/errors" +) + +// CantDecodeHex wraps the given error with the given string that cannot be hex-decoded. +func CantDecodeHex(err error, s string) error { + return errors.Wrapf(err, "can't decode hex %q", s) +} + +// CantParseFloat64 wraps the given error with the specified string that cannot be parsed into float64. +func CantParseFloat64(err error, s string) error { + return errors.Wrapf(err, "can't parse %q into float64", s) +} + +// CantParseInt64 wraps the given error with the specified string that cannot be parsed into int64. +func CantParseInt64(err error, s string) error { + return errors.Wrapf(err, "can't parse %q into int64", s) +} + +// CantParseUint64 wraps the given error with the specified string that cannot be parsed into uint64. +func CantParseUint64(err error, s string) error { + return errors.Wrapf(err, "can't parse %q into uint64", s) +} + +// CantPerformQuery wraps the given error with the specified query that cannot be executed. +func CantPerformQuery(err error, q string) error { + return errors.Wrapf(err, "can't perform %q", q) +} + +// CantUnmarshalYAML wraps the given error with the designated value, which cannot be unmarshalled into. +func CantUnmarshalYAML(err error, v interface{}) error { + return errors.Wrapf(err, "can't unmarshal YAML into %T", v) +} + +// MarshalJSON calls json.Marshal and wraps any resulting errors. +func MarshalJSON(v interface{}) ([]byte, error) { + b, err := json.Marshal(v) + + return b, errors.Wrapf(err, "can't marshal JSON from %T", v) +} + +// UnmarshalJSON calls json.Unmarshal and wraps any resulting errors. +func UnmarshalJSON(data []byte, v interface{}) error { + return errors.Wrapf(json.Unmarshal(data, v), "can't unmarshal JSON into %T", v) +} diff --git a/internal/version.go b/internal/version.go new file mode 100644 index 0000000..1b7adfb --- /dev/null +++ b/internal/version.go @@ -0,0 +1,10 @@ +package internal + +import ( + "github.com/icinga/icingadb/pkg/version" +) + +// Version contains version and Git commit information. +// +// The placeholders are replaced on `git archive` using the `export-subst` attribute. +var Version = version.Version("1.1.1", "v1.1.1", "6c8b52f2033cd94466863c92d3df632e3c87743c") -- cgit v1.2.3