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.

98 lines
2.1 KiB

  1. //nolint: gosec
  2. package main
  3. import (
  4. "fmt"
  5. "math"
  6. "math/rand"
  7. "os"
  8. "path/filepath"
  9. "github.com/spf13/cobra"
  10. "github.com/tendermint/tendermint/libs/log"
  11. )
  12. const (
  13. randomSeed int64 = 4827085738
  14. )
  15. var logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
  16. func main() {
  17. NewCLI().Run()
  18. }
  19. // CLI is the Cobra-based command-line interface.
  20. type CLI struct {
  21. root *cobra.Command
  22. }
  23. // NewCLI sets up the CLI.
  24. func NewCLI() *CLI {
  25. cli := &CLI{}
  26. cli.root = &cobra.Command{
  27. Use: "generator",
  28. Short: "End-to-end testnet generator",
  29. SilenceUsage: true,
  30. SilenceErrors: true, // we'll output them ourselves in Run()
  31. RunE: func(cmd *cobra.Command, args []string) error {
  32. dir, err := cmd.Flags().GetString("dir")
  33. if err != nil {
  34. return err
  35. }
  36. groups, err := cmd.Flags().GetInt("groups")
  37. if err != nil {
  38. return err
  39. }
  40. return cli.generate(dir, groups)
  41. },
  42. }
  43. cli.root.PersistentFlags().StringP("dir", "d", "", "Output directory for manifests")
  44. _ = cli.root.MarkPersistentFlagRequired("dir")
  45. cli.root.PersistentFlags().IntP("groups", "g", 0, "Number of groups")
  46. return cli
  47. }
  48. // generate generates manifests in a directory.
  49. func (cli *CLI) generate(dir string, groups int) error {
  50. err := os.MkdirAll(dir, 0755)
  51. if err != nil {
  52. return err
  53. }
  54. manifests, err := Generate(rand.New(rand.NewSource(randomSeed)))
  55. if err != nil {
  56. return err
  57. }
  58. if groups <= 0 {
  59. for i, manifest := range manifests {
  60. err = manifest.Save(filepath.Join(dir, fmt.Sprintf("gen-%04d.toml", i)))
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. } else {
  66. groupSize := int(math.Ceil(float64(len(manifests)) / float64(groups)))
  67. for g := 0; g < groups; g++ {
  68. for i := 0; i < groupSize && g*groupSize+i < len(manifests); i++ {
  69. manifest := manifests[g*groupSize+i]
  70. err = manifest.Save(filepath.Join(dir, fmt.Sprintf("gen-group%02d-%04d.toml", g, i)))
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. }
  76. }
  77. return nil
  78. }
  79. // Run runs the CLI.
  80. func (cli *CLI) Run() {
  81. if err := cli.root.Execute(); err != nil {
  82. logger.Error(err.Error())
  83. os.Exit(1)
  84. }
  85. }