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