mirror of
https://github.com/Icinga/icingadb.git
synced 2026-05-28 04:35:54 -04:00
Move internal/config#ParseFlags() to config#ParseFlags()
This commit is contained in:
parent
8beb8616ad
commit
e5a830cabc
3 changed files with 26 additions and 19 deletions
|
|
@ -3,7 +3,8 @@ package command
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/icinga/icingadb/internal"
|
||||
"github.com/icinga/icingadb/internal/config"
|
||||
icingadbconfig "github.com/icinga/icingadb/internal/config"
|
||||
"github.com/icinga/icingadb/pkg/config"
|
||||
"github.com/icinga/icingadb/pkg/database"
|
||||
"github.com/icinga/icingadb/pkg/icingaredis"
|
||||
"github.com/icinga/icingadb/pkg/icingaredis/telemetry"
|
||||
|
|
@ -16,13 +17,13 @@ import (
|
|||
|
||||
// Command provides factories for creating Redis and Database connections from Config.
|
||||
type Command struct {
|
||||
Flags *config.Flags
|
||||
Config *config.Config
|
||||
Flags *icingadbconfig.Flags
|
||||
Config *icingadbconfig.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()
|
||||
flags, err := config.ParseFlags[icingadbconfig.Flags]()
|
||||
if err != nil {
|
||||
var cliErr *goflags.Error
|
||||
if errors.As(err, &cliErr) && cliErr.Type == goflags.ErrHelp {
|
||||
|
|
@ -37,7 +38,7 @@ func New() *Command {
|
|||
os.Exit(0)
|
||||
}
|
||||
|
||||
cfg, err := config.FromYAMLFile(flags.Config)
|
||||
cfg, err := icingadbconfig.FromYAMLFile(flags.Config)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(2)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import (
|
|||
"github.com/goccy/go-yaml"
|
||||
"github.com/icinga/icingadb/pkg/database"
|
||||
"github.com/icinga/icingadb/pkg/logging"
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/pkg/errors"
|
||||
"os"
|
||||
)
|
||||
|
|
@ -69,16 +68,3 @@ func FromYAMLFile(name string) (*Config, error) {
|
|||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// ParseFlags parses CLI flags and
|
||||
// returns a Flags value created from them.
|
||||
func ParseFlags() (*Flags, error) {
|
||||
f := &Flags{}
|
||||
parser := flags.NewParser(f, flags.Default)
|
||||
|
||||
if _, err := parser.Parse(); err != nil {
|
||||
return nil, errors.Wrap(err, "can't parse CLI flags")
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
|
|
|||
20
pkg/config/config.go
Normal file
20
pkg/config/config.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// ParseFlags parses CLI flags and
|
||||
// returns a new value of type T created from them.
|
||||
func ParseFlags[T any]() (*T, error) {
|
||||
var f T
|
||||
|
||||
parser := flags.NewParser(&f, flags.Default)
|
||||
|
||||
if _, err := parser.Parse(); err != nil {
|
||||
return nil, errors.Wrap(err, "can't parse CLI flags")
|
||||
}
|
||||
|
||||
return &f, nil
|
||||
}
|
||||
Loading…
Reference in a new issue