coredhcp.go.template 3.1 KB

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