move bootstrap functions

This commit is contained in:
c9s 2022-09-06 16:50:45 +08:00
parent 3f27facd83
commit 8f363677bc
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
6 changed files with 39 additions and 33 deletions

View File

@ -15,7 +15,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/cmd"
"github.com/c9s/bbgo/pkg/server"
)
@ -92,7 +91,7 @@ func main() {
// we could initialize the environment from the settings
if setup == nil {
if err := cmd.BootstrapEnvironment(ctx, environ, userConfig); err != nil {
if err := bbgo.BootstrapEnvironment(ctx, environ, userConfig); err != nil {
log.WithError(err).Error("failed to bootstrap environment")
return
}

View File

@ -16,7 +16,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/cmd"
"github.com/c9s/bbgo/pkg/server"
)
@ -93,7 +92,7 @@ func main() {
// we could initialize the environment from the settings
if setup == nil {
if err := cmd.BootstrapEnvironment(ctx, environ, userConfig); err != nil {
if err := bbgo.BootstrapEnvironment(ctx, environ, userConfig); err != nil {
log.WithError(err).Error("failed to bootstrap environment")
return
}

34
pkg/bbgo/bootstrap.go Normal file
View File

@ -0,0 +1,34 @@
package bbgo
import (
"context"
"github.com/pkg/errors"
)
func BootstrapEnvironment(ctx context.Context, environ *Environment, userConfig *Config) error {
if err := environ.ConfigureDatabase(ctx); err != nil {
return err
}
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return errors.Wrap(err, "exchange session configure error")
}
if userConfig.Persistence != nil {
if err := environ.ConfigurePersistence(userConfig.Persistence); err != nil {
return errors.Wrap(err, "persistence configure error")
}
}
if err := environ.ConfigureNotificationSystem(userConfig); err != nil {
return errors.Wrap(err, "notification configure error")
}
return nil
}
func BootstrapBacktestEnvironment(ctx context.Context, environ *Environment) error {
return environ.ConfigureDatabase(ctx)
}

View File

@ -155,7 +155,7 @@ var BacktestCmd = &cobra.Command{
log.Infof("starting backtest with startTime %s", startTime.Format(time.RFC3339))
environ := bbgo.NewEnvironment()
if err := BootstrapBacktestEnvironment(ctx, environ); err != nil {
if err := bbgo.BootstrapBacktestEnvironment(ctx, environ); err != nil {
return err
}

View File

@ -80,32 +80,6 @@ func runSetup(baseCtx context.Context, userConfig *bbgo.Config, enableApiServer
return nil
}
func BootstrapBacktestEnvironment(ctx context.Context, environ *bbgo.Environment) error {
return environ.ConfigureDatabase(ctx)
}
func BootstrapEnvironment(ctx context.Context, environ *bbgo.Environment, userConfig *bbgo.Config) error {
if err := environ.ConfigureDatabase(ctx); err != nil {
return err
}
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return errors.Wrap(err, "exchange session configure error")
}
if userConfig.Persistence != nil {
if err := environ.ConfigurePersistence(userConfig.Persistence); err != nil {
return errors.Wrap(err, "persistence configure error")
}
}
if err := environ.ConfigureNotificationSystem(userConfig); err != nil {
return errors.Wrap(err, "notification configure error")
}
return nil
}
func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Config) error {
noSync, err := cmd.Flags().GetBool("no-sync")
if err != nil {
@ -148,7 +122,7 @@ func runConfig(basectx context.Context, cmd *cobra.Command, userConfig *bbgo.Con
defer cancelTrading()
environ := bbgo.NewEnvironment()
if err := BootstrapEnvironment(ctx, environ, userConfig); err != nil {
if err := bbgo.BootstrapEnvironment(ctx, environ, userConfig); err != nil {
return err
}

View File

@ -59,7 +59,7 @@ var TransferHistoryCmd = &cobra.Command{
}
environ := bbgo.NewEnvironment()
if err := BootstrapEnvironment(ctx, environ, userConfig); err != nil {
if err := bbgo.BootstrapEnvironment(ctx, environ, userConfig); err != nil {
return err
}