main.go 1.6 KB

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