Add debug client to script and change commands
This commit is contained in:
parent
761772f09a
commit
2ffd735e25
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,6 +10,7 @@
|
||||
|
||||
# playground/tmp folder for testing different clients
|
||||
playground/tmp
|
||||
playground/debug.json
|
||||
|
||||
# .paw folder for macos paw client
|
||||
.paw
|
||||
|
||||
28
etc/acl.yml
28
etc/acl.yml
@ -1,28 +0,0 @@
|
||||
records:
|
||||
- identity: A
|
||||
aclChanges:
|
||||
- userAdd:
|
||||
identity: A
|
||||
permission: admin
|
||||
encryptionKey: key.Enc.A
|
||||
encryptedReadKeys: [key.Read.1]
|
||||
- userAdd:
|
||||
identity: B
|
||||
permission: admin
|
||||
encryptionKey: key.Enc.B
|
||||
encryptedReadKeys: [key.Read.1]
|
||||
readKey: key.Read.1
|
||||
keys:
|
||||
Enc:
|
||||
- name: A
|
||||
value: JgG4CcCbae1qEpe7mKpBzsHjZhXUmDSNVNX2B1gxFZsJyMX4V6kBQUott9zRWyeXaW1ZmpzuxDXnwSQpAnNurhXyGa9iQaAPqzY9A9VWBPD33Yy1eW7TRuVemzToh8jJQKQKnZNbF8ucTWV9qahusKzyvN8uyhrqoW2tAPfA9S3E3ognCuqbLSW6yjE2rBKayvyS1BVwzjSd6FZK4DDyjfU3pbEVjut3wytGEAn9af6sNMmyCnf2MX5vLovWs9rU8av61wD4z7HTsXyGFx4K75N4Go249Hpe9SKAT6HxhRc3yvj63krPLiQV5yMuH2UeMUXBDekUQyNmBEdn9wrur7mLqB67Bc6tcc2PP8XApBCdWJHvHjN4FktSpaG5vbCqoZbLD1oCbk36q2x9s6XM8pydVqD1J9P3nTbfgMb5pJCTFjNtgKeuKv6wjfJeA9jF1VhcJQisfsahgv9MvZ9M8FJpZTq1zKUhYDCRnZxUkraoMS5yNNVdDzaUckKEDthqik7BMWCWT79vq7uVgMwEvGwGi76gtoMg1159bbPMLZ4bdPVfhH2S9QjPrzQfwZSrzB2YeVPjWpaXDeLDity5H8n1NK2oniAQR6gE71n81neSptsuhV6o6QpQ89AU8y57XmEsou4VEryn8vUxBHhULLxrLNUouxyWamCeFiDjk5cSN6koQsf9BYKSNTPFTrwjTKForDokMhcPdMtFktKwjv7u9UEGcY4MKvNzZZkc77gHiP8bqVtdNNoLpTFUC5SZ9i7bKdHvK12HpSy7yzzPeMXJ9UwhLxkok1g81ngTbN1yxRhvYXyHZFtguCR9kvGojDjka91MTBtk551qDw9eCn2xZT9U8jqzBCjdpvSg3mRWKMPnYAGB7m7u1ye165wyGFvzcHAx3vtXjxAqLUeKYZCjv2m6V9D2Y4qH1TQNddWqH14T1JVMis971UCH9Ddpj6a3387oUnufD1P6HZN2ieJCvptrmbGVvxJYYSvmVf1dkwbtqurDRNWD7TJ7gf6iqSP549C9bxP4GpLt3ygjHmMtcuUzstBuztvunJUnQhfnJxqU6LjRdsFzm53wGWgXNxab7ZvQcPyLwsevn1b98FGPnVpS5iY4LjmqW4ugrC6HgrbsjrXiKzR1yZKhLQkCbLzPoaHb8iB5iBnCr7d4yf5CtfpFRqgoqMFdK5LNZYmDX4HzUKN6A7wC3gGiSRFTLcgGZeSMkB5Pa61CZBU7WCQgFxykycE9HRA7PiQa496GWDCV15teToCpFRsAa6jDmR1MGXPeLRqQgve49VXnQN5FL7c1VuEv5SWjeTuCnMB47DJKBaP7eKJNKgLwETALzSCMF3nRiRgeb15kfoS4BbrJ5yupjrvwmbmvNg1AYFFS5sYNWft7K8v87wQvBakRtGP71Kp8NX77XFtu6xdB7sR6jpfC6qJPyB9akWNXgCrWy9kE4ih42gwAZdUugNZ9YtEsgRM3pwb6qJhkAPyEJtrxrja859PCAgqPSQiPQN33PaMkgQ6HJknu8CrjKRiXAycZ16KLUkHV64TNhEjPTcX1a7rqpD131AYMWX8d7CCdc9Ys7RUb6BwguuNSh8rJK3x4AkMDSUsaE8ynKvpC7RXZpJ9Nxfhd
|
||||
- name: B
|
||||
value: JgG4CcCbae1qEpe7mKXzp7m5hNc56SSyZd9DwUaEStKJrq7RToAC2Vgd3i6hKRwa58zCWeN6Wjc3o6qrdKPEPRvcyEPysamajVo5mdQiUgWAmr97pGEsyjuRjQoC2GY2LvLiEQxEgwFgJxKGMHMiaWMtDfxCDUaDEm4bu5RdMhqRZekAWho6c3WoEeruSr14iX1TrocFNfBkBY7CjEw8kcywXCTNgtvhb2Qiwgj5AxEF4wyw4bzaNA9ctXb1hoHPFVMu6C51pkFY7jUD9zwyH3ukgnAewkGAcPNbKmaTAtMosKRVaAN97mAwXh2VRt1hWmRvVk7r76EjnVKhD4vbsKZc56RVcHTVWRVdhU7FGyPsiE5rSQAz1JQGYzxnZpX7EG77CyrmUGyfueVfRHhwY2oq8A4uQCRaQxSaJHYLowjXSxh8DQ2V6MTqyzti32C27utBYdHzLVCJSGkmdzGwrFcHqsq7nLDxmvJVErPvyReixEe8kFmqopJ3e6LLm8WdYw9K6JYBjXnEfwPzm7Von9sf3dcaGDUHYfttMyeke7fAXJkvPRje69hYVyzdQGAauuojzGkkvQWCSMK1KCMNMznRaPDCNvofrQhYrub24WhmwpKhorufdfW8Cb4T6reBDCtaWVsbuinjtL6F6Sui5aYHJFLJ6e4pPewr1P4EuZYRbMBZwN5KvDLhTGLBuBnaTqUUdF6bj2U22NoRYMogiHiftqKqiexKNDXX1Zg9RQEvxgjuVo6SBW42mVEA8agrLhruRqCmiduJxVrfqLNGeYXHXrcmMEgW7uosJbPXvTcfRvdFWS1ov7oSALvj6vhDQ28Yi9D2ETNdNsfVWAFQuwvPpW7CHQGXTitprVbqH8JYxNZuGygcLmr5efbB22Vzu4ntd1HoraQpG12qeDEUA7tXYUpoYyuSdWwKPjSAMtaQcCSfVrhKQHQuKJargrVrez8vjWuwLfvSucV7ZHe7gjqvYgULdE1ubRCRSd7DuLjEN2Vd6obzV2c3MRet7ZSf4Sp88WM5AuTyW7BjArBc4S3gUQ8rYaiZ8Tu7NCxkEzbFwWRaemZkwfvcsX3XxqjyF37tFSGkEqE5kuBvpZW72675LkDffj7kH1zA8yE6dVujJjWsNYVFJWndUtz5Vy2KCdZAbBgq19q4AtsxWPodU2N3yZXzFAFAzTrxS6V4P7Scpdau1avgRvHLcBQPunA37xaYMy8YMifJwtmRY25mnAQwZAk3eANk7tXwZd58SDnciLNvARJvwKzTQBXcshkwyy52SX8XmXDJsPnRLaHmiYBJ63Yzr5XpZuuAtxb9qrWG2NHCNxfomHokWacV1hjZPPd6ZxT1FuRozB6Qt2NLcyqY7bnTcQJb1jPUaTAGXXCR8WVmmmYo2fDQe8CdBmgyPvbzNTEJUyScBz4RdycB5PZap4SurJCWtHbuMyQbQUB6jJgURDstfXS5Akfe4oruNq9rnYcNtnsDJPtrhXHBqzDizmf1BDxR5FB2RCxzCgeAfg8WQ1Ug9PVAGTzob6ZqCrGXzWXEUniZnf1vjr7QhGKBYXEX9SWDoSMUpP4FreVDTnx15ijRZTV3p8xG5fE9e36TnugRVvTyq7XzmyPBjW2r66f1bior
|
||||
Sign:
|
||||
- name: A
|
||||
value: 3id6ddLcoNoe9rDgGM88ET8T6TnvHm5GFqFdN6kBzn7Q8d6VUGgjeT59CNWFiaofdeRnHBvX2A5ZacMXvfwaYEFuCbug
|
||||
- name: B
|
||||
value: 3iiLPj6wMUQpPwTBNZcUgkbXub1jumg4AEV9LfMyFHZVc84GLyAjVbVvH6EAGhcNrxRxL82aW4BimhDZCpLsRCqx5vwj
|
||||
Read:
|
||||
- name: 1
|
||||
value: bamccoi5jdypwnjkiuuogkawvhkbowha4qg756uhnbkecr5vt3h4q
|
||||
@ -1,4 +1,4 @@
|
||||
.PHONY: client1 client2 node nodes-start nodes-stop clean-db log-node-1 log-node-2 log-node3 log-client-1 log-client-2
|
||||
.PHONY: nodes-start nodes-stop clients-start clients-stop clean-all log-node-1 log-node-2 log-node3 log-client-1 log-client-2
|
||||
export GOPRIVATE=github.com/anytypeio
|
||||
# TODO: make different folders in etc for different programs
|
||||
|
||||
@ -8,7 +8,7 @@ nodes-start:
|
||||
nodes-stop:
|
||||
./init.sh nodes_stop
|
||||
|
||||
clean-db:
|
||||
clean-all:
|
||||
rm -rf tmp
|
||||
|
||||
clients-start:
|
||||
@ -30,4 +30,7 @@ log-client-1:
|
||||
./init.sh client_log 1
|
||||
|
||||
log-client-2:
|
||||
./init.sh client_log 2
|
||||
./init.sh client_log 2
|
||||
|
||||
debug:
|
||||
@(echo "To run debug api please run './init.sh debug [params]' with respective args to debug client")
|
||||
|
||||
@ -5,10 +5,12 @@ export GOPRIVATE=github.com/anytypeio
|
||||
|
||||
NODE_GO="../../../node/cmd/node.go"
|
||||
CLIENT_GO="../../../client/cmd/client.go"
|
||||
DEBUG_GO="../util/cmd/debug/debug.go"
|
||||
NODEMAP_YML="../util/cmd/nodesgen/nodemap.yml"
|
||||
CONFIGS_DIR="../../../etc/configs"
|
||||
|
||||
do_usage() {
|
||||
echo "usage: $0 {nodes_start|nodes_stop|clients_start|clients_stop}"
|
||||
echo "usage: $0 {nodes_start|nodes_stop|clients_start|clients_stop|debug}"
|
||||
echo "usage: $0 node_log <N>"
|
||||
echo "usage: $0 client_log <N>"
|
||||
exit 1
|
||||
@ -34,12 +36,12 @@ do_nodes_stop() {
|
||||
|
||||
do_node_log() {
|
||||
local NODE_NUMBER=$1
|
||||
tail -f tmp/log/node$NODE_NUMBER.log
|
||||
tail -f -n 500 tmp/log/node$NODE_NUMBER.log
|
||||
}
|
||||
|
||||
do_client_log() {
|
||||
local CLIENT_NUMBER=$1
|
||||
tail -f tmp/log/client$CLIENT_NUMBER.log
|
||||
tail -f -n 500 tmp/log/client$CLIENT_NUMBER.log
|
||||
}
|
||||
|
||||
do_clients_start() {
|
||||
@ -60,9 +62,15 @@ do_clients_stop() {
|
||||
done
|
||||
}
|
||||
|
||||
do_debug() {
|
||||
go run $DEBUG_GO --config $NODEMAP_YML $*
|
||||
}
|
||||
|
||||
case $1 in
|
||||
nodes_start | nodes_stop | clients_start | clients_stop)
|
||||
do_$1
|
||||
nodes_start | nodes_stop | clients_start | clients_stop | debug)
|
||||
first_arg=$1
|
||||
shift
|
||||
do_$first_arg $*
|
||||
;;
|
||||
node_log)
|
||||
if [[ -z $2 ]]; then
|
||||
|
||||
@ -1,564 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
clientproto "github.com/anytypeio/go-anytype-infrastructure-experiments/client/api/apiproto"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/logger"
|
||||
nodeproto "github.com/anytypeio/go-anytype-infrastructure-experiments/node/api/apiproto"
|
||||
"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/spf13/cobra"
|
||||
_ "github.com/spf13/cobra"
|
||||
"github.com/zeebo/errs"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const CName = "debug.api"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
type Service interface {
|
||||
app.ComponentRunnable
|
||||
}
|
||||
|
||||
type service struct {
|
||||
client client.Service
|
||||
node node.Service
|
||||
peers peers.Service
|
||||
clientCommands []*cobra.Command
|
||||
nodeCommands []*cobra.Command
|
||||
scripts []*cobra.Command
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
return &service{}
|
||||
}
|
||||
|
||||
func (s *service) Run(ctx context.Context) (err error) {
|
||||
rootCmd := &cobra.Command{Use: "debug"}
|
||||
|
||||
clientCmd := &cobra.Command{Use: "client commands to be executed on a specified client"}
|
||||
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 to be executed on a node"}
|
||||
nodeCmd.PersistentFlags().StringP("node", "n", "", "the alias of the node")
|
||||
nodeCmd.MarkFlagRequired("node")
|
||||
for _, cmd := range s.nodeCommands {
|
||||
nodeCmd.AddCommand(cmd)
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
func (s *service) Close(ctx context.Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.client = a.MustComponent(client.CName).(client.Service)
|
||||
s.node = a.MustComponent(node.CName).(node.Service)
|
||||
s.peers = a.MustComponent(peers.CName).(peers.Service)
|
||||
s.registerClientCommands()
|
||||
s.registerNodeCommands()
|
||||
s.registerScripts()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (s *service) registerClientCommands() {
|
||||
cmdCreateSpace := &cobra.Command{
|
||||
Use: "create-space",
|
||||
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 [space]",
|
||||
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",
|
||||
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 [space]",
|
||||
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 [document]",
|
||||
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[0])
|
||||
},
|
||||
}
|
||||
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 [text]",
|
||||
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")
|
||||
snapshot, _ := cmd.Flags().GetBool("snapshot")
|
||||
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],
|
||||
IsSnapshot: snapshot,
|
||||
})
|
||||
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.Flags().Bool("snapshot", false, "tells if the snapshot should be created")
|
||||
cmdAddText.MarkFlagRequired("space")
|
||||
cmdAddText.MarkFlagRequired("document")
|
||||
s.clientCommands = append(s.clientCommands, cmdAddText)
|
||||
|
||||
cmdAllTrees := &cobra.Command{
|
||||
Use: "all-trees [space]",
|
||||
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"
|
||||
}
|
||||
}
|
||||
fmt.Println(res)
|
||||
},
|
||||
}
|
||||
s.clientCommands = append(s.clientCommands, cmdAllTrees)
|
||||
|
||||
cmdDumpTree := &cobra.Command{
|
||||
Use: "dump-tree [document]",
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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 [document]",
|
||||
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
|
||||
}
|
||||
|
||||
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",
|
||||
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() {
|
||||
cmdAllTrees := &cobra.Command{
|
||||
Use: "all-trees [space]",
|
||||
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
|
||||
}
|
||||
resp, err := s.node.AllTrees(context.Background(), server.Address, &nodeproto.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"
|
||||
}
|
||||
}
|
||||
fmt.Println(res)
|
||||
},
|
||||
}
|
||||
s.nodeCommands = append(s.nodeCommands, cmdAllTrees)
|
||||
|
||||
cmdDumpTree := &cobra.Command{
|
||||
Use: "dump-tree [space]",
|
||||
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 [document]",
|
||||
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",
|
||||
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() {
|
||||
cmdAddTextMany := &cobra.Command{
|
||||
Use: "add-text-many [text]",
|
||||
Short: "add text to the document in the particular space in many clients at the same time with randomized snapshots",
|
||||
Args: cobra.RangeArgs(1, 1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
clients, _ := cmd.Flags().GetString("clients")
|
||||
space, _ := cmd.Flags().GetString("space")
|
||||
document, _ := cmd.Flags().GetString("document")
|
||||
times, _ := cmd.Flags().GetInt("times")
|
||||
if times <= 0 {
|
||||
fmt.Println("the times parameter should be more than 0")
|
||||
return
|
||||
}
|
||||
var addresses []string
|
||||
for _, cl := range strings.Split(clients, ",") {
|
||||
if len(cl) == 0 {
|
||||
continue
|
||||
}
|
||||
server, err := s.peers.Get(cl)
|
||||
if err != nil {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
addresses = append(addresses, server.Address)
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
var mError errs.Group
|
||||
createMany := func(address string) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < times; i++ {
|
||||
_, err := s.client.AddText(context.Background(), address, &clientproto.AddTextRequest{
|
||||
SpaceId: space,
|
||||
DocumentId: document,
|
||||
Text: args[0],
|
||||
IsSnapshot: rand.Int()%2 == 0,
|
||||
})
|
||||
if err != nil {
|
||||
mError.Add(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, p := range addresses {
|
||||
wg.Add(1)
|
||||
createMany(p)
|
||||
}
|
||||
wg.Wait()
|
||||
if mError.Err() != nil {
|
||||
fmt.Println("got errors while executing add many", mError.Err())
|
||||
return
|
||||
}
|
||||
return
|
||||
},
|
||||
}
|
||||
cmdAddTextMany.Flags().String("space", "", "the space where something is happening :-)")
|
||||
cmdAddTextMany.Flags().String("document", "", "the document where something is happening :-)")
|
||||
cmdAddTextMany.Flags().String("clients", "", "the aliases of clients with value separated by comma")
|
||||
cmdAddTextMany.Flags().Int("times", 1, "how many times we should add the change")
|
||||
cmdAddTextMany.MarkFlagRequired("space")
|
||||
cmdAddTextMany.MarkFlagRequired("document")
|
||||
cmdAddTextMany.MarkFlagRequired("clients")
|
||||
s.scripts = append(s.scripts, cmdAddTextMany)
|
||||
}
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/drpcclient"
|
||||
)
|
||||
|
||||
const CName = "api.client"
|
||||
const CName = "commands.client"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
293
util/cmd/debug/commands/clientcmds.go
Normal file
293
util/cmd/debug/commands/clientcmds.go
Normal file
@ -0,0 +1,293 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
clientproto "github.com/anytypeio/go-anytype-infrastructure-experiments/client/api/apiproto"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func (s *service) registerClientCommands() {
|
||||
cmdCreateSpace := &cobra.Command{
|
||||
Use: "create-space",
|
||||
Short: "create the space",
|
||||
Args: cobra.RangeArgs(0, 0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cli, _ := cmd.Flags().GetString("client")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
resp, err := s.client.CreateSpace(context.Background(), addr, &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 [space]",
|
||||
Short: "load the space",
|
||||
Args: cobra.RangeArgs(1, 1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cli, _ := cmd.Flags().GetString("client")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
|
||||
_, err := s.client.LoadSpace(context.Background(), addr, &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",
|
||||
Short: "derive the space from account data",
|
||||
Args: cobra.RangeArgs(0, 0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cli, _ := cmd.Flags().GetString("client")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.client.DeriveSpace(context.Background(), addr, &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 [space]",
|
||||
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")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
resp, err := s.client.CreateDocument(context.Background(), addr, &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 [document]",
|
||||
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")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
_, err := s.client.DeleteDocument(context.Background(), addr, &clientproto.DeleteDocumentRequest{
|
||||
SpaceId: space,
|
||||
DocumentId: args[0],
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("couldn't delete the document", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("deleted", args[0])
|
||||
},
|
||||
}
|
||||
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 [text]",
|
||||
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")
|
||||
snapshot, _ := cmd.Flags().GetBool("snapshot")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.client.AddText(context.Background(), addr, &clientproto.AddTextRequest{
|
||||
SpaceId: space,
|
||||
DocumentId: document,
|
||||
Text: args[0],
|
||||
IsSnapshot: snapshot,
|
||||
})
|
||||
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.Flags().Bool("snapshot", false, "tells if the snapshot should be created")
|
||||
cmdAddText.MarkFlagRequired("space")
|
||||
cmdAddText.MarkFlagRequired("document")
|
||||
s.clientCommands = append(s.clientCommands, cmdAddText)
|
||||
|
||||
cmdAllTrees := &cobra.Command{
|
||||
Use: "all-trees [space]",
|
||||
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")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.client.AllTrees(context.Background(), addr, &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"
|
||||
}
|
||||
}
|
||||
fmt.Println(res)
|
||||
},
|
||||
}
|
||||
s.clientCommands = append(s.clientCommands, cmdAllTrees)
|
||||
|
||||
cmdDumpTree := &cobra.Command{
|
||||
Use: "dump-tree [document]",
|
||||
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")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.client.DumpTree(context.Background(), addr, &clientproto.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.clientCommands = append(s.clientCommands, cmdDumpTree)
|
||||
|
||||
cmdTreeParams := &cobra.Command{
|
||||
Use: "tree-params [document]",
|
||||
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")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.client.TreeParams(context.Background(), addr, &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",
|
||||
Short: "print all spaces",
|
||||
Args: cobra.RangeArgs(0, 0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cli, _ := cmd.Flags().GetString("client")
|
||||
addr, ok := s.peers[cli]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.client.AllSpaces(context.Background(), addr, &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)
|
||||
}
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/drpcclient"
|
||||
)
|
||||
|
||||
const CName = "api.node"
|
||||
const CName = "commands.node"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
141
util/cmd/debug/commands/nodecmds.go
Normal file
141
util/cmd/debug/commands/nodecmds.go
Normal file
@ -0,0 +1,141 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
nodeproto "github.com/anytypeio/go-anytype-infrastructure-experiments/node/api/apiproto"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func (s *service) registerNodeCommands() {
|
||||
cmdAllTrees := &cobra.Command{
|
||||
Use: "all-trees [space]",
|
||||
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")
|
||||
addr, ok := s.peers[nd]
|
||||
if !ok {
|
||||
fmt.Println("no such node")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.node.AllTrees(context.Background(), addr, &nodeproto.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"
|
||||
}
|
||||
}
|
||||
fmt.Println(res)
|
||||
},
|
||||
}
|
||||
s.nodeCommands = append(s.nodeCommands, cmdAllTrees)
|
||||
|
||||
cmdDumpTree := &cobra.Command{
|
||||
Use: "dump-tree [space]",
|
||||
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")
|
||||
addr, ok := s.peers[nd]
|
||||
if !ok {
|
||||
fmt.Println("no such node")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.node.DumpTree(context.Background(), addr, &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 [document]",
|
||||
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")
|
||||
addr, ok := s.peers[nd]
|
||||
if !ok {
|
||||
fmt.Println("no such node")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.node.TreeParams(context.Background(), addr, &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",
|
||||
Short: "print all spaces",
|
||||
Args: cobra.RangeArgs(0, 0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
nd, _ := cmd.Flags().GetString("node")
|
||||
addr, ok := s.peers[nd]
|
||||
if !ok {
|
||||
fmt.Println("no such node")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := s.node.AllSpaces(context.Background(), addr, &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)
|
||||
}
|
||||
15
util/cmd/debug/commands/nodesmap.go
Normal file
15
util/cmd/debug/commands/nodesmap.go
Normal file
@ -0,0 +1,15 @@
|
||||
package commands
|
||||
|
||||
type NodesMap struct {
|
||||
Nodes []struct {
|
||||
Addresses []string `yaml:"grpcAddresses"`
|
||||
APIAddresses []string `yaml:"apiAddresses"`
|
||||
} `yaml:"nodes"`
|
||||
Consensus []struct {
|
||||
Addresses []string `yaml:"grpcAddresses"`
|
||||
}
|
||||
Clients []struct {
|
||||
Addresses []string `yaml:"grpcAddresses"`
|
||||
APIAddresses []string `yaml:"apiAddresses"`
|
||||
}
|
||||
}
|
||||
79
util/cmd/debug/commands/scripts.go
Normal file
79
util/cmd/debug/commands/scripts.go
Normal file
@ -0,0 +1,79 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
clientproto "github.com/anytypeio/go-anytype-infrastructure-experiments/client/api/apiproto"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/zeebo/errs"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func (s *service) registerScripts() {
|
||||
cmdAddTextMany := &cobra.Command{
|
||||
Use: "add-text-many [text]",
|
||||
Short: "add text to the document in the particular space in many clients at the same time with randomized snapshots",
|
||||
Args: cobra.RangeArgs(1, 1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
clients, _ := cmd.Flags().GetString("clients")
|
||||
space, _ := cmd.Flags().GetString("space")
|
||||
document, _ := cmd.Flags().GetString("document")
|
||||
times, _ := cmd.Flags().GetInt("times")
|
||||
if times <= 0 {
|
||||
fmt.Println("the times parameter should be more than 0")
|
||||
return
|
||||
}
|
||||
var addresses []string
|
||||
for _, cl := range strings.Split(clients, ",") {
|
||||
if len(cl) == 0 {
|
||||
continue
|
||||
}
|
||||
addr, ok := s.peers[cl]
|
||||
if !ok {
|
||||
fmt.Println("no such client")
|
||||
return
|
||||
}
|
||||
|
||||
addresses = append(addresses, addr)
|
||||
}
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
var mError errs.Group
|
||||
createMany := func(address string) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < times; i++ {
|
||||
_, err := s.client.AddText(context.Background(), address, &clientproto.AddTextRequest{
|
||||
SpaceId: space,
|
||||
DocumentId: document,
|
||||
Text: args[0],
|
||||
IsSnapshot: rand.Int()%2 == 0,
|
||||
})
|
||||
if err != nil {
|
||||
mError.Add(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, p := range addresses {
|
||||
wg.Add(1)
|
||||
createMany(p)
|
||||
}
|
||||
wg.Wait()
|
||||
if mError.Err() != nil {
|
||||
fmt.Println("got errors while executing add many", mError.Err())
|
||||
return
|
||||
}
|
||||
return
|
||||
},
|
||||
}
|
||||
cmdAddTextMany.Flags().String("space", "", "the space where something is happening :-)")
|
||||
cmdAddTextMany.Flags().String("document", "", "the document where something is happening :-)")
|
||||
cmdAddTextMany.Flags().String("clients", "", "the aliases of clients with value separated by comma")
|
||||
cmdAddTextMany.Flags().Int("times", 1, "how many times we should add the change")
|
||||
cmdAddTextMany.MarkFlagRequired("space")
|
||||
cmdAddTextMany.MarkFlagRequired("document")
|
||||
cmdAddTextMany.MarkFlagRequired("clients")
|
||||
s.scripts = append(s.scripts, cmdAddTextMany)
|
||||
}
|
||||
112
util/cmd/debug/commands/service.go
Normal file
112
util/cmd/debug/commands/service.go
Normal file
@ -0,0 +1,112 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"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/commands/client"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/commands/node"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const CName = "debug.commands"
|
||||
|
||||
var log = logger.NewNamed(CName)
|
||||
|
||||
type Service interface {
|
||||
app.ComponentRunnable
|
||||
}
|
||||
|
||||
type service struct {
|
||||
client client.Service
|
||||
node node.Service
|
||||
peers map[string]string
|
||||
clientCommands []*cobra.Command
|
||||
nodeCommands []*cobra.Command
|
||||
scripts []*cobra.Command
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
return &service{}
|
||||
}
|
||||
|
||||
func (s *service) Close(ctx context.Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.client = a.MustComponent(client.CName).(client.Service)
|
||||
s.node = a.MustComponent(node.CName).(node.Service)
|
||||
s.peers = map[string]string{}
|
||||
s.registerClientCommands()
|
||||
s.registerNodeCommands()
|
||||
s.registerScripts()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) Run(ctx context.Context) (err error) {
|
||||
rootCmd := &cobra.Command{Use: "debug", PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
cfgPath, err := cmd.Flags().GetString("config")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("no config flag is registered: %s", err.Error()))
|
||||
}
|
||||
err = s.parseAddresses(cfgPath)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("couldn't load config with addresses of nodes: %s", err.Error()))
|
||||
}
|
||||
}}
|
||||
rootCmd.PersistentFlags().String("config", "util/cmd/nodesgen/nodemap.yml", "nodes configuration")
|
||||
|
||||
clientCmd := &cobra.Command{Use: "client commands to be executed on a specified client"}
|
||||
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 to be executed on a node"}
|
||||
nodeCmd.PersistentFlags().StringP("node", "n", "", "the alias of the node")
|
||||
nodeCmd.MarkFlagRequired("node")
|
||||
for _, cmd := range s.nodeCommands {
|
||||
nodeCmd.AddCommand(cmd)
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (s *service) parseAddresses(path string) (err error) {
|
||||
nodesMap := &NodesMap{}
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(data, nodesMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for idx, n := range nodesMap.Nodes {
|
||||
s.peers[fmt.Sprintf("node%d", idx+1)] = n.APIAddresses[0]
|
||||
}
|
||||
for idx, c := range nodesMap.Clients {
|
||||
s.peers[fmt.Sprintf("client%d", idx+1)] = c.APIAddresses[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -4,11 +4,10 @@ import (
|
||||
"context"
|
||||
"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/api/client"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/api/node"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/commands"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/commands/client"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/commands/node"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/drpcclient"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/cmd/debug/peers"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"net/http"
|
||||
@ -41,7 +40,7 @@ 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))
|
||||
@ -50,8 +49,7 @@ func main() {
|
||||
|
||||
func Bootstrap(a *app.App) {
|
||||
a.Register(drpcclient.New()).
|
||||
Register(peers.New()).
|
||||
Register(client.New()).
|
||||
Register(node.New()).
|
||||
Register(api.New())
|
||||
Register(commands.New())
|
||||
}
|
||||
|
||||
@ -1,63 +0,0 @@
|
||||
package peers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/app"
|
||||
)
|
||||
|
||||
type PeerType int
|
||||
|
||||
const (
|
||||
PeerTypeClient PeerType = iota
|
||||
PeerTypeNode
|
||||
)
|
||||
|
||||
const CName = "debug.peers"
|
||||
|
||||
var ErrNoSuchPeer = errors.New("no such peer")
|
||||
|
||||
type Service interface {
|
||||
app.Component
|
||||
Get(alias string) (Peer, error)
|
||||
}
|
||||
|
||||
type Peer struct {
|
||||
PeerType PeerType
|
||||
Address string
|
||||
}
|
||||
|
||||
type service struct {
|
||||
peerMap map[string]Peer
|
||||
}
|
||||
|
||||
func New() Service {
|
||||
return &service{peerMap: map[string]Peer{}}
|
||||
}
|
||||
|
||||
func (s *service) Init(a *app.App) (err error) {
|
||||
s.peerMap["client1"] = Peer{
|
||||
PeerType: PeerTypeClient,
|
||||
Address: "127.0.0.1:8090",
|
||||
}
|
||||
s.peerMap["client2"] = Peer{
|
||||
PeerType: PeerTypeClient,
|
||||
Address: "127.0.0.1:8091",
|
||||
}
|
||||
s.peerMap["node1"] = Peer{
|
||||
PeerType: PeerTypeNode,
|
||||
Address: "127.0.0.1:8080",
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *service) Name() (name string) {
|
||||
return CName
|
||||
}
|
||||
|
||||
func (s *service) Get(alias string) (Peer, error) {
|
||||
peer, ok := s.peerMap[alias]
|
||||
if !ok {
|
||||
return Peer{}, ErrNoSuchPeer
|
||||
}
|
||||
return peer, nil
|
||||
}
|
||||
@ -34,6 +34,7 @@ type NodesMap struct {
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
nodesMap := &NodesMap{}
|
||||
data, err := ioutil.ReadFile(*flagNodeMap)
|
||||
if err != nil {
|
||||
@ -44,7 +45,6 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
var configs []config.Config
|
||||
var nodes []config.Node
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user