coredhcp.go.template 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. {{/* This file is the template source. The following comment obviously doesn't apply here */ -}}
  5. // This is a generated file, edits should be made in the corresponding source file
  6. // And this file regenerated using `coredhcp-generator -from core-plugins.txt`
  7. package main
  8. import (
  9. "flag"
  10. "fmt"
  11. "io/ioutil"
  12. "time"
  13. "github.com/coredhcp/coredhcp/config"
  14. "github.com/coredhcp/coredhcp/logger"
  15. "github.com/coredhcp/coredhcp/server"
  16. "github.com/coredhcp/coredhcp/plugins"
  17. {{- range $plugin := .}}
  18. {{- /* We import all plugins as pl_<pluginname> to avoid conflicts with reserved keywords */}}
  19. {{importname $plugin}} "{{$plugin}}"
  20. {{- end}}
  21. "github.com/sirupsen/logrus"
  22. )
  23. var (
  24. flagLogFile = flag.String("logfile", "", "Name of the log file to append to. Default: stdout/stderr only")
  25. flagLogNoStdout = flag.Bool("nostdout", false, "Disable logging to stdout/stderr")
  26. flagLogLevel = flag.String("loglevel", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
  27. flagConfig = flag.String("conf", "", "Use this configuration file instead of the default location")
  28. )
  29. var logLevels = map[string]func(*logrus.Logger){
  30. "none": func(l *logrus.Logger) { l.SetOutput(ioutil.Discard) },
  31. "debug": func(l *logrus.Logger) { l.SetLevel(logrus.DebugLevel) },
  32. "info": func(l *logrus.Logger) { l.SetLevel(logrus.InfoLevel) },
  33. "warning": func(l *logrus.Logger) { l.SetLevel(logrus.WarnLevel) },
  34. "error": func(l *logrus.Logger) { l.SetLevel(logrus.ErrorLevel) },
  35. "fatal": func(l *logrus.Logger) { l.SetLevel(logrus.FatalLevel) },
  36. }
  37. func getLogLevels() []string {
  38. var levels []string
  39. for k := range logLevels {
  40. levels = append(levels, k)
  41. }
  42. return levels
  43. }
  44. var desiredPlugins = []*plugins.Plugin{
  45. {{- range $plugin := .}}
  46. &{{importname $plugin}}.Plugin,
  47. {{- end}}
  48. }
  49. func main() {
  50. flag.Parse()
  51. log := logger.GetLogger("main")
  52. fn, ok := logLevels[*flagLogLevel]
  53. if !ok {
  54. log.Fatalf("Invalid log level '%s'. Valid log levels are %v", *flagLogLevel, getLogLevels())
  55. }
  56. fn(log.Logger)
  57. log.Infof("Setting log level to '%s'", *flagLogLevel)
  58. if *flagLogFile != "" {
  59. log.Infof("Logging to file %s", *flagLogFile)
  60. logger.WithFile(log, *flagLogFile)
  61. }
  62. if *flagLogNoStdout {
  63. log.Infof("Disabling logging to stdout/stderr")
  64. logger.WithNoStdOutErr(log)
  65. }
  66. config, err := config.Load(*flagConfig)
  67. if err != nil {
  68. log.Fatalf("Failed to load configuration: %v", err)
  69. }
  70. // register plugins
  71. for _, plugin := range desiredPlugins {
  72. if err := plugins.RegisterPlugin(plugin); err != nil {
  73. log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
  74. }
  75. }
  76. // start server
  77. srv, err := server.Start(config)
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. if err := srv.Wait(); err != nil {
  82. log.Print(err)
  83. }
  84. time.Sleep(time.Second)
  85. }