From 4eca88b0bcc43ce3d2f7fb9971a8d06385422325 Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Mon, 5 Dec 2022 12:43:36 +0100 Subject: [PATCH] Use cobra command line --- util/cmd/debug/api/service.go | 880 ++++++++++++++++++-------------- util/cmd/debug/debug.go | 44 +- util/cmd/debug/stdin/service.go | 98 ---- 3 files changed, 509 insertions(+), 513 deletions(-) delete mode 100644 util/cmd/debug/stdin/service.go diff --git a/util/cmd/debug/api/service.go b/util/cmd/debug/api/service.go index 475c17f2..d5a5f61a 100644 --- a/util/cmd/debug/api/service.go +++ b/util/cmd/debug/api/service.go @@ -2,7 +2,6 @@ package api import ( "context" - "errors" "fmt" clientproto "github.com/anytypeio/go-anytype-infrastructure-experiments/client/api/apiproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" @@ -11,51 +10,55 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api/client" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api/node" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers" - "github.com/zeebo/errs" - "math/rand" - "strconv" - "sync" + "github.com/spf13/cobra" + _ "github.com/spf13/cobra" ) const CName = "debug.api" var log = logger.NewNamed(CName) -var ( - ErrNoSuchCommand = errors.New("no such command") - ErrIncorrectPeerType = errors.New("incorrect peer type") - ErrIncorrectParamsCount = errors.New("incorrect params count") -) - -type Command struct { - Cmd func(server peers.Peer, params []string) (string, error) -} - -type Script struct { - Cmd func(params []string) (string, error) -} - type Service interface { - app.Component - Call(server peers.Peer, cmdName string, params []string) (string, error) - Script(scriptName string, params []string) (res string, err error) + app.ComponentRunnable } type service struct { - clientCommands map[string]Command - nodeCommands map[string]Command - scripts map[string]Script client client.Service node node.Service peers peers.Service + clientCommands []*cobra.Command + nodeCommands []*cobra.Command + scripts []*cobra.Command } func New() Service { - return &service{ - clientCommands: map[string]Command{}, - nodeCommands: map[string]Command{}, - scripts: map[string]Script{}, + return &service{} +} + +func (s *service) Run(ctx context.Context) (err error) { + rootCmd := &cobra.Command{Use: "debug"} + + clientCmd := &cobra.Command{Use: "client commands"} + clientCmd.PersistentFlags().StringP("client", "c", "", "the alias of the client") + clientCmd.MarkFlagRequired("client") + for _, cmd := range s.clientCommands { + clientCmd.AddCommand(cmd) } + rootCmd.AddCommand(clientCmd) + + nodeCmd := &cobra.Command{Use: "node commands"} + nodeCmd.PersistentFlags().StringP("node", "n", "", "the alias of the node") + nodeCmd.MarkFlagRequired("node") + for _, cmd := range s.nodeCommands { + nodeCmd.AddCommand(cmd) + } + rootCmd.AddCommand(nodeCmd) + + return rootCmd.Execute() +} + +func (s *service) Close(ctx context.Context) (err error) { + return nil } func (s *service) Init(a *app.App) (err error) { @@ -64,7 +67,7 @@ func (s *service) Init(a *app.App) (err error) { s.peers = a.MustComponent(peers.CName).(peers.Service) s.registerClientCommands() s.registerNodeCommands() - s.registerScripts() + //s.registerScripts() return nil } @@ -73,362 +76,493 @@ func (s *service) Name() (name string) { return CName } -func (s *service) Script(scriptName string, params []string) (res string, err error) { - script, ok := s.scripts[scriptName] - if !ok { - err = ErrNoSuchCommand - return - } - return script.Cmd(params) -} - -func (s *service) Call(server peers.Peer, cmdName string, params []string) (res string, err error) { - var ( - cmd Command - commands map[string]Command - ) - switch server.PeerType { - case peers.PeerTypeClient: - commands = s.clientCommands - case peers.PeerTypeNode: - commands = s.nodeCommands - } - cmd, ok := commands[cmdName] - if !ok { - err = ErrNoSuchCommand - return - } - return cmd.Cmd(server, params) -} - func (s *service) registerClientCommands() { - s.clientCommands["create-space"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 0 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.client.CreateSpace(context.Background(), server.Address, &clientproto.CreateSpaceRequest{}) - if err != nil { - return - } - res = resp.Id - return - }} - s.clientCommands["derive-space"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 0 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.client.DeriveSpace(context.Background(), server.Address, &clientproto.DeriveSpaceRequest{}) - if err != nil { - return - } - res = resp.Id - return - }} - s.clientCommands["create-document"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 1 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.client.CreateDocument(context.Background(), server.Address, &clientproto.CreateDocumentRequest{ - SpaceId: params[0], - }) - if err != nil { - return - } - res = resp.Id - return - }} - s.clientCommands["delete-document"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 2 { - err = ErrIncorrectParamsCount - return - } - _, err = s.client.DeleteDocument(context.Background(), server.Address, &clientproto.DeleteDocumentRequest{ - SpaceId: params[0], - DocumentId: params[1], - }) - if err != nil { - return - } - res = "deleted" - return - }} - s.clientCommands["add-text"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 3 && len(params) != 4 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.client.AddText(context.Background(), server.Address, &clientproto.AddTextRequest{ - SpaceId: params[0], - DocumentId: params[1], - Text: params[2], - IsSnapshot: len(params) == 4, - }) - if err != nil { - return - } - res = resp.DocumentId + "->" + resp.RootId + "->" + resp.HeadId - return - }} - s.clientCommands["load-space"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 1 { - err = ErrIncorrectParamsCount - return - } - _, err = s.client.LoadSpace(context.Background(), server.Address, &clientproto.LoadSpaceRequest{ - SpaceId: params[0], - }) - if err != nil { - return - } - res = params[0] - return - }} - s.clientCommands["all-trees"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 1 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.client.AllTrees(context.Background(), server.Address, &clientproto.AllTreesRequest{ - SpaceId: params[0], - }) - if err != nil { - return - } - for treeIdx, tree := range resp.Trees { - treeStr := tree.Id + ":[" - for headIdx, head := range tree.Heads { - treeStr += head - if headIdx != len(tree.Heads)-1 { - treeStr += "," + cmdCreateSpace := &cobra.Command{ + Use: "create-space [params]", + Short: "create the space", + Args: cobra.RangeArgs(0, 0), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return + } + resp, err := s.client.CreateSpace(context.Background(), server.Address, &clientproto.CreateSpaceRequest{}) + if err != nil { + fmt.Println("couldn't create a space", err) + return + } + fmt.Println(resp.Id) + }, + } + s.clientCommands = append(s.clientCommands, cmdCreateSpace) + + cmdLoadSpace := &cobra.Command{ + Use: "load-space [params]", + Short: "load the space", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return + } + _, err = s.client.LoadSpace(context.Background(), server.Address, &clientproto.LoadSpaceRequest{ + SpaceId: args[0], + }) + if err != nil { + fmt.Println("couldn't load the space", err) + return + } + fmt.Println("space loaded", args[0]) + }, + } + s.clientCommands = append(s.clientCommands, cmdLoadSpace) + + cmdDeriveSpace := &cobra.Command{ + Use: "derive-space [params]", + Short: "derive the space from account data", + Args: cobra.RangeArgs(0, 0), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return + } + resp, err := s.client.DeriveSpace(context.Background(), server.Address, &clientproto.DeriveSpaceRequest{}) + if err != nil { + fmt.Println("couldn't derive a space", err) + return + } + fmt.Println(resp.Id) + }, + } + s.clientCommands = append(s.clientCommands, cmdDeriveSpace) + + cmdCreateDocument := &cobra.Command{ + Use: "create-document [params]", + Short: "create the document in a particular space", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return + } + resp, err := s.client.CreateDocument(context.Background(), server.Address, &clientproto.CreateDocumentRequest{ + SpaceId: args[0], + }) + if err != nil { + fmt.Println("couldn't create a document", err) + return + } + fmt.Println(resp.Id) + }, + } + s.clientCommands = append(s.clientCommands, cmdCreateDocument) + + cmdDeleteDocument := &cobra.Command{ + Use: "delete-document [params]", + Short: "delete the document in a particular space", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + space, _ := cmd.Flags().GetString("space") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return + } + _, err = s.client.DeleteDocument(context.Background(), server.Address, &clientproto.DeleteDocumentRequest{ + SpaceId: space, + DocumentId: args[0], + }) + if err != nil { + fmt.Println("couldn't delete the document", err) + return + } + fmt.Println("deleted", args[1]) + }, + } + cmdDeleteDocument.Flags().String("space", "", "the space where something is happening :-)") + cmdDeleteDocument.MarkFlagRequired("space") + s.clientCommands = append(s.clientCommands, cmdDeleteDocument) + + cmdAddText := &cobra.Command{ + Use: "add-text [params]", + Short: "add text to the document in the particular space", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + space, _ := cmd.Flags().GetString("space") + document, _ := cmd.Flags().GetString("document") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return + } + resp, err := s.client.AddText(context.Background(), server.Address, &clientproto.AddTextRequest{ + SpaceId: space, + DocumentId: document, + Text: args[0], + }) + if err != nil { + fmt.Println("couldn't add text to the document", err) + return + } + fmt.Println("added text", resp.DocumentId, "root:", resp.RootId, "head:", resp.HeadId) + }, + } + cmdAddText.Flags().String("space", "", "the space where something is happening :-)") + cmdAddText.Flags().String("document", "", "the document where something is happening :-)") + cmdAddText.MarkFlagRequired("space") + cmdAddText.MarkFlagRequired("document") + s.clientCommands = append(s.clientCommands, cmdAddText) + + cmdAllTrees := &cobra.Command{ + Use: "all-trees [params]", + Short: "print all trees in space and their heads", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return + } + resp, err := s.client.AllTrees(context.Background(), server.Address, &clientproto.AllTreesRequest{ + SpaceId: args[0], + }) + if err != nil { + fmt.Println("couldn't print all the trees", err) + return + } + var res string + for treeIdx, tree := range resp.Trees { + treeStr := tree.Id + ":[" + for headIdx, head := range tree.Heads { + treeStr += head + if headIdx != len(tree.Heads)-1 { + treeStr += "," + } + } + treeStr += "]" + res += treeStr + if treeIdx != len(resp.Trees)-1 { + res += "\n" } } - treeStr += "]" - res += treeStr - if treeIdx != len(resp.Trees)-1 { - res += "\n" + fmt.Println(res) + }, + } + s.clientCommands = append(s.clientCommands, cmdAllTrees) + + cmdDumpTree := &cobra.Command{ + Use: "dump-tree [params]", + Short: "get graphviz description of the tree", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + space, _ := cmd.Flags().GetString("space") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return } - } - return - }} - s.clientCommands["dump-tree"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 2 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.client.DumpTree(context.Background(), server.Address, &clientproto.DumpTreeRequest{ - SpaceId: params[0], - DocumentId: params[1], - }) - if err != nil { - return - } - res = resp.Dump - return - }} - s.clientCommands["tree-params"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 2 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.client.TreeParams(context.Background(), server.Address, &clientproto.TreeParamsRequest{ - SpaceId: params[0], - DocumentId: params[1], - }) - if err != nil { - return - } - res = resp.RootId + "->" - for headIdx, head := range resp.HeadIds { - res += head - if headIdx != len(resp.HeadIds)-1 { - res += "," + + resp, err := s.client.DumpTree(context.Background(), server.Address, &clientproto.DumpTreeRequest{ + SpaceId: space, + DocumentId: args[0], + }) + if err != nil { + fmt.Println("couldn't dump the tree", err) + return } - } - return - }} - s.clientCommands["all-spaces"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 0 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.client.AllSpaces(context.Background(), server.Address, &clientproto.AllSpacesRequest{}) - if err != nil { - return - } - for treeIdx, spaceId := range resp.SpaceIds { - res += spaceId - if treeIdx != len(resp.SpaceIds)-1 { - res += "\n" + fmt.Println(resp.Dump) + }, + } + cmdDumpTree.Flags().String("space", "", "the space where something is happening :-)") + cmdDumpTree.MarkFlagRequired("space") + s.clientCommands = append(s.clientCommands, cmdDumpTree) + + cmdTreeParams := &cobra.Command{ + Use: "tree-params [params]", + Short: "print heads and root of the tree", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + space, _ := cmd.Flags().GetString("space") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return } - } - return - }} + + resp, err := s.client.TreeParams(context.Background(), server.Address, &clientproto.TreeParamsRequest{ + SpaceId: space, + DocumentId: args[0], + }) + if err != nil { + fmt.Println("couldn't print params of the tree", err) + return + } + res := resp.RootId + "->" + for headIdx, head := range resp.HeadIds { + res += head + if headIdx != len(resp.HeadIds)-1 { + res += "," + } + } + fmt.Println(res) + }, + } + cmdTreeParams.Flags().String("space", "", "the space where something is happening :-)") + cmdTreeParams.MarkFlagRequired("space") + s.clientCommands = append(s.clientCommands, cmdTreeParams) + + cmdAllSpaces := &cobra.Command{ + Use: "all-spaces [params]", + Short: "print all spaces", + Args: cobra.RangeArgs(0, 0), + Run: func(cmd *cobra.Command, args []string) { + cli, _ := cmd.Flags().GetString("client") + server, err := s.peers.Get(cli) + if err != nil { + fmt.Println("no such client") + return + } + resp, err := s.client.AllSpaces(context.Background(), server.Address, &clientproto.AllSpacesRequest{}) + if err != nil { + fmt.Println("couldn't print all the spaces", err) + return + } + var res string + for treeIdx, spaceId := range resp.SpaceIds { + res += spaceId + if treeIdx != len(resp.SpaceIds)-1 { + res += "\n" + } + } + fmt.Println(res) + }, + } + s.clientCommands = append(s.clientCommands, cmdAllSpaces) } func (s *service) registerNodeCommands() { - s.nodeCommands["all-trees"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 1 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.node.AllTrees(context.Background(), server.Address, &nodeproto.AllTreesRequest{ - SpaceId: params[0], - }) - if err != nil { - return - } - for treeIdx, tree := range resp.Trees { - treeStr := tree.Id + ":[" - for headIdx, head := range tree.Heads { - treeStr += head - if headIdx != len(tree.Heads)-1 { - treeStr += "," - } + cmdAllTrees := &cobra.Command{ + Use: "all-trees [params]", + Short: "print all trees in space and their heads", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + nd, _ := cmd.Flags().GetString("node") + server, err := s.peers.Get(nd) + if err != nil { + fmt.Println("no such node") + return } - treeStr += "]" - res += treeStr - if treeIdx != len(resp.Trees)-1 { - res += "\n" - } - } - return - }} - s.nodeCommands["dump-tree"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 2 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.node.DumpTree(context.Background(), server.Address, &nodeproto.DumpTreeRequest{ - SpaceId: params[0], - DocumentId: params[1], - }) - if err != nil { - return - } - res = resp.Dump - return - }} - s.nodeCommands["tree-params"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 2 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.node.TreeParams(context.Background(), server.Address, &nodeproto.TreeParamsRequest{ - SpaceId: params[0], - DocumentId: params[1], - }) - if err != nil { - return - } - res = resp.RootId + "->" - for headIdx, head := range resp.HeadIds { - res += head - if headIdx != len(resp.HeadIds)-1 { - res += "," - } - } - return - }} - s.nodeCommands["all-spaces"] = Command{Cmd: func(server peers.Peer, params []string) (res string, err error) { - if len(params) != 0 { - err = ErrIncorrectParamsCount - return - } - resp, err := s.node.AllSpaces(context.Background(), server.Address, &nodeproto.AllSpacesRequest{}) - if err != nil { - return - } - for treeIdx, spaceId := range resp.SpaceIds { - res += spaceId - if treeIdx != len(resp.SpaceIds)-1 { - res += "\n" - } - } - return - }} -} - -func (s *service) registerScripts() { - s.scripts["create-many"] = Script{Cmd: func(params []string) (res string, err error) { - if len(params) != 5 { - err = ErrIncorrectParamsCount - return - } - peer, err := s.peers.Get(params[0]) - if err != nil { - return - } - last, err := strconv.Atoi(params[4]) - if err != nil { - return - } - if last <= 0 { - err = fmt.Errorf("incorrect number of steps") - return - } - - for i := 0; i < last; i++ { - _, err := s.client.AddText(context.Background(), peer.Address, &clientproto.AddTextRequest{ - SpaceId: params[1], - DocumentId: params[2], - Text: params[3], + resp, err := s.node.AllTrees(context.Background(), server.Address, &nodeproto.AllTreesRequest{ + SpaceId: args[0], }) if err != nil { - return "", err + fmt.Println("couldn't print all the trees", err) + return } - } - return - }} - s.scripts["create-many-two-clients"] = Script{Cmd: func(params []string) (res string, err error) { - if len(params) != 6 { - err = ErrIncorrectParamsCount - return - } - peer1, err := s.peers.Get(params[0]) - if err != nil { - return - } - peer2, err := s.peers.Get(params[1]) - if err != nil { - return - } - last, err := strconv.Atoi(params[5]) - if err != nil { - return - } - if last <= 0 { - err = fmt.Errorf("incorrect number of steps") - return - } - wg := &sync.WaitGroup{} - var mError errs.Group - createMany := func(peer peers.Peer) { - defer wg.Done() - for i := 0; i < last; i++ { - _, err := s.client.AddText(context.Background(), peer.Address, &clientproto.AddTextRequest{ - SpaceId: params[2], - DocumentId: params[3], - Text: params[4], - IsSnapshot: rand.Int()%2 == 0, - }) - if err != nil { - mError.Add(err) - return + var res string + for treeIdx, tree := range resp.Trees { + treeStr := tree.Id + ":[" + for headIdx, head := range tree.Heads { + treeStr += head + if headIdx != len(tree.Heads)-1 { + treeStr += "," + } + } + treeStr += "]" + res += treeStr + if treeIdx != len(resp.Trees)-1 { + res += "\n" } } - } - for _, p := range []peers.Peer{peer1, peer2} { - wg.Add(1) - createMany(p) - } - wg.Wait() - if mError.Err() != nil { - err = mError.Err() - return - } - return - }} + fmt.Println(res) + }, + } + s.nodeCommands = append(s.nodeCommands, cmdAllTrees) + + cmdDumpTree := &cobra.Command{ + Use: "dump-tree [params]", + Short: "get graphviz description of the tree", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + nd, _ := cmd.Flags().GetString("node") + space, _ := cmd.Flags().GetString("space") + server, err := s.peers.Get(nd) + if err != nil { + fmt.Println("no such node") + return + } + + resp, err := s.node.DumpTree(context.Background(), server.Address, &nodeproto.DumpTreeRequest{ + SpaceId: space, + DocumentId: args[0], + }) + if err != nil { + fmt.Println("couldn't dump the tree", err) + return + } + fmt.Println(resp.Dump) + }, + } + cmdDumpTree.Flags().String("space", "", "the space where something is happening :-)") + cmdDumpTree.MarkFlagRequired("space") + s.nodeCommands = append(s.nodeCommands, cmdDumpTree) + + cmdTreeParams := &cobra.Command{ + Use: "tree-params [params]", + Short: "print heads and root of the tree", + Args: cobra.RangeArgs(1, 1), + Run: func(cmd *cobra.Command, args []string) { + nd, _ := cmd.Flags().GetString("node") + space, _ := cmd.Flags().GetString("space") + server, err := s.peers.Get(nd) + if err != nil { + fmt.Println("no such node") + return + } + + resp, err := s.node.TreeParams(context.Background(), server.Address, &nodeproto.TreeParamsRequest{ + SpaceId: space, + DocumentId: args[0], + }) + if err != nil { + fmt.Println("couldn't print params of the tree", err) + return + } + res := resp.RootId + "->" + for headIdx, head := range resp.HeadIds { + res += head + if headIdx != len(resp.HeadIds)-1 { + res += "," + } + } + fmt.Println(res) + }, + } + cmdTreeParams.Flags().String("space", "", "the space where something is happening :-)") + cmdTreeParams.MarkFlagRequired("space") + s.nodeCommands = append(s.nodeCommands, cmdTreeParams) + + cmdAllSpaces := &cobra.Command{ + Use: "all-spaces [params]", + Short: "print all spaces", + Args: cobra.RangeArgs(0, 0), + Run: func(cmd *cobra.Command, args []string) { + nd, _ := cmd.Flags().GetString("node") + server, err := s.peers.Get(nd) + if err != nil { + fmt.Println("no such node") + return + } + resp, err := s.node.AllSpaces(context.Background(), server.Address, &nodeproto.AllSpacesRequest{}) + if err != nil { + fmt.Println("couldn't print all the spaces", err) + return + } + var res string + for treeIdx, spaceId := range resp.SpaceIds { + res += spaceId + if treeIdx != len(resp.SpaceIds)-1 { + res += "\n" + } + } + fmt.Println(res) + }, + } + s.nodeCommands = append(s.nodeCommands, cmdAllSpaces) } + +// +//func (s *service) registerScripts() { +// s.scripts["create-many"] = Script{Cmd: func(params []string) (res string, err error) { +// if len(params) != 5 { +// err = ErrIncorrectParamsCount +// return +// } +// peer, err := s.peers.Get(params[0]) +// if err != nil { +// return +// } +// last, err := strconv.Atoi(params[4]) +// if err != nil { +// return +// } +// if last <= 0 { +// err = fmt.Errorf("incorrect number of steps") +// return +// } +// +// for i := 0; i < last; i++ { +// _, err := s.client.AddText(context.Background(), peer.Address, &clientproto.AddTextRequest{ +// SpaceId: params[1], +// DocumentId: params[2], +// Text: params[3], +// }) +// if err != nil { +// return "", err +// } +// } +// return +// }} +// s.scripts["create-many-two-clients"] = Script{Cmd: func(params []string) (res string, err error) { +// if len(params) != 6 { +// err = ErrIncorrectParamsCount +// return +// } +// peer1, err := s.peers.Get(params[0]) +// if err != nil { +// return +// } +// peer2, err := s.peers.Get(params[1]) +// if err != nil { +// return +// } +// last, err := strconv.Atoi(params[5]) +// if err != nil { +// return +// } +// if last <= 0 { +// err = fmt.Errorf("incorrect number of steps") +// return +// } +// wg := &sync.WaitGroup{} +// var mError errs.Group +// createMany := func(peer peers.Peer) { +// defer wg.Done() +// for i := 0; i < last; i++ { +// _, err := s.client.AddText(context.Background(), peer.Address, &clientproto.AddTextRequest{ +// SpaceId: params[2], +// DocumentId: params[3], +// Text: params[4], +// IsSnapshot: rand.Int()%2 == 0, +// }) +// 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 +// }} +//} diff --git a/util/cmd/debug/debug.go b/util/cmd/debug/debug.go index 3a29c7bf..a17cb5c4 100644 --- a/util/cmd/debug/debug.go +++ b/util/cmd/debug/debug.go @@ -2,8 +2,6 @@ package main import ( "context" - "flag" - "fmt" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api" @@ -11,24 +9,14 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api/node" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/drpcclient" "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/stdin" "go.uber.org/zap" "go.uber.org/zap/zapcore" "net/http" "os" - "os/signal" - "syscall" - "time" ) var log = logger.NewNamed("main") -var ( - // we can't use "v" here because of glog init (through badger) setting flag.Bool with "v" - flagVersion = flag.Bool("ver", false, "show version and exit") - flagHelp = flag.Bool("h", false, "show help and exit") -) - func init() { config := zap.NewProductionEncoderConfig() config.EncodeTime = zapcore.ISO8601TimeEncoder @@ -43,17 +31,6 @@ func init() { } func main() { - flag.Parse() - - if *flagVersion { - fmt.Println(app.VersionDescription()) - return - } - if *flagHelp { - flag.PrintDefaults() - return - } - if debug, ok := os.LookupEnv("ANYPROF"); ok && debug != "" { go func() { http.ListenAndServe(debug, nil) @@ -64,27 +41,11 @@ func main() { ctx := context.Background() a := new(app.App) Bootstrap(a) + // start app if err := a.Start(ctx); err != nil { log.Fatal("can't start app", zap.Error(err)) } - log.Info("app started", zap.String("version", a.Version())) - - // wait exit signal - exit := make(chan os.Signal, 1) - signal.Notify(exit, os.Interrupt, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGQUIT) - sig := <-exit - log.Info("received exit signal, stop app...", zap.String("signal", fmt.Sprint(sig))) - - // close app - ctx, cancel := context.WithTimeout(ctx, time.Minute) - defer cancel() - if err := a.Close(ctx); err != nil { - log.Fatal("close error", zap.Error(err)) - } else { - log.Info("goodbye!") - } - time.Sleep(time.Second / 3) } func Bootstrap(a *app.App) { @@ -92,6 +53,5 @@ func Bootstrap(a *app.App) { Register(peers.New()). Register(client.New()). Register(node.New()). - Register(api.New()). - Register(stdin.New()) + Register(api.New()) } diff --git a/util/cmd/debug/stdin/service.go b/util/cmd/debug/stdin/service.go deleted file mode 100644 index 982a4b93..00000000 --- a/util/cmd/debug/stdin/service.go +++ /dev/null @@ -1,98 +0,0 @@ -package stdin - -import ( - "bufio" - "context" - "fmt" - "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" - "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api" - "github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers" - "os" - "strings" -) - -const CName = "debug.stdin" - -var log = logger.NewNamed(CName) - -type Service interface { - app.ComponentRunnable -} - -type service struct { - api api.Service - peers peers.Service -} - -func New() Service { - return &service{} -} - -func (s *service) Init(a *app.App) (err error) { - s.api = a.MustComponent(api.CName).(api.Service) - s.peers = a.MustComponent(peers.CName).(peers.Service) - return -} - -func (s *service) Name() (name string) { - return CName -} - -func (s *service) Run(ctx context.Context) (err error) { - go s.readStdin() - return nil -} - -func (s *service) Close(ctx context.Context) (err error) { - return nil -} - -func (s *service) readStdin() { - reader := bufio.NewReader(os.Stdin) -Loop: - for { - fmt.Print("> ") - str, err := reader.ReadString('\n') - if err != nil { - fmt.Println("error in read string:", err) - return - } - // trimming newline - str = str[:len(str)-1] - - split := strings.Split(str, " ") - if len(split) < 2 { - fmt.Println("incorrect number of arguments") - continue - } - switch split[0] { - case "script": - res, err := s.api.Script(split[1], split[2:]) - if err != nil { - fmt.Println("error in performing script:", err) - continue Loop - } - fmt.Println(res) - continue Loop - case "cmd": - break - default: - fmt.Println("incorrect input") - continue Loop - } - - peer, err := s.peers.Get(split[1]) - if err != nil { - fmt.Println("no such peer", err) - continue - } - - res, err := s.api.Call(peer, split[2], split[3:]) - if err != nil { - fmt.Println("error in performing command:", err) - continue - } - fmt.Println(res) - } -}