qbtrade/pkg/cmd/balances.go

68 lines
1.3 KiB
Go
Raw Normal View History

2024-06-27 14:42:38 +00:00
package cmd
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"git.qtrade.icu/lychiyu/qbtrade/pkg/qbtrade"
)
func init() {
balancesCmd.Flags().String("session", "", "the exchange session name for querying balances")
RootCmd.AddCommand(balancesCmd)
}
// go run ./cmd/qbtrade balances --session=binance
var balancesCmd = &cobra.Command{
Use: "balances [--session SESSION]",
Short: "Show user account balances",
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"session",
}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
environ := qbtrade.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
if len(sessionName) > 0 {
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
b, err := session.Exchange.QueryAccountBalances(ctx)
if err != nil {
return err
}
b.Print()
} else {
for _, session := range environ.Sessions() {
b, err := session.Exchange.QueryAccountBalances(ctx)
if err != nil {
return err
}
log.Infof("SESSION %s", session.Name)
b.Print()
}
}
return nil
},
}