main.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. serviceFlag := flag.String("service", "", "Control the system service.")
  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 *serviceFlag != "" {
  31. doService(configPath, configTest, Build, serviceFlag)
  32. os.Exit(1)
  33. }
  34. if *configPath == "" {
  35. fmt.Println("-config flag must be set")
  36. flag.Usage()
  37. os.Exit(1)
  38. }
  39. l := logrus.New()
  40. l.Out = os.Stdout
  41. config := nebula.NewConfig(l)
  42. err := config.Load(*configPath)
  43. if err != nil {
  44. fmt.Printf("failed to load config: %s", err)
  45. os.Exit(1)
  46. }
  47. c, err := nebula.Main(config, *configTest, Build, l, nil)
  48. switch v := err.(type) {
  49. case nebula.ContextualError:
  50. v.Log(l)
  51. os.Exit(1)
  52. case error:
  53. l.WithError(err).Error("Failed to start")
  54. os.Exit(1)
  55. }
  56. if !*configTest {
  57. c.Start()
  58. c.ShutdownBlock()
  59. }
  60. os.Exit(0)
  61. }