main.go 1.5 KB

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