From 01ad4c0514b42b07294e62e43e6ca2a1e558dab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 27 Nov 2020 15:57:00 +0100 Subject: [PATCH 1/2] shed rpc: Allow calling with args --- cmd/lotus-shed/rpc.go | 101 ++++++++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 39 deletions(-) diff --git a/cmd/lotus-shed/rpc.go b/cmd/lotus-shed/rpc.go index 924bd197c..6720a480e 100644 --- a/cmd/lotus-shed/rpc.go +++ b/cmd/lotus-shed/rpc.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "net/http" "net/url" + "os" "strings" "text/scanner" @@ -55,45 +56,7 @@ var rpcCmd = &cli.Command{ cs.Close() // nolint:errcheck }() - 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:] - + send := func(method, params string) error { jreq, err := json.Marshal(struct { Jsonrpc string `json:"jsonrpc"` ID int `json:"id"` @@ -129,6 +92,66 @@ var rpcCmd = &cli.Command{ if err := resp.Body.Close(); err != nil { 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 From 70732ac423b59c9976182349aec3ece665e86186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 30 Nov 2020 14:26:42 +0100 Subject: [PATCH 2/2] Fix lint --- cmd/lotus-shed/rpc.go | 70 +++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/cmd/lotus-shed/rpc.go b/cmd/lotus-shed/rpc.go index 6720a480e..cbee95c6b 100644 --- a/cmd/lotus-shed/rpc.go +++ b/cmd/lotus-shed/rpc.go @@ -108,49 +108,49 @@ var rpcCmd = &cli.Command{ } 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, + cctx.App.Metadata["repoType"] = repo.FullNode + if err := lcli.VersionCmd.Action(cctx); err != nil { + return err + } + fmt.Println("Usage: > Method [Param1, Param2, ...]") - // TODO: Some basic auto completion - }) - if err != nil { - return err - } + rl, err := readline.NewEx(&readline.Config{ + Stdin: cs, + HistoryFile: "/tmp/lotusrpc.tmp", + Prompt: "> ", + EOFPrompt: "exit", + HistorySearchFold: true, - for { - line, err := rl.Readline() - if err == readline.ErrInterrupt { - if len(line) == 0 { - break - } else { - continue - } - } else if err == io.EOF { + // 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() + var s scanner.Scanner + s.Init(strings.NewReader(line)) + s.Scan() + method := s.TokenText() - s.Scan() - params := line[s.Position.Offset:] + s.Scan() + params := line[s.Position.Offset:] - if err := send(method, params); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "%v", err) - } + if err := send(method, params); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "%v", err) } }