graylog-groups/main.go
Emmanuel BENOîT 4722223603 Refactored into multiple files
* The main program, command line argument parsing and logging
  initialization remains in main.go
* Configuration structure and loader are in config.go
* LDAP connection and querying is in ldap.go
* Anything that has to do with Graylog, including the privilege mapping,
  is in graylog.go
2021-02-11 20:44:07 +01:00

78 lines
1.8 KiB
Go

package main
import (
"flag"
"github.com/sirupsen/logrus"
)
type (
// This structure contains all values that may be set from the command line.
cliFlags struct {
// The path to the configuration file.
cfgFile string
// The name of the instance, to be used in logs.
instance string
// The log level.
logLevel string
}
)
var (
// The logging context.
log *logrus.Entry
)
// Parse command line options.
func parseCommandLine() cliFlags {
flags := cliFlags{}
flag.StringVar(&flags.cfgFile, "c", "graylog-groups.yml", "Configuration file.")
flag.StringVar(&flags.instance, "i", "", "Instance identifier.")
flag.StringVar(&flags.logLevel, "L", "", "Log level for the logrus library.")
flag.Parse()
return flags
}
// Initialize the logging context.
func getLoggingContext(instance string) *logrus.Entry {
logFields := logrus.Fields{
"application": "graylog",
"component": "graylog-groups",
}
if instance != "" {
logFields["instance"] = instance
}
return logrus.WithFields(logFields)
}
// Configure the log level
func configureLogLevel(cliLevel string) {
var lvl logrus.Level
if cliLevel == "" {
lvl = logrus.InfoLevel
} else {
var err error
lvl, err = logrus.ParseLevel(cliLevel)
if err != nil {
log.WithFields(logrus.Fields{
"level": cliLevel,
}).Warning("Invalid log level on command line")
lvl = logrus.InfoLevel
}
}
log.Logger.SetLevel(lvl)
}
// Configure the logging library based on the various command line flags.
func configureLogging(flags cliFlags) {
log = getLoggingContext(flags.instance)
configureLogLevel(flags.logLevel)
}
func main() {
flags := parseCommandLine()
configureLogging(flags)
configuration := loadConfiguration(flags)
glUsers := getGraylogUsers(configuration.Graylog)
groups := readLdapGroups(configuration)
applyMapping(configuration, glUsers, groups)
}