main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "github.com/slackhq/nebula/util"
  10. )
  11. // A version string that can be set with
  12. //
  13. // -ldflags "-X main.Build=SOMEVERSION"
  14. //
  15. // at compile-time.
  16. var Build string
  17. func main() {
  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 *configPath == "" {
  32. fmt.Println("-config flag must be set")
  33. flag.Usage()
  34. os.Exit(1)
  35. }
  36. l := logrus.New()
  37. l.Out = os.Stdout
  38. c := config.NewC(l)
  39. err := c.Load(*configPath)
  40. if err != nil {
  41. fmt.Printf("failed to load config: %s", err)
  42. os.Exit(1)
  43. }
  44. ctrl, err := nebula.Main(c, *configTest, Build, l, nil)
  45. switch v := err.(type) {
  46. case util.ContextualError:
  47. v.Log(l)
  48. os.Exit(1)
  49. case error:
  50. l.WithError(err).Error("Failed to start")
  51. os.Exit(1)
  52. }
  53. if !*configTest {
  54. ctrl.Start()
  55. ctrl.ShutdownBlock()
  56. }
  57. os.Exit(0)
  58. }