add all files from Hong
This commit is contained in:
104
cmds/coredhcp-generator/coredhcp.go.template
Normal file
104
cmds/coredhcp-generator/coredhcp.go.template
Normal file
@@ -0,0 +1,104 @@
|
||||
// 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 (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"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"
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
var (
|
||||
flagLogFile = flag.StringP("logfile", "l", "", "Name of the log file to append to. Default: stdout/stderr only")
|
||||
flagLogNoStdout = flag.BoolP("nostdout", "N", false, "Disable logging to stdout/stderr")
|
||||
flagLogLevel = flag.StringP("loglevel", "L", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
|
||||
flagConfig = flag.StringP("conf", "c", "", "Use this configuration file instead of the default location")
|
||||
flagPlugins = flag.BoolP("plugins", "P", false, "list plugins")
|
||||
)
|
||||
|
||||
var logLevels = map[string]func(*logrus.Logger){
|
||||
"none": func(l *logrus.Logger) { l.SetOutput(io.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()
|
||||
|
||||
if *flagPlugins {
|
||||
for _, p := range desiredPlugins {
|
||||
fmt.Println(p.Name)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
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.Error(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user