Skip to content

Commit

Permalink
fix: clash between daemon --api flag and cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Oct 8, 2020
1 parent 8f35a5c commit 893ff47
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
57 changes: 40 additions & 17 deletions cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -186,7 +209,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
}

return APIInfo{
Addr: ma,
Addr: ma.String(),
Token: token,
}, nil
}
Expand Down
10 changes: 5 additions & 5 deletions cli/paych_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions cli/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/lotus-shed/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ var consensusCheckCmd = &cli.Command{
continue
}

apima, err := multiaddr.NewMultiaddr(strma)
if err != nil {
return err
}
ainfo := lcli.APIInfo{Addr: apima}
ainfo := lcli.APIInfo{Addr: strma}
addr, err := ainfo.DialArgs()
if err != nil {
return err
Expand Down

0 comments on commit 893ff47

Please sign in to comment.