icingadb/internal/command/command.go
Julian Brost c6e843146f Add more information to icingadb --version ouput
This commit adds the build information (Go version, Git commit) and system
information (distribution with version) to the output of `icingadb --version`.

When exporting the source code using `git archive`, Git will insert the version
information using the export-subst attribute (adds both a pretty version string
like `git describe` and the commit hash).

`go build` also adds some Git commit information to the resulting binary when
building from a Git working directory (only the commit hash and a flag if there
are uncommitted changes, but no pretty version string).

Note that `go build` does not include Git version information in the binary, so
when running directly from a Git working directory, commit information won't be
available in the binary, but when doing this, you should be well aware which
version you're running anyways.

System information is obtained from the `os-release` file.
2022-05-23 16:03:56 +02:00

58 lines
1.4 KiB
Go

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