| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- // Copyright 2018-present the CoreDHCP Authors. All rights reserved
- // This source code is licensed under the MIT license found in the
- // LICENSE file in the root directory of this source tree.
- {{/* This file is the template source. The following comment obviously doesn't apply here */ -}}
- // This is a generated file, edits should be made in the corresponding source file
- // And this file regenerated using `coredhcp-generator -from core-plugins.txt`
- package main
- import (
- "flag"
- "fmt"
- "io/ioutil"
- "time"
- "github.com/coredhcp/coredhcp/config"
- "github.com/coredhcp/coredhcp/logger"
- "github.com/coredhcp/coredhcp/server"
- "github.com/coredhcp/coredhcp/plugins"
- {{- range $plugin := .}}
- {{- /* We import all plugins as pl_<pluginname> to avoid conflicts with reserved keywords */}}
- {{importname $plugin}} "{{$plugin}}"
- {{- end}}
- "github.com/sirupsen/logrus"
- )
- var (
- flagLogFile = flag.String("logfile", "", "Name of the log file to append to. Default: stdout/stderr only")
- flagLogNoStdout = flag.Bool("nostdout", false, "Disable logging to stdout/stderr")
- flagLogLevel = flag.String("loglevel", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
- flagConfig = flag.String("conf", "", "Use this configuration file instead of the default location")
- )
- var logLevels = map[string]func(*logrus.Logger){
- "none": func(l *logrus.Logger) { l.SetOutput(ioutil.Discard) },
- "debug": func(l *logrus.Logger) { l.SetLevel(logrus.DebugLevel) },
- "info": func(l *logrus.Logger) { l.SetLevel(logrus.InfoLevel) },
- "warning": func(l *logrus.Logger) { l.SetLevel(logrus.WarnLevel) },
- "error": func(l *logrus.Logger) { l.SetLevel(logrus.ErrorLevel) },
- "fatal": func(l *logrus.Logger) { l.SetLevel(logrus.FatalLevel) },
- }
- func getLogLevels() []string {
- var levels []string
- for k := range logLevels {
- levels = append(levels, k)
- }
- return levels
- }
- var desiredPlugins = []*plugins.Plugin{
- {{- range $plugin := .}}
- &{{importname $plugin}}.Plugin,
- {{- end}}
- }
- func main() {
- flag.Parse()
- log := logger.GetLogger("main")
- fn, ok := logLevels[*flagLogLevel]
- if !ok {
- log.Fatalf("Invalid log level '%s'. Valid log levels are %v", *flagLogLevel, getLogLevels())
- }
- fn(log.Logger)
- log.Infof("Setting log level to '%s'", *flagLogLevel)
- if *flagLogFile != "" {
- log.Infof("Logging to file %s", *flagLogFile)
- logger.WithFile(log, *flagLogFile)
- }
- if *flagLogNoStdout {
- log.Infof("Disabling logging to stdout/stderr")
- logger.WithNoStdOutErr(log)
- }
- config, err := config.Load(*flagConfig)
- if err != nil {
- log.Fatalf("Failed to load configuration: %v", err)
- }
- // register plugins
- for _, plugin := range desiredPlugins {
- if err := plugins.RegisterPlugin(plugin); err != nil {
- log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
- }
- }
- // start server
- srv, err := server.Start(config)
- if err != nil {
- log.Fatal(err)
- }
- if err := srv.Wait(); err != nil {
- log.Print(err)
- }
- time.Sleep(time.Second)
- }
|