1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 09:35:00 +09:00

cli cafe findprofile

This commit is contained in:
requilence 2021-03-25 12:07:18 +04:00
parent 16328691ed
commit e8ac5b71b8
No known key found for this signature in database
GPG key ID: F07A7D55A2684852
5 changed files with 141 additions and 289 deletions

97
cmd/cli/cafe.go Normal file
View file

@ -0,0 +1,97 @@
package main
import (
"context"
"fmt"
core2 "github.com/anytypeio/go-anytype-middleware/pkg/lib/core"
"github.com/anytypeio/go-anytype-middleware/pkg/lib/wallet"
"github.com/anytypeio/go-anytype-middleware/util/console"
"github.com/spf13/cobra"
"io/ioutil"
"os"
)
var cafeCmd = &cobra.Command{
Use: "cafe",
Short: "Cafe-specific commands",
}
var (
mnemonic string
account string
)
var findProfiles = &cobra.Command{
Use: "findprofiles",
Short: "Find profiles by mnemonic or accountId",
Run: func(c *cobra.Command, args []string) {
var (
appMnemonic string
appAccount wallet.Keypair
accountsToFind []string
err error
)
if mnemonic != "" {
for i:=0; i<10; i++ {
ac, err := core2.WalletAccountAt(mnemonic, i, "")
if err != nil {
console.Fatal("failed to get account from provided mnemonic: %s", err.Error())
return
}
accountsToFind = append(accountsToFind, ac.Address())
}
} else if account != "" {
accountsToFind = []string{account}
} else {
console.Fatal("no mnemonic or account provided")
return
}
// create temp wallet in order to do requests to cafe
appMnemonic, err = core2.WalletGenerateMnemonic(12)
appAccount, err = core2.WalletAccountAt(appMnemonic, 0, "")
rootPath, err := ioutil.TempDir(os.TempDir(), "anytype_*")
rawSeed, err := appAccount.Raw()
err = core2.WalletInitRepo(rootPath, rawSeed)
var opts = []core2.ServiceOption{core2.WithRootPathAndAccount(rootPath, appAccount.Address())}
a, _ := core2.New(opts...)
err = a.Start()
if err != nil {
fmt.Println("failed to start: "+ err.Error())
return
}
var found bool
var ch = make(chan core2.Profile)
closeCh := make(chan struct{})
go func() {
defer close(closeCh)
select {
case profile, ok := <-ch:
if !ok {
return
}
found = true
console.Success("got profile: id=%s name=%s", profile.AccountAddr, profile.Name)
}
}()
err = a.FindProfilesByAccountIDs(context.Background(), accountsToFind, ch)
if err != nil {
console.Fatal("failed to query cafe: " + err.Error())
}
<-closeCh
if !found {
console.Fatal("no accounts found on cafe")
}
},
}
func init() {
// subcommands
cafeCmd.AddCommand(findProfiles)
findProfiles.PersistentFlags().StringVarP(&mnemonic, "mnemonic", "", "", "mnemonic to find profiles on")
findProfiles.PersistentFlags().StringVarP(&account, "account", "a", "", "account to find profiles on")
}

View file

@ -14,6 +14,7 @@ var CliCmd = &cobra.Command{
func init() {
// subcommands
CliCmd.AddCommand(migrateCmd)
CliCmd.AddCommand(cafeCmd)
// local flags
}