Skip to content

Commit

Permalink
Move api client builders to a cliutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Mar 5, 2021
1 parent d53c700 commit e03e321
Show file tree
Hide file tree
Showing 71 changed files with 877 additions and 843 deletions.
2 changes: 1 addition & 1 deletion build/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package build

import (
_ "github.com/golang/mock/mockgen"
_ "github.com/GeertJohan/go.rice/rice"
_ "github.com/golang/mock/mockgen"
_ "github.com/whyrusleeping/bencher"
)
13 changes: 7 additions & 6 deletions cli/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"github.com/filecoin-project/lotus/cli/util"

"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -32,13 +33,13 @@ var authCreateAdminToken = &cli.Command{
},

Action: func(cctx *cli.Context) error {
napi, closer, err := GetAPI(cctx)
napi, closer, err := cliutil.GetAPI(cctx)
if err != nil {
return err
}
defer closer()

ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

if !cctx.IsSet("perm") {
return xerrors.New("--perm flag not set")
Expand Down Expand Up @@ -80,13 +81,13 @@ var authApiInfoToken = &cli.Command{
},

Action: func(cctx *cli.Context) error {
napi, closer, err := GetAPI(cctx)
napi, closer, err := cliutil.GetAPI(cctx)
if err != nil {
return err
}
defer closer()

ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

if !cctx.IsSet("perm") {
return xerrors.New("--perm flag not set")
Expand Down Expand Up @@ -120,14 +121,14 @@ var authApiInfoToken = &cli.Command{
log.Errorf("repoType type does not match the type of repo.RepoType")
}

ainfo, err := GetAPIInfo(cctx, t)
ainfo, err := cliutil.GetAPIInfo(cctx, t)
if err != nil {
return xerrors.Errorf("could not get API info: %w", err)
}

// TODO: Log in audit log when it is implemented

fmt.Printf("%s=%s:%s\n", envForRepo(t), string(token), ainfo.Addr)
fmt.Printf("%s=%s:%s\n", cliutil.EnvForRepo(t), string(token), ainfo.Addr)
return nil
},
}
3 changes: 2 additions & 1 deletion cli/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"
"fmt"
"github.com/filecoin-project/lotus/cli/util"
"os"

logging "github.com/ipfs/go-log/v2"
Expand Down Expand Up @@ -84,7 +85,7 @@ func BackupCmd(repoFlag string, rt repo.RepoType, getApi BackupApiFn) *cli.Comma
}
defer closer()

err = api.CreateBackup(ReqContext(cctx), cctx.Args().First())
err = api.CreateBackup(cliutil.ReqContext(cctx), cctx.Args().First())
if err != nil {
return err
}
Expand Down
65 changes: 33 additions & 32 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/filecoin-project/lotus/cli/util"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -66,12 +67,12 @@ var chainHeadCmd = &cli.Command{
Name: "head",
Usage: "Print chain head",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

head, err := api.ChainHead(ctx)
if err != nil {
Expand All @@ -96,12 +97,12 @@ var chainGetBlock = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must pass cid of block to print")
Expand Down Expand Up @@ -181,12 +182,12 @@ var chainReadObjCmd = &cli.Command{
Usage: "Read the raw bytes of an object",
ArgsUsage: "[objectCid]",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

c, err := cid.Decode(cctx.Args().First())
if err != nil {
Expand Down Expand Up @@ -214,12 +215,12 @@ var chainDeleteObjCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

c, err := cid.Decode(cctx.Args().First())
if err != nil {
Expand Down Expand Up @@ -256,12 +257,12 @@ var chainStatObjCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

obj, err := cid.Decode(cctx.Args().First())
if err != nil {
Expand Down Expand Up @@ -296,12 +297,12 @@ var chainGetMsgCmd = &cli.Command{
return fmt.Errorf("must pass a cid of a message to get")
}

api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

c, err := cid.Decode(cctx.Args().First())
if err != nil {
Expand Down Expand Up @@ -350,12 +351,12 @@ var chainSetHeadCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

var ts *types.TipSet

Expand Down Expand Up @@ -405,12 +406,12 @@ var chainInspectUsage = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

ts, err := LoadTipSet(ctx, cctx, api)
if err != nil {
Expand Down Expand Up @@ -547,12 +548,12 @@ var chainListCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

var head *types.TipSet

Expand Down Expand Up @@ -706,12 +707,12 @@ var chainGetCmd = &cli.Command{
- account-state
`,
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

p := path.Clean(cctx.Args().First())
if strings.HasPrefix(p, "/pstate") {
Expand Down Expand Up @@ -923,12 +924,12 @@ var chainBisectCmd = &cli.Command{
For special path elements see 'chain get' help
`,
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

if cctx.Args().Len() < 4 {
return xerrors.New("need at least 4 args")
Expand Down Expand Up @@ -1045,12 +1046,12 @@ var chainExportCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

if !cctx.Args().Present() {
return fmt.Errorf("must specify filename to export chain to")
Expand Down Expand Up @@ -1121,12 +1122,12 @@ var slashConsensusFault = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

c1, err := cid.Parse(cctx.Args().Get(0))
if err != nil {
Expand Down Expand Up @@ -1231,12 +1232,12 @@ var chainGasPriceCmd = &cli.Command{
Name: "gas-price",
Usage: "Estimate gas prices",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

nb := []int{1, 2, 3, 5, 10, 20, 50, 100, 300}
for _, nblocks := range nb {
Expand Down Expand Up @@ -1277,12 +1278,12 @@ var chainDecodeParamsCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

if cctx.Args().Len() != 3 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
Expand Down Expand Up @@ -1357,12 +1358,12 @@ var chainEncodeParamsCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
api, closer, err := cliutil.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ctx := cliutil.ReqContext(cctx)

if cctx.Args().Len() != 3 {
return ShowHelp(cctx, fmt.Errorf("incorrect number of arguments"))
Expand Down
Loading

0 comments on commit e03e321

Please sign in to comment.