fix: mark flags as required during PreRunE

This commit is contained in:
zenix 2022-03-03 17:34:32 +09:00
parent 9c36e59f84
commit a33b748563
10 changed files with 82 additions and 134 deletions

View File

@ -3,7 +3,6 @@ package cmd
import (
"context"
"fmt"
"os"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
@ -25,18 +24,12 @@ var accountCmd = &cobra.Command{
Use: "account [--session=[exchange_name]]",
Short: "show user account details (ex: balance)",
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"config",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
if len(configFile) == 0 {
return errors.New("--config option is required")
}
showTotal, err := cmd.Flags().GetBool("total")
if err != nil {
return err
@ -47,21 +40,6 @@ var accountCmd = &cobra.Command{
return err
}
var userConfig *bbgo.Config
if _, err := os.Stat(configFile); err == nil {
// load successfully
userConfig, err = bbgo.Load(configFile, false)
if err != nil {
return err
}
} else if os.IsNotExist(err) {
// config file doesn't exist
userConfig = &bbgo.Config{}
} else {
// other error
return err
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureDatabase(ctx); err != nil {
return err

View File

@ -20,6 +20,10 @@ var balancesCmd = &cobra.Command{
Use: "balances [--session SESSION]",
Short: "Show user account balances",
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"config",
"session",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
@ -28,10 +32,6 @@ var balancesCmd = &cobra.Command{
return err
}
if userConfig == nil {
return fmt.Errorf("user config is not loaded")
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -35,7 +34,7 @@ var cancelOrderCmd = &cobra.Command{
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{"config"}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@ -70,10 +69,6 @@ var cancelOrderCmd = &cobra.Command{
return err
}
if userConfig == nil {
return errors.New("config file is required")
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err

View File

@ -8,7 +8,6 @@ import (
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@ -17,13 +16,15 @@ import (
var klineCmd = &cobra.Command{
Use: "kline",
Short: "connect to the kline market data streaming service of an exchange",
PreRunE: cobraInitRequired([]string{
"config",
"session",
"symbol",
"interval",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if userConfig == nil {
return errors.New("--config option or config file is missing")
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err

View File

@ -6,7 +6,6 @@ import (
"syscall"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -19,6 +18,11 @@ import (
var orderbookCmd = &cobra.Command{
Use: "orderbook --session=[exchange_name] --symbol=[pair_name]",
Short: "connect to the order book market data streaming service of an exchange",
PreRunE: cobraInitRequired([]string{
"config",
"session",
"symbol",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
@ -41,10 +45,6 @@ var orderbookCmd = &cobra.Command{
return err
}
if userConfig == nil {
return errors.New("--config option or config file is missing")
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
@ -114,13 +114,13 @@ var orderbookCmd = &cobra.Command{
var orderUpdateCmd = &cobra.Command{
Use: "orderupdate",
Short: "Listen to order update events",
PreRunE: cobraInitRequired([]string{
"config",
"session",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if userConfig == nil {
return errors.New("--config option or config file is missing")
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err

View File

@ -23,13 +23,14 @@ var getOrderCmd = &cobra.Command{
Use: "get-order --session SESSION --order-id ORDER_ID",
Short: "Get order status",
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"config",
"order-id",
"symbol",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if userConfig == nil {
return errors.New("config file is required")
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
@ -76,13 +77,14 @@ var listOrdersCmd = &cobra.Command{
// default is open which means we query open orders if you haven't provided args.
ValidArgs: []string{"", "open", "closed"},
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"config",
"session",
"symbol",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if userConfig == nil {
return errors.New("config file is required")
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
@ -146,13 +148,16 @@ var executeOrderCmd = &cobra.Command{
Use: "execute-order --session SESSION --symbol SYMBOL --side SIDE --target-quantity TOTAL_QUANTITY --slice-quantity SLICE_QUANTITY",
Short: "execute buy/sell on the balance/position you have on specific symbol",
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"config",
"symbol",
"side",
"target-quantity",
"slice-quantity",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if userConfig == nil {
return errors.New("config file is required")
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
@ -294,13 +299,17 @@ var submitOrderCmd = &cobra.Command{
Use: "submit-order --session SESSION --symbol SYMBOL --side SIDE --quantity QUANTITY [--price PRICE]",
Short: "place order to the exchange",
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"config",
"session",
"symbol",
"side",
"price",
"quantity",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if userConfig == nil {
return errors.New("config file is required")
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err

View File

@ -40,6 +40,9 @@ var RunCmd = &cobra.Command{
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"config",
}),
RunE: run,
}
@ -228,8 +231,6 @@ func run(cmd *cobra.Command, args []string) error {
return err
}
var userConfig = &bbgo.Config{}
if !setup {
// if it's not setup, then the config file option is required.
if len(configFile) == 0 {
@ -240,10 +241,6 @@ func run(cmd *cobra.Command, args []string) error {
return err
}
userConfig, err = bbgo.Load(configFile, false)
if err != nil {
return err
}
}
ctx, cancel := context.WithCancel(context.Background())
@ -259,6 +256,7 @@ func run(cmd *cobra.Command, args []string) error {
return runSetup(ctx, userConfig, true)
}
// default setting is false, here load as true
userConfig, err = bbgo.Load(configFile, true)
if err != nil {
return err

View File

@ -3,11 +3,9 @@ package cmd
import (
"context"
"fmt"
"os"
"syscall"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -21,35 +19,14 @@ var tradesCmd = &cobra.Command{
Use: "trades --session=[exchange_name] --symbol=[pair_name]",
Short: "Query trading history",
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"config",
"session",
"symbol",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
if len(configFile) == 0 {
return errors.New("--config option is required")
}
// if config file exists, use the config loaded from the config file.
// otherwise, use a empty config object
var userConfig *bbgo.Config
if _, err := os.Stat(configFile); err == nil {
// load successfully
userConfig, err = bbgo.Load(configFile, false)
if err != nil {
return err
}
} else if os.IsNotExist(err) {
// config file doesn't exist
userConfig = &bbgo.Config{}
} else {
// other error
return err
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
@ -120,35 +97,13 @@ var tradesCmd = &cobra.Command{
var tradeUpdateCmd = &cobra.Command{
Use: "tradeupdate --session=[exchange_name]",
Short: "Listen to trade update events",
PreRunE: cobraInitRequired([]string{
"config",
"session",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
if len(configFile) == 0 {
return errors.New("--config option is required")
}
// if config file exists, use the config loaded from the config file.
// otherwise, use a empty config object
var userConfig *bbgo.Config
if _, err := os.Stat(configFile); err == nil {
// load successfully
userConfig, err = bbgo.Load(configFile, false)
if err != nil {
return err
}
} else if os.IsNotExist(err) {
// config file doesn't exist
userConfig = &bbgo.Config{}
} else {
// other error
return err
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {

View File

@ -6,7 +6,6 @@ import (
"syscall"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -19,13 +18,13 @@ import (
var userDataStreamCmd = &cobra.Command{
Use: "userdatastream",
Short: "Listen to session events (orderUpdate, tradeUpdate, balanceUpdate, balanceSnapshot)",
PreRunE: cobraInitRequired([]string{
"config",
"session",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if userConfig == nil {
return errors.New("--config option or config file is missing")
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err

View File

@ -8,8 +8,21 @@ import (
"github.com/c9s/bbgo/pkg/exchange/ftx"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func cobraInitRequired(required []string) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
for _, key := range required {
if err := cmd.MarkFlagRequired(key); err != nil {
log.WithError(err).Errorf("cannot mark --%s option required", key)
}
}
return nil
}
}
func inQuoteAsset(balances types.BalanceMap, market types.Market, price fixedpoint.Value) fixedpoint.Value {
quote := balances[market.QuoteCurrency]
base := balances[market.BaseCurrency]