main.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. serviceFlag := flag.String("service", "", "Control the system service.")
  18. configPath := flag.String("config", "", "Path to either a file or directory to load configuration from")
  19. configTest := flag.Bool("test", false, "Test the config and print the end result. Non zero exit indicates a faulty config")
  20. printVersion := flag.Bool("version", false, "Print version")
  21. printUsage := flag.Bool("help", false, "Print command line usage")
  22. flag.Parse()
  23. if *printVersion {
  24. fmt.Printf("Version: %s\n", Build)
  25. os.Exit(0)
  26. }
  27. if *printUsage {
  28. flag.Usage()
  29. os.Exit(0)
  30. }
  31. if *serviceFlag != "" {
  32. doService(configPath, configTest, Build, serviceFlag)
  33. os.Exit(1)
  34. }
  35. if *configPath == "" {
  36. fmt.Println("-config flag must be set")
  37. flag.Usage()
  38. os.Exit(1)
  39. }
  40. l := logrus.New()
  41. l.Out = os.Stdout
  42. c := config.NewC(l)
  43. err := c.Load(*configPath)
  44. if err != nil {
  45. fmt.Printf("failed to load config: %s", err)
  46. os.Exit(1)
  47. }
  48. ctrl, err := nebula.Main(c, *configTest, Build, l, nil)
  49. switch v := err.(type) {
  50. case nebula.ContextualError:
  51. v.Log(l)
  52. os.Exit(1)
  53. case error:
  54. l.WithError(err).Error("Failed to start")
  55. os.Exit(1)
  56. }
  57. if !*configTest {
  58. ctrl.Start()
  59. ctrl.ShutdownBlock()
  60. }
  61. os.Exit(0)
  62. }