Merge pull request #4241 from filecoin-project/fix/daemon-flag-clash

fix: clash between daemon --api flag and cli tests
This commit is contained in:
Łukasz Magiera 2020-10-08 18:30:47 +02:00 committed by GitHub
commit d26ece10b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 25 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
@ -47,14 +48,44 @@ func NewCliError(s string) error {
type ApiConnector func() api.FullNode
type APIInfo struct {
Addr multiaddr.Multiaddr
Addr string
Token []byte
}
func (a APIInfo) DialArgs() (string, error) {
_, addr, err := manet.DialArgs(a.Addr)
ma, err := multiaddr.NewMultiaddr(a.Addr)
if err == nil {
_, addr, err := manet.DialArgs(ma)
if err != nil {
return "", err
}
return "ws://" + addr + "/rpc/v0", err
return "ws://" + addr + "/rpc/v0", nil
}
_, err = url.Parse(a.Addr)
if err != nil {
return "", err
}
return a.Addr + "/rpc/v0", nil
}
func (a APIInfo) Host() (string, error) {
ma, err := multiaddr.NewMultiaddr(a.Addr)
if err == nil {
_, addr, err := manet.DialArgs(ma)
if err != nil {
return "", err
}
return addr, nil
}
spec, err := url.Parse(a.Addr)
if err != nil {
return "", err
}
return spec.Host, nil
}
func (a APIInfo) AuthHeader() http.Header {
@ -72,11 +103,11 @@ func (a APIInfo) AuthHeader() http.Header {
func flagForAPI(t repo.RepoType) string {
switch t {
case repo.FullNode:
return "api"
return "api-url"
case repo.StorageMiner:
return "miner-api"
return "miner-api-url"
case repo.Worker:
return "worker-api"
return "worker-api-url"
default:
panic(fmt.Sprintf("Unknown repo type: %v", t))
}
@ -130,11 +161,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
strma := ctx.String(apiFlag)
strma = strings.TrimSpace(strma)
apima, err := multiaddr.NewMultiaddr(strma)
if err != nil {
return APIInfo{}, err
}
return APIInfo{Addr: apima}, nil
return APIInfo{Addr: strma}, nil
}
envKey := envForRepo(t)
@ -152,12 +179,8 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
if len(sp) != 2 {
log.Warnf("invalid env(%s) value, missing token or address", envKey)
} else {
ma, err := multiaddr.NewMultiaddr(sp[1])
if err != nil {
return APIInfo{}, xerrors.Errorf("could not parse multiaddr from env(%s): %w", envKey, err)
}
return APIInfo{
Addr: ma,
Addr: sp[1],
Token: []byte(sp[0]),
}, nil
}
@ -186,7 +209,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
}
return APIInfo{
Addr: ma,
Addr: ma.String(),
Token: token,
}, nil
}

View File

@ -439,12 +439,12 @@ type mockCLI struct {
}
func newMockCLI(t *testing.T) *mockCLI {
// Create a CLI App with an --api flag so that we can specify which node
// Create a CLI App with an --api-url flag so that we can specify which node
// the command should be executed against
app := cli.NewApp()
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "api",
Name: "api-url",
Hidden: true,
},
}
@ -476,8 +476,8 @@ func (c *mockCLIClient) runCmd(cmd *cli.Command, input []string) string {
}
func (c *mockCLIClient) runCmdRaw(cmd *cli.Command, input []string) (string, error) {
// prepend --api=<node api listener address>
apiFlag := "--api=" + c.addr.String()
// prepend --api-url=<node api listener address>
apiFlag := "--api-url=" + c.addr.String()
input = append([]string{apiFlag}, input...)
fs := c.flagSet(cmd)
@ -493,7 +493,7 @@ func (c *mockCLIClient) runCmdRaw(cmd *cli.Command, input []string) (string, err
}
func (c *mockCLIClient) flagSet(cmd *cli.Command) *flag.FlagSet {
// Apply app level flags (so we can process --api flag)
// Apply app level flags (so we can process --api-url flag)
fs := &flag.FlagSet{}
for _, f := range c.cctx.App.Flags {
err := f.Apply(fs)

View File

@ -9,7 +9,6 @@ import (
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/node/repo"
manet "github.com/multiformats/go-multiaddr/net"
)
var pprofCmd = &cli.Command{
@ -37,7 +36,7 @@ var PprofGoroutines = &cli.Command{
if err != nil {
return xerrors.Errorf("could not get API info: %w", err)
}
_, addr, err := manet.DialArgs(ainfo.Addr)
addr, err := ainfo.Host()
if err != nil {
return err
}

View File

@ -111,7 +111,7 @@ var consensusCheckCmd = &cli.Command{
if err != nil {
return err
}
ainfo := lcli.APIInfo{Addr: apima}
ainfo := lcli.APIInfo{Addr: apima.String()}
addr, err := ainfo.DialArgs()
if err != nil {
return err