You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
2.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/signal"
  6. "github.com/hermeznetwork/hermez-node/config"
  7. "github.com/hermeznetwork/hermez-node/log"
  8. "github.com/hermeznetwork/hermez-node/node"
  9. "github.com/hermeznetwork/tracerr"
  10. "github.com/urfave/cli/v2"
  11. )
  12. const (
  13. flagCfg = "cfg"
  14. flagMode = "mode"
  15. modeSync = "sync"
  16. modeCoord = "coord"
  17. )
  18. func cmdInit(c *cli.Context) error {
  19. log.Info("Init")
  20. cfg, err := parseCli(c)
  21. if err != nil {
  22. return tracerr.Wrap(err)
  23. }
  24. fmt.Println("TODO", cfg)
  25. return tracerr.Wrap(err)
  26. }
  27. func cmdRun(c *cli.Context) error {
  28. cfg, err := parseCli(c)
  29. if err != nil {
  30. return tracerr.Wrap(fmt.Errorf("error parsing flags and config: %w", err))
  31. }
  32. node, err := node.NewNode(cfg.mode, cfg.node)
  33. if err != nil {
  34. return tracerr.Wrap(fmt.Errorf("error starting node: %w", err))
  35. }
  36. node.Start()
  37. stopCh := make(chan interface{})
  38. // catch ^C to send the stop signal
  39. ossig := make(chan os.Signal, 1)
  40. signal.Notify(ossig, os.Interrupt)
  41. go func() {
  42. for sig := range ossig {
  43. if sig == os.Interrupt {
  44. stopCh <- nil
  45. }
  46. }
  47. }()
  48. <-stopCh
  49. node.Stop()
  50. return nil
  51. }
  52. // Config is the configuration of the hermez node execution
  53. type Config struct {
  54. mode node.Mode
  55. node *config.Node
  56. }
  57. func parseCli(c *cli.Context) (*Config, error) {
  58. cfg, err := getConfig(c)
  59. if err != nil {
  60. if err := cli.ShowAppHelp(c); err != nil {
  61. panic(err)
  62. }
  63. return nil, tracerr.Wrap(err)
  64. }
  65. return cfg, nil
  66. }
  67. func getConfig(c *cli.Context) (*Config, error) {
  68. var cfg Config
  69. mode := c.String(flagMode)
  70. nodeCfgPath := c.String(flagCfg)
  71. if nodeCfgPath == "" {
  72. return nil, tracerr.Wrap(fmt.Errorf("required flag \"%v\" not set", flagCfg))
  73. }
  74. var err error
  75. switch mode {
  76. case modeSync:
  77. cfg.mode = node.ModeSynchronizer
  78. cfg.node, err = config.LoadNode(nodeCfgPath)
  79. if err != nil {
  80. return nil, tracerr.Wrap(err)
  81. }
  82. case modeCoord:
  83. cfg.mode = node.ModeCoordinator
  84. cfg.node, err = config.LoadCoordinator(nodeCfgPath)
  85. if err != nil {
  86. return nil, tracerr.Wrap(err)
  87. }
  88. default:
  89. return nil, tracerr.Wrap(fmt.Errorf("invalid mode \"%v\"", mode))
  90. }
  91. return &cfg, nil
  92. }
  93. func main() {
  94. app := cli.NewApp()
  95. app.Name = "hermez-node"
  96. app.Version = "0.1.0-alpha"
  97. app.Flags = []cli.Flag{
  98. &cli.StringFlag{
  99. Name: flagMode,
  100. Usage: fmt.Sprintf("Set node `MODE` (can be \"%v\" or \"%v\")", modeSync, modeCoord),
  101. Required: true,
  102. },
  103. &cli.StringFlag{
  104. Name: flagCfg,
  105. Usage: "Node configuration `FILE`",
  106. Required: true,
  107. },
  108. }
  109. app.Commands = []*cli.Command{
  110. {
  111. Name: "init",
  112. Aliases: []string{},
  113. Usage: "Initialize the hermez-node",
  114. Action: cmdInit,
  115. },
  116. {
  117. Name: "run",
  118. Aliases: []string{},
  119. Usage: "Run the hermez-node in the indicated mode",
  120. Action: cmdRun,
  121. },
  122. }
  123. err := app.Run(os.Args)
  124. if err != nil {
  125. fmt.Printf("\nError: %v\n", tracerr.Sprint(err))
  126. os.Exit(1)
  127. }
  128. }