main.go 1.3 KB

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