main.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "github.com/sirupsen/logrus"
  7. "github.com/slackhq/nebula"
  8. "github.com/slackhq/nebula/config"
  9. )
  10. // A version string that can be set with
  11. //
  12. // -ldflags "-X main.Build=SOMEVERSION"
  13. //
  14. // at compile-time.
  15. var Build string
  16. func main() {
  17. configPath := flag.String("config", "", "Path to either a file or directory to load configuration from")
  18. configTest := flag.Bool("test", false, "Test the config and print the end result. Non zero exit indicates a faulty config")
  19. printVersion := flag.Bool("version", false, "Print version")
  20. printUsage := flag.Bool("help", false, "Print command line usage")
  21. flag.Parse()
  22. if *printVersion {
  23. fmt.Printf("Version: %s\n", Build)
  24. os.Exit(0)
  25. }
  26. if *printUsage {
  27. flag.Usage()
  28. os.Exit(0)
  29. }
  30. if *configPath == "" {
  31. fmt.Println("-config flag must be set")
  32. flag.Usage()
  33. os.Exit(1)
  34. }
  35. l := logrus.New()
  36. l.Out = os.Stdout
  37. c := config.NewC(l)
  38. err := c.Load(*configPath)
  39. if err != nil {
  40. fmt.Printf("failed to load config: %s", err)
  41. os.Exit(1)
  42. }
  43. ctrl, err := nebula.Main(c, *configTest, Build, l, nil)
  44. switch v := err.(type) {
  45. case nebula.ContextualError:
  46. v.Log(l)
  47. os.Exit(1)
  48. case error:
  49. l.WithError(err).Error("Failed to start")
  50. os.Exit(1)
  51. }
  52. if !*configTest {
  53. ctrl.Start()
  54. ctrl.ShutdownBlock()
  55. }
  56. os.Exit(0)
  57. }