shed rpc: Allow calling with args
This commit is contained in:
parent
aa278443d3
commit
01ad4c0514
@ -9,6 +9,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"text/scanner"
|
"text/scanner"
|
||||||
|
|
||||||
@ -55,45 +56,7 @@ var rpcCmd = &cli.Command{
|
|||||||
cs.Close() // nolint:errcheck
|
cs.Close() // nolint:errcheck
|
||||||
}()
|
}()
|
||||||
|
|
||||||
cctx.App.Metadata["repoType"] = repo.FullNode
|
send := func(method, params string) error {
|
||||||
if err := lcli.VersionCmd.Action(cctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Println("Usage: > Method [Param1, Param2, ...]")
|
|
||||||
|
|
||||||
rl, err := readline.NewEx(&readline.Config{
|
|
||||||
Stdin: cs,
|
|
||||||
HistoryFile: "/tmp/lotusrpc.tmp",
|
|
||||||
Prompt: "> ",
|
|
||||||
EOFPrompt: "exit",
|
|
||||||
HistorySearchFold: true,
|
|
||||||
|
|
||||||
// TODO: Some basic auto completion
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
line, err := rl.Readline()
|
|
||||||
if err == readline.ErrInterrupt {
|
|
||||||
if len(line) == 0 {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
var s scanner.Scanner
|
|
||||||
s.Init(strings.NewReader(line))
|
|
||||||
s.Scan()
|
|
||||||
method := s.TokenText()
|
|
||||||
|
|
||||||
s.Scan()
|
|
||||||
params := line[s.Position.Offset:]
|
|
||||||
|
|
||||||
jreq, err := json.Marshal(struct {
|
jreq, err := json.Marshal(struct {
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
Jsonrpc string `json:"jsonrpc"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
@ -129,6 +92,66 @@ var rpcCmd = &cli.Command{
|
|||||||
if err := resp.Body.Close(); err != nil {
|
if err := resp.Body.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if cctx.Args().Present() {
|
||||||
|
if cctx.Args().Len() > 2 {
|
||||||
|
return xerrors.Errorf("expected 1 or 2 arguments: method [params]")
|
||||||
|
}
|
||||||
|
|
||||||
|
params := cctx.Args().Get(1)
|
||||||
|
if params == "" {
|
||||||
|
// TODO: try to be smart and use zero-values for method
|
||||||
|
params = "[]"
|
||||||
|
}
|
||||||
|
|
||||||
|
return send(cctx.Args().Get(0), params)
|
||||||
|
} else {
|
||||||
|
cctx.App.Metadata["repoType"] = repo.FullNode
|
||||||
|
if err := lcli.VersionCmd.Action(cctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println("Usage: > Method [Param1, Param2, ...]")
|
||||||
|
|
||||||
|
rl, err := readline.NewEx(&readline.Config{
|
||||||
|
Stdin: cs,
|
||||||
|
HistoryFile: "/tmp/lotusrpc.tmp",
|
||||||
|
Prompt: "> ",
|
||||||
|
EOFPrompt: "exit",
|
||||||
|
HistorySearchFold: true,
|
||||||
|
|
||||||
|
// TODO: Some basic auto completion
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
line, err := rl.Readline()
|
||||||
|
if err == readline.ErrInterrupt {
|
||||||
|
if len(line) == 0 {
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
var s scanner.Scanner
|
||||||
|
s.Init(strings.NewReader(line))
|
||||||
|
s.Scan()
|
||||||
|
method := s.TokenText()
|
||||||
|
|
||||||
|
s.Scan()
|
||||||
|
params := line[s.Position.Offset:]
|
||||||
|
|
||||||
|
if err := send(method, params); err != nil {
|
||||||
|
_, _ = fmt.Fprintf(os.Stderr, "%v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user