coredhcp.go.template 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "fmt"
  10. "io"
  11. "os"
  12. "github.com/coredhcp/coredhcp/config"
  13. "github.com/coredhcp/coredhcp/logger"
  14. "github.com/coredhcp/coredhcp/server"
  15. "github.com/coredhcp/coredhcp/plugins"
  16. {{- range $plugin := .}}
  17. {{- /* We import all plugins as pl_<pluginname> to avoid conflicts with reserved keywords */}}
  18. {{importname $plugin}} "{{$plugin}}"
  19. {{- end}}
  20. "github.com/sirupsen/logrus"
  21. flag "github.com/spf13/pflag"
  22. )
  23. var (
  24. flagLogFile = flag.StringP("logfile", "l", "", "Name of the log file to append to. Default: stdout/stderr only")
  25. flagLogNoStdout = flag.BoolP("nostdout", "N", false, "Disable logging to stdout/stderr")
  26. flagLogLevel = flag.StringP("loglevel", "L", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
  27. flagConfig = flag.StringP("conf", "c", "", "Use this configuration file instead of the default location")
  28. flagPlugins = flag.BoolP("plugins", "P", false, "list plugins")
  29. )
  30. var logLevels = map[string]func(*logrus.Logger){
  31. "none": func(l *logrus.Logger) { l.SetOutput(io.Discard) },
  32. "debug": func(l *logrus.Logger) { l.SetLevel(logrus.DebugLevel) },
  33. "info": func(l *logrus.Logger) { l.SetLevel(logrus.InfoLevel) },
  34. "warning": func(l *logrus.Logger) { l.SetLevel(logrus.WarnLevel) },
  35. "error": func(l *logrus.Logger) { l.SetLevel(logrus.ErrorLevel) },
  36. "fatal": func(l *logrus.Logger) { l.SetLevel(logrus.FatalLevel) },
  37. }
  38. func getLogLevels() []string {
  39. var levels []string
  40. for k := range logLevels {
  41. levels = append(levels, k)
  42. }
  43. return levels
  44. }
  45. var desiredPlugins = []*plugins.Plugin{
  46. {{- range $plugin := .}}
  47. &{{importname $plugin}}.Plugin,
  48. {{- end}}
  49. }
  50. func main() {
  51. flag.Parse()
  52. if *flagPlugins {
  53. for _, p := range desiredPlugins {
  54. fmt.Println(p.Name)
  55. }
  56. os.Exit(0)
  57. }
  58. log := logger.GetLogger("main")
  59. fn, ok := logLevels[*flagLogLevel]
  60. if !ok {
  61. log.Fatalf("Invalid log level '%s'. Valid log levels are %v", *flagLogLevel, getLogLevels())
  62. }
  63. fn(log.Logger)
  64. log.Infof("Setting log level to '%s'", *flagLogLevel)
  65. if *flagLogFile != "" {
  66. log.Infof("Logging to file %s", *flagLogFile)
  67. logger.WithFile(log, *flagLogFile)
  68. }
  69. if *flagLogNoStdout {
  70. log.Infof("Disabling logging to stdout/stderr")
  71. logger.WithNoStdOutErr(log)
  72. }
  73. config, err := config.Load(*flagConfig)
  74. if err != nil {
  75. log.Fatalf("Failed to load configuration: %v", err)
  76. }
  77. // register plugins
  78. for _, plugin := range desiredPlugins {
  79. if err := plugins.RegisterPlugin(plugin); err != nil {
  80. log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
  81. }
  82. }
  83. // start server
  84. srv, err := server.Start(config)
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. if err := srv.Wait(); err != nil {
  89. log.Error(err)
  90. }
  91. }