Fix APIInfo import cycle in tests
This commit is contained in:
parent
3cd53ad9d9
commit
6cc7559b04
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/filecoin-project/lotus/api"
|
"github.com/filecoin-project/lotus/api"
|
||||||
"github.com/filecoin-project/lotus/api/client"
|
"github.com/filecoin-project/lotus/api/client"
|
||||||
lcli "github.com/filecoin-project/lotus/cli"
|
cliutil "github.com/filecoin-project/lotus/cli/util"
|
||||||
"github.com/filecoin-project/lotus/node/modules/helpers"
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ type RemoteWallet struct {
|
|||||||
|
|
||||||
func SetupRemoteWallet(info string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) {
|
func SetupRemoteWallet(info string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) {
|
||||||
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) {
|
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) {
|
||||||
ai := lcli.ParseApiInfo(info)
|
ai := cliutil.ParseApiInfo(info)
|
||||||
|
|
||||||
url, err := ai.DialArgs()
|
url, err := ai.DialArgs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
88
cli/cmd.go
88
cli/cmd.go
@ -4,17 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
logging "github.com/ipfs/go-log/v2"
|
logging "github.com/ipfs/go-log/v2"
|
||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
"github.com/multiformats/go-multiaddr"
|
|
||||||
manet "github.com/multiformats/go-multiaddr/net"
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
@ -22,6 +18,7 @@ import (
|
|||||||
|
|
||||||
"github.com/filecoin-project/lotus/api"
|
"github.com/filecoin-project/lotus/api"
|
||||||
"github.com/filecoin-project/lotus/api/client"
|
"github.com/filecoin-project/lotus/api/client"
|
||||||
|
cliutil "github.com/filecoin-project/lotus/cli/util"
|
||||||
"github.com/filecoin-project/lotus/node/repo"
|
"github.com/filecoin-project/lotus/node/repo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -48,57 +45,6 @@ func NewCliError(s string) error {
|
|||||||
// ApiConnector returns API instance
|
// ApiConnector returns API instance
|
||||||
type ApiConnector func() api.FullNode
|
type ApiConnector func() api.FullNode
|
||||||
|
|
||||||
type APIInfo struct {
|
|
||||||
Addr string
|
|
||||||
Token []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a APIInfo) DialArgs() (string, error) {
|
|
||||||
ma, err := multiaddr.NewMultiaddr(a.Addr)
|
|
||||||
if err == nil {
|
|
||||||
_, addr, err := manet.DialArgs(ma)
|
|
||||||
if err != nil {
|
|
||||||
return "", 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 {
|
|
||||||
if len(a.Token) != 0 {
|
|
||||||
headers := http.Header{}
|
|
||||||
headers.Add("Authorization", "Bearer "+string(a.Token))
|
|
||||||
return headers
|
|
||||||
}
|
|
||||||
log.Warn("API Token not set and requested, capabilities might be limited.")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// The flag passed on the command line with the listen address of the API
|
// The flag passed on the command line with the listen address of the API
|
||||||
// server (only used by the tests)
|
// server (only used by the tests)
|
||||||
func flagForAPI(t repo.RepoType) string {
|
func flagForAPI(t repo.RepoType) string {
|
||||||
@ -154,25 +100,7 @@ func envForRepoDeprecation(t repo.RepoType) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (cliutil.APIInfo, error) {
|
||||||
infoWithToken = regexp.MustCompile("^[a-zA-Z0-9\\-_]+?\\.[a-zA-Z0-9\\-_]+?\\.([a-zA-Z0-9\\-_]+)?:.+$")
|
|
||||||
)
|
|
||||||
|
|
||||||
func ParseApiInfo(s string) APIInfo {
|
|
||||||
var tok []byte
|
|
||||||
if infoWithToken.Match([]byte(s)) {
|
|
||||||
sp := strings.SplitN(s, ":", 2)
|
|
||||||
tok = []byte(sp[0])
|
|
||||||
s = sp[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
return APIInfo{
|
|
||||||
Addr: s,
|
|
||||||
Token: tok,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
|
|
||||||
// Check if there was a flag passed with the listen address of the API
|
// Check if there was a flag passed with the listen address of the API
|
||||||
// server (only used by the tests)
|
// server (only used by the tests)
|
||||||
apiFlag := flagForAPI(t)
|
apiFlag := flagForAPI(t)
|
||||||
@ -180,7 +108,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
|
|||||||
strma := ctx.String(apiFlag)
|
strma := ctx.String(apiFlag)
|
||||||
strma = strings.TrimSpace(strma)
|
strma = strings.TrimSpace(strma)
|
||||||
|
|
||||||
return APIInfo{Addr: strma}, nil
|
return cliutil.APIInfo{Addr: strma}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
envKey := envForRepo(t)
|
envKey := envForRepo(t)
|
||||||
@ -194,24 +122,24 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ok {
|
if ok {
|
||||||
return ParseApiInfo(env), nil
|
return cliutil.ParseApiInfo(env), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
repoFlag := flagForRepo(t)
|
repoFlag := flagForRepo(t)
|
||||||
|
|
||||||
p, err := homedir.Expand(ctx.String(repoFlag))
|
p, err := homedir.Expand(ctx.String(repoFlag))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return APIInfo{}, xerrors.Errorf("could not expand home dir (%s): %w", repoFlag, err)
|
return cliutil.APIInfo{}, xerrors.Errorf("could not expand home dir (%s): %w", repoFlag, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := repo.NewFS(p)
|
r, err := repo.NewFS(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return APIInfo{}, xerrors.Errorf("could not open repo at path: %s; %w", p, err)
|
return cliutil.APIInfo{}, xerrors.Errorf("could not open repo at path: %s; %w", p, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ma, err := r.APIEndpoint()
|
ma, err := r.APIEndpoint()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return APIInfo{}, xerrors.Errorf("could not get api endpoint: %w", err)
|
return cliutil.APIInfo{}, xerrors.Errorf("could not get api endpoint: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := r.APIToken()
|
token, err := r.APIToken()
|
||||||
@ -219,7 +147,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
|
|||||||
log.Warnf("Couldn't load CLI token, capabilities may be limited: %v", err)
|
log.Warnf("Couldn't load CLI token, capabilities may be limited: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return APIInfo{
|
return cliutil.APIInfo{
|
||||||
Addr: ma.String(),
|
Addr: ma.String(),
|
||||||
Token: token,
|
Token: token,
|
||||||
}, nil
|
}, nil
|
||||||
|
83
cli/util/apiinfo.go
Normal file
83
cli/util/apiinfo.go
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package cliutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
logging "github.com/ipfs/go-log/v2"
|
||||||
|
"github.com/multiformats/go-multiaddr"
|
||||||
|
manet "github.com/multiformats/go-multiaddr/net"
|
||||||
|
)
|
||||||
|
|
||||||
|
var log = logging.Logger("cliutil")
|
||||||
|
|
||||||
|
var (
|
||||||
|
infoWithToken = regexp.MustCompile("^[a-zA-Z0-9\\-_]+?\\.[a-zA-Z0-9\\-_]+?\\.([a-zA-Z0-9\\-_]+)?:.+$")
|
||||||
|
)
|
||||||
|
|
||||||
|
type APIInfo struct {
|
||||||
|
Addr string
|
||||||
|
Token []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseApiInfo(s string) APIInfo {
|
||||||
|
var tok []byte
|
||||||
|
if infoWithToken.Match([]byte(s)) {
|
||||||
|
sp := strings.SplitN(s, ":", 2)
|
||||||
|
tok = []byte(sp[0])
|
||||||
|
s = sp[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return APIInfo{
|
||||||
|
Addr: s,
|
||||||
|
Token: tok,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a APIInfo) DialArgs() (string, error) {
|
||||||
|
ma, err := multiaddr.NewMultiaddr(a.Addr)
|
||||||
|
if err == nil {
|
||||||
|
_, addr, err := manet.DialArgs(ma)
|
||||||
|
if err != nil {
|
||||||
|
return "", 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 {
|
||||||
|
if len(a.Token) != 0 {
|
||||||
|
headers := http.Header{}
|
||||||
|
headers.Add("Authorization", "Bearer "+string(a.Token))
|
||||||
|
return headers
|
||||||
|
}
|
||||||
|
log.Warn("API Token not set and requested, capabilities might be limited.")
|
||||||
|
return nil
|
||||||
|
}
|
@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/filecoin-project/lotus/build"
|
"github.com/filecoin-project/lotus/build"
|
||||||
"github.com/filecoin-project/lotus/chain/types"
|
"github.com/filecoin-project/lotus/chain/types"
|
||||||
lcli "github.com/filecoin-project/lotus/cli"
|
lcli "github.com/filecoin-project/lotus/cli"
|
||||||
|
cliutil "github.com/filecoin-project/lotus/cli/util"
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
"github.com/multiformats/go-multiaddr"
|
"github.com/multiformats/go-multiaddr"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -111,7 +112,7 @@ var consensusCheckCmd = &cli.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ainfo := lcli.APIInfo{Addr: apima.String()}
|
ainfo := cliutil.APIInfo{Addr: apima.String()}
|
||||||
addr, err := ainfo.DialArgs()
|
addr, err := ainfo.DialArgs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user