main.go 1.4 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. 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. config := nebula.NewConfig()
  40. err := config.Load(*configPath)
  41. if err != nil {
  42. fmt.Printf("failed to load config: %s", err)
  43. os.Exit(1)
  44. }
  45. l := logrus.New()
  46. l.Out = os.Stdout
  47. err = nebula.Main(config, *configTest, true, Build, l, nil, 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. os.Exit(0)
  57. }