main.go 1.5 KB

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