Skip to content

Commit

Permalink
minor refactor of prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
zl03jsj committed Jun 17, 2022
1 parent 109d1f6 commit 6f45ae4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
26 changes: 18 additions & 8 deletions cmd/venus-market/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"context"
"fmt"
"log"
"os"
"strings"

"github.com/filecoin-project/venus-market/v2/cmd"

logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -156,40 +159,47 @@ func main() {
}
}

func prepare(cctx *cli.Context) (*config.MarketConfig, error) {
func prepare(cctx *cli.Context, defSignerType config.SignerType) error {
if !cctx.IsSet(HidenSignerTypeFlag.Name) {
if err := cctx.Set(HidenSignerTypeFlag.Name, defSignerType); err != nil {
return fmt.Errorf("set %s with wallet failed %v", HidenSignerTypeFlag.Name, err)
}
}
cfg := config.DefaultMarketConfig
cfg.HomeDir = cctx.String(RepoFlag.Name)
cfgPath, err := cfg.ConfigPath()
if err != nil {
return nil, err
return err
}
mainLog.Info("load config from path ", cfgPath)
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
//create
err = flagData(cctx, cfg)
if err != nil {
return nil, fmt.Errorf("parser data from flag %w", err)
return fmt.Errorf("parser data from flag %w", err)
}

err = config.SaveConfig(cfg)
if err != nil {
return nil, fmt.Errorf("save config to %s %w", cfgPath, err)
return fmt.Errorf("save config to %s %w", cfgPath, err)
}
} else if err == nil {
//loadConfig
err = config.LoadConfig(cfgPath, cfg)
if err != nil {
return nil, err
return err
}

err = flagData(cctx, cfg)
if err != nil {
return nil, fmt.Errorf("parser data from flag %w", err)
return fmt.Errorf("parser data from flag %w", err)
}
} else {
return nil, err
return err
}
return cfg, nil

cctx.Context = context.WithValue(cctx.Context, contextKeyMarketConfig, cfg)
return cmd.FetchAndLoadBundles(cctx.Context, cfg.Node)
}

func flagData(cctx *cli.Context, cfg *config.MarketConfig) error {
Expand Down
14 changes: 1 addition & 13 deletions cmd/venus-market/pool-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"

"github.com/filecoin-project/venus-market/v2/cmd"

"github.com/filecoin-project/venus-auth/cmd/jwtclient"
"github.com/filecoin-project/venus-market/v2/api/clients"
"github.com/filecoin-project/venus-market/v2/api/impl"
Expand Down Expand Up @@ -130,15 +128,5 @@ func poolDaemon(cctx *cli.Context) error {
}

var beforePoolRunCmd = func(cctx *cli.Context) error {
if !cctx.IsSet(HidenSignerTypeFlag.Name) {
if err := cctx.Set(HidenSignerTypeFlag.Name, config.SignerTypeGateway); err != nil {
return fmt.Errorf("set %s with wallet failed %v", HidenSignerTypeFlag.Name, err)
}
}
cfg, err := prepare(cctx)
if err != nil {
return err
}
cctx.Context = context.WithValue(cctx.Context, contextKeyMarketConfig, cfg)
return cmd.FetchAndLoadBundles(cctx.Context, cfg.Node)
return prepare(cctx, config.SignerTypeGateway)
}
14 changes: 1 addition & 13 deletions cmd/venus-market/solo-run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"

"github.com/filecoin-project/venus-market/v2/cmd"

"github.com/filecoin-project/venus-auth/cmd/jwtclient"
"github.com/filecoin-project/venus-market/v2/api/clients"
"github.com/filecoin-project/venus-market/v2/api/impl"
Expand Down Expand Up @@ -119,15 +117,5 @@ func soloDaemon(cctx *cli.Context) error {
}

var beforeSoloRunCmd = func(cctx *cli.Context) error {
if !cctx.IsSet(HidenSignerTypeFlag.Name) {
if err := cctx.Set(HidenSignerTypeFlag.Name, config.SignerTypeWallet); err != nil {
return fmt.Errorf("set %s with wallet failed %v", HidenSignerTypeFlag.Name, err)
}
}
cfg, err := prepare(cctx)
if err != nil {
return err
}
cctx.Context = context.WithValue(cctx.Context, contextKeyMarketConfig, cfg)
return cmd.FetchAndLoadBundles(cctx.Context, cfg.Node)
return prepare(cctx, config.SignerTypeWallet)
}

0 comments on commit 6f45ae4

Please sign in to comment.