mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-09 17:44:59 +09:00
cli cafe findprofile
This commit is contained in:
parent
16328691ed
commit
e8ac5b71b8
5 changed files with 141 additions and 289 deletions
97
cmd/cli/cafe.go
Normal file
97
cmd/cli/cafe.go
Normal 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")
|
||||
}
|
|
@ -14,6 +14,7 @@ var CliCmd = &cobra.Command{
|
|||
func init() {
|
||||
// subcommands
|
||||
CliCmd.AddCommand(migrateCmd)
|
||||
CliCmd.AddCommand(cafeCmd)
|
||||
// local flags
|
||||
}
|
||||
|
||||
|
|
1
go.mod
1
go.mod
|
@ -46,6 +46,7 @@ require (
|
|||
github.com/libp2p/go-libp2p-swarm v0.4.2
|
||||
github.com/libp2p/go-libp2p-tls v0.1.3
|
||||
github.com/libp2p/go-tcp-transport v0.2.1
|
||||
github.com/logrusorgru/aurora v2.0.3+incompatible
|
||||
github.com/magiconair/properties v1.8.4
|
||||
github.com/mauidude/go-readability v0.0.0-20141216012317-2f30b1a346f1
|
||||
github.com/mb0/diff v0.0.0-20131118162322-d8d9a906c24d
|
||||
|
|
40
util/console/console.go
Normal file
40
util/console/console.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
)
|
||||
|
||||
func Message(format string, args ...interface{}) {
|
||||
if len(format) > 0 {
|
||||
fmt.Println(aurora.Sprintf(aurora.BrightBlack("> "+format), args...))
|
||||
}
|
||||
}
|
||||
|
||||
func Success(format string, args ...interface{}) {
|
||||
fmt.Println(aurora.Sprintf(aurora.Green("> Success! %s"),
|
||||
aurora.Sprintf(aurora.BrightBlack(format), args...)))
|
||||
}
|
||||
|
||||
func Warn(format string, args ...interface{}) {
|
||||
fmt.Println(aurora.Sprintf(aurora.Magenta("> Warning! %s"),
|
||||
aurora.Sprintf(aurora.BrightBlack(format), args...)))
|
||||
}
|
||||
|
||||
func Error(format string, args ...interface{}) {
|
||||
fmt.Println(aurora.Sprintf(aurora.Yellow("> Error! %s"),
|
||||
aurora.Sprintf(aurora.BrightBlack(format), args...)))
|
||||
}
|
||||
|
||||
func End(format string, args ...interface{}) {
|
||||
Message(format, args...)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func Fatal(format string, args ...interface{}) {
|
||||
fmt.Println(aurora.Sprintf(aurora.Red("> Fatal! %s"),
|
||||
aurora.Sprintf(aurora.BrightBlack(format), args...)))
|
||||
os.Exit(1)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue