add --session option check

This commit is contained in:
c9s 2021-05-06 23:44:05 +08:00
parent 7c57246070
commit 1a81813e17
2 changed files with 19 additions and 9 deletions

View File

@ -33,6 +33,15 @@ var accountCmd = &cobra.Command{
return errors.New("--config option is required") return errors.New("--config option is required")
} }
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
if len(sessionName) == 0 {
return errors.New("--session option is required")
}
var userConfig *bbgo.Config var userConfig *bbgo.Config
if _, err := os.Stat(configFile); err == nil { if _, err := os.Stat(configFile); err == nil {
// load successfully // load successfully
@ -57,11 +66,6 @@ var accountCmd = &cobra.Command{
return err return err
} }
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
session, ok := environ.Session(sessionName) session, ok := environ.Session(sessionName)
if !ok { if !ok {
return fmt.Errorf("session %s not found", sessionName) return fmt.Errorf("session %s not found", sessionName)

View File

@ -24,6 +24,7 @@ var balancesCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background() ctx := context.Background()
configFile, err := cmd.Flags().GetString("config") configFile, err := cmd.Flags().GetString("config")
if err != nil { if err != nil {
return err return err
@ -33,6 +34,15 @@ var balancesCmd = &cobra.Command{
return errors.New("--config option is required") return errors.New("--config option is required")
} }
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
if len(sessionName) == 0 {
return errors.New("--session option is required")
}
// if config file exists, use the config loaded from the config file. // if config file exists, use the config loaded from the config file.
// otherwise, use a empty config object // otherwise, use a empty config object
var userConfig *bbgo.Config var userConfig *bbgo.Config
@ -56,10 +66,6 @@ var balancesCmd = &cobra.Command{
return err return err
} }
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
session, ok := environ.Session(sessionName) session, ok := environ.Session(sessionName)
if !ok { if !ok {