Add scripts

This commit is contained in:
mcrakhman 2022-12-05 16:35:14 +01:00 committed by Mikhail Iudin
parent 4eca88b0bc
commit c5b766345c
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0

View File

@ -12,6 +12,10 @@ import (
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers"
"github.com/spf13/cobra" "github.com/spf13/cobra"
_ "github.com/spf13/cobra" _ "github.com/spf13/cobra"
"github.com/zeebo/errs"
"math/rand"
"strings"
"sync"
) )
const CName = "debug.api" const CName = "debug.api"
@ -38,7 +42,7 @@ func New() Service {
func (s *service) Run(ctx context.Context) (err error) { func (s *service) Run(ctx context.Context) (err error) {
rootCmd := &cobra.Command{Use: "debug"} rootCmd := &cobra.Command{Use: "debug"}
clientCmd := &cobra.Command{Use: "client commands"} clientCmd := &cobra.Command{Use: "client commands to be executed on a specified client"}
clientCmd.PersistentFlags().StringP("client", "c", "", "the alias of the client") clientCmd.PersistentFlags().StringP("client", "c", "", "the alias of the client")
clientCmd.MarkFlagRequired("client") clientCmd.MarkFlagRequired("client")
for _, cmd := range s.clientCommands { for _, cmd := range s.clientCommands {
@ -46,7 +50,7 @@ func (s *service) Run(ctx context.Context) (err error) {
} }
rootCmd.AddCommand(clientCmd) rootCmd.AddCommand(clientCmd)
nodeCmd := &cobra.Command{Use: "node commands"} nodeCmd := &cobra.Command{Use: "node commands to be executed on a node"}
nodeCmd.PersistentFlags().StringP("node", "n", "", "the alias of the node") nodeCmd.PersistentFlags().StringP("node", "n", "", "the alias of the node")
nodeCmd.MarkFlagRequired("node") nodeCmd.MarkFlagRequired("node")
for _, cmd := range s.nodeCommands { for _, cmd := range s.nodeCommands {
@ -54,6 +58,12 @@ func (s *service) Run(ctx context.Context) (err error) {
} }
rootCmd.AddCommand(nodeCmd) rootCmd.AddCommand(nodeCmd)
scriptsCmd := &cobra.Command{Use: "script which can have arbitrary params and can include mutliple clients and nodes"}
for _, cmd := range s.scripts {
scriptsCmd.AddCommand(cmd)
}
rootCmd.AddCommand(scriptsCmd)
return rootCmd.Execute() return rootCmd.Execute()
} }
@ -67,7 +77,7 @@ func (s *service) Init(a *app.App) (err error) {
s.peers = a.MustComponent(peers.CName).(peers.Service) s.peers = a.MustComponent(peers.CName).(peers.Service)
s.registerClientCommands() s.registerClientCommands()
s.registerNodeCommands() s.registerNodeCommands()
//s.registerScripts() s.registerScripts()
return nil return nil
} }
@ -78,7 +88,7 @@ func (s *service) Name() (name string) {
func (s *service) registerClientCommands() { func (s *service) registerClientCommands() {
cmdCreateSpace := &cobra.Command{ cmdCreateSpace := &cobra.Command{
Use: "create-space [params]", Use: "create-space",
Short: "create the space", Short: "create the space",
Args: cobra.RangeArgs(0, 0), Args: cobra.RangeArgs(0, 0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -99,7 +109,7 @@ func (s *service) registerClientCommands() {
s.clientCommands = append(s.clientCommands, cmdCreateSpace) s.clientCommands = append(s.clientCommands, cmdCreateSpace)
cmdLoadSpace := &cobra.Command{ cmdLoadSpace := &cobra.Command{
Use: "load-space [params]", Use: "load-space [space]",
Short: "load the space", Short: "load the space",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -122,7 +132,7 @@ func (s *service) registerClientCommands() {
s.clientCommands = append(s.clientCommands, cmdLoadSpace) s.clientCommands = append(s.clientCommands, cmdLoadSpace)
cmdDeriveSpace := &cobra.Command{ cmdDeriveSpace := &cobra.Command{
Use: "derive-space [params]", Use: "derive-space",
Short: "derive the space from account data", Short: "derive the space from account data",
Args: cobra.RangeArgs(0, 0), Args: cobra.RangeArgs(0, 0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -143,7 +153,7 @@ func (s *service) registerClientCommands() {
s.clientCommands = append(s.clientCommands, cmdDeriveSpace) s.clientCommands = append(s.clientCommands, cmdDeriveSpace)
cmdCreateDocument := &cobra.Command{ cmdCreateDocument := &cobra.Command{
Use: "create-document [params]", Use: "create-document [space]",
Short: "create the document in a particular space", Short: "create the document in a particular space",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -166,7 +176,7 @@ func (s *service) registerClientCommands() {
s.clientCommands = append(s.clientCommands, cmdCreateDocument) s.clientCommands = append(s.clientCommands, cmdCreateDocument)
cmdDeleteDocument := &cobra.Command{ cmdDeleteDocument := &cobra.Command{
Use: "delete-document [params]", Use: "delete-document [document]",
Short: "delete the document in a particular space", Short: "delete the document in a particular space",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -193,13 +203,14 @@ func (s *service) registerClientCommands() {
s.clientCommands = append(s.clientCommands, cmdDeleteDocument) s.clientCommands = append(s.clientCommands, cmdDeleteDocument)
cmdAddText := &cobra.Command{ cmdAddText := &cobra.Command{
Use: "add-text [params]", Use: "add-text [text]",
Short: "add text to the document in the particular space", Short: "add text to the document in the particular space",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
cli, _ := cmd.Flags().GetString("client") cli, _ := cmd.Flags().GetString("client")
space, _ := cmd.Flags().GetString("space") space, _ := cmd.Flags().GetString("space")
document, _ := cmd.Flags().GetString("document") document, _ := cmd.Flags().GetString("document")
snapshot, _ := cmd.Flags().GetBool("snapshot")
server, err := s.peers.Get(cli) server, err := s.peers.Get(cli)
if err != nil { if err != nil {
fmt.Println("no such client") fmt.Println("no such client")
@ -209,6 +220,7 @@ func (s *service) registerClientCommands() {
SpaceId: space, SpaceId: space,
DocumentId: document, DocumentId: document,
Text: args[0], Text: args[0],
IsSnapshot: snapshot,
}) })
if err != nil { if err != nil {
fmt.Println("couldn't add text to the document", err) fmt.Println("couldn't add text to the document", err)
@ -219,12 +231,13 @@ func (s *service) registerClientCommands() {
} }
cmdAddText.Flags().String("space", "", "the space where something is happening :-)") cmdAddText.Flags().String("space", "", "the space where something is happening :-)")
cmdAddText.Flags().String("document", "", "the document where something is happening :-)") cmdAddText.Flags().String("document", "", "the document where something is happening :-)")
cmdAddText.Flags().Bool("snapshot", false, "tells if the snapshot should be created")
cmdAddText.MarkFlagRequired("space") cmdAddText.MarkFlagRequired("space")
cmdAddText.MarkFlagRequired("document") cmdAddText.MarkFlagRequired("document")
s.clientCommands = append(s.clientCommands, cmdAddText) s.clientCommands = append(s.clientCommands, cmdAddText)
cmdAllTrees := &cobra.Command{ cmdAllTrees := &cobra.Command{
Use: "all-trees [params]", Use: "all-trees [space]",
Short: "print all trees in space and their heads", Short: "print all trees in space and their heads",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -262,7 +275,7 @@ func (s *service) registerClientCommands() {
s.clientCommands = append(s.clientCommands, cmdAllTrees) s.clientCommands = append(s.clientCommands, cmdAllTrees)
cmdDumpTree := &cobra.Command{ cmdDumpTree := &cobra.Command{
Use: "dump-tree [params]", Use: "dump-tree [document]",
Short: "get graphviz description of the tree", Short: "get graphviz description of the tree",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -290,7 +303,7 @@ func (s *service) registerClientCommands() {
s.clientCommands = append(s.clientCommands, cmdDumpTree) s.clientCommands = append(s.clientCommands, cmdDumpTree)
cmdTreeParams := &cobra.Command{ cmdTreeParams := &cobra.Command{
Use: "tree-params [params]", Use: "tree-params [document]",
Short: "print heads and root of the tree", Short: "print heads and root of the tree",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -325,7 +338,7 @@ func (s *service) registerClientCommands() {
s.clientCommands = append(s.clientCommands, cmdTreeParams) s.clientCommands = append(s.clientCommands, cmdTreeParams)
cmdAllSpaces := &cobra.Command{ cmdAllSpaces := &cobra.Command{
Use: "all-spaces [params]", Use: "all-spaces",
Short: "print all spaces", Short: "print all spaces",
Args: cobra.RangeArgs(0, 0), Args: cobra.RangeArgs(0, 0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -355,7 +368,7 @@ func (s *service) registerClientCommands() {
func (s *service) registerNodeCommands() { func (s *service) registerNodeCommands() {
cmdAllTrees := &cobra.Command{ cmdAllTrees := &cobra.Command{
Use: "all-trees [params]", Use: "all-trees [space]",
Short: "print all trees in space and their heads", Short: "print all trees in space and their heads",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -393,7 +406,7 @@ func (s *service) registerNodeCommands() {
s.nodeCommands = append(s.nodeCommands, cmdAllTrees) s.nodeCommands = append(s.nodeCommands, cmdAllTrees)
cmdDumpTree := &cobra.Command{ cmdDumpTree := &cobra.Command{
Use: "dump-tree [params]", Use: "dump-tree [space]",
Short: "get graphviz description of the tree", Short: "get graphviz description of the tree",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -421,7 +434,7 @@ func (s *service) registerNodeCommands() {
s.nodeCommands = append(s.nodeCommands, cmdDumpTree) s.nodeCommands = append(s.nodeCommands, cmdDumpTree)
cmdTreeParams := &cobra.Command{ cmdTreeParams := &cobra.Command{
Use: "tree-params [params]", Use: "tree-params [document]",
Short: "print heads and root of the tree", Short: "print heads and root of the tree",
Args: cobra.RangeArgs(1, 1), Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -456,7 +469,7 @@ func (s *service) registerNodeCommands() {
s.nodeCommands = append(s.nodeCommands, cmdTreeParams) s.nodeCommands = append(s.nodeCommands, cmdTreeParams)
cmdAllSpaces := &cobra.Command{ cmdAllSpaces := &cobra.Command{
Use: "all-spaces [params]", Use: "all-spaces",
Short: "print all spaces", Short: "print all spaces",
Args: cobra.RangeArgs(0, 0), Args: cobra.RangeArgs(0, 0),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -484,85 +497,68 @@ func (s *service) registerNodeCommands() {
s.nodeCommands = append(s.nodeCommands, cmdAllSpaces) s.nodeCommands = append(s.nodeCommands, cmdAllSpaces)
} }
// func (s *service) registerScripts() {
//func (s *service) registerScripts() { cmdAddTextMany := &cobra.Command{
// s.scripts["create-many"] = Script{Cmd: func(params []string) (res string, err error) { Use: "add-text-many [text]",
// if len(params) != 5 { Short: "add text to the document in the particular space in many clients at the same time with randomized snapshots",
// err = ErrIncorrectParamsCount Args: cobra.RangeArgs(1, 1),
// return Run: func(cmd *cobra.Command, args []string) {
// } clients, _ := cmd.Flags().GetString("clients")
// peer, err := s.peers.Get(params[0]) space, _ := cmd.Flags().GetString("space")
// if err != nil { document, _ := cmd.Flags().GetString("document")
// return times, _ := cmd.Flags().GetInt("times")
// } if times <= 0 {
// last, err := strconv.Atoi(params[4]) fmt.Println("the times parameter should be more than 0")
// if err != nil { return
// return }
// } var addresses []string
// if last <= 0 { for _, cl := range strings.Split(clients, ",") {
// err = fmt.Errorf("incorrect number of steps") if len(cl) == 0 {
// return continue
// } }
// server, err := s.peers.Get(cl)
// for i := 0; i < last; i++ { if err != nil {
// _, err := s.client.AddText(context.Background(), peer.Address, &clientproto.AddTextRequest{ fmt.Println("no such client")
// SpaceId: params[1], return
// DocumentId: params[2], }
// Text: params[3], addresses = append(addresses, server.Address)
// }) }
// if err != nil {
// return "", err wg := &sync.WaitGroup{}
// } var mError errs.Group
// } createMany := func(address string) {
// return defer wg.Done()
// }} for i := 0; i < times; i++ {
// s.scripts["create-many-two-clients"] = Script{Cmd: func(params []string) (res string, err error) { _, err := s.client.AddText(context.Background(), address, &clientproto.AddTextRequest{
// if len(params) != 6 { SpaceId: space,
// err = ErrIncorrectParamsCount DocumentId: document,
// return Text: args[0],
// } IsSnapshot: rand.Int()%2 == 0,
// peer1, err := s.peers.Get(params[0]) })
// if err != nil { if err != nil {
// return mError.Add(err)
// } return
// peer2, err := s.peers.Get(params[1]) }
// if err != nil { }
// return }
// } for _, p := range addresses {
// last, err := strconv.Atoi(params[5]) wg.Add(1)
// if err != nil { createMany(p)
// return }
// } wg.Wait()
// if last <= 0 { if mError.Err() != nil {
// err = fmt.Errorf("incorrect number of steps") fmt.Println("got errors while executing add many", mError.Err())
// return return
// } }
// wg := &sync.WaitGroup{} return
// var mError errs.Group },
// createMany := func(peer peers.Peer) { }
// defer wg.Done() cmdAddTextMany.Flags().String("space", "", "the space where something is happening :-)")
// for i := 0; i < last; i++ { cmdAddTextMany.Flags().String("document", "", "the document where something is happening :-)")
// _, err := s.client.AddText(context.Background(), peer.Address, &clientproto.AddTextRequest{ cmdAddTextMany.Flags().String("clients", "", "the aliases of clients with value separated by comma")
// SpaceId: params[2], cmdAddTextMany.Flags().Int("times", 1, "how many times we should add the change")
// DocumentId: params[3], cmdAddTextMany.MarkFlagRequired("space")
// Text: params[4], cmdAddTextMany.MarkFlagRequired("document")
// IsSnapshot: rand.Int()%2 == 0, cmdAddTextMany.MarkFlagRequired("clients")
// }) s.scripts = append(s.scripts, cmdAddTextMany)
// if err != nil { }
// mError.Add(err)
// return
// }
// }
// }
// for _, p := range []peers.Peer{peer1, peer2} {
// wg.Add(1)
// createMany(p)
// }
// wg.Wait()
// if mError.Err() != nil {
// err = mError.Err()
// return
// }
// return
// }}
//}