bbgo_origin/pkg/cmd/root.go

152 lines
4.4 KiB
Go
Raw Normal View History

2020-09-19 00:10:48 +00:00
package cmd
import (
"os"
"path"
"strings"
"time"
"github.com/joho/godotenv"
2020-10-23 06:28:07 +00:00
"github.com/lestrrat-go/file-rotatelogs"
"github.com/pkg/errors"
2020-09-19 00:10:48 +00:00
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
2020-09-19 00:10:48 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2020-10-23 06:28:07 +00:00
"github.com/x-cray/logrus-prefixed-formatter"
_ "github.com/go-sql-driver/mysql"
2020-09-19 00:10:48 +00:00
)
var RootCmd = &cobra.Command{
Use: "bbgo",
2021-02-03 09:41:48 +00:00
Short: "bbgo is a crypto trading bot",
2020-09-19 00:10:48 +00:00
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
disableDotEnv, err := cmd.Flags().GetBool("no-dotenv")
if err != nil {
return err
}
if !disableDotEnv {
dotenvFile, err := cmd.Flags().GetString("dotenv")
if err != nil {
return err
}
if _, err := os.Stat(dotenvFile); err == nil {
if err := godotenv.Load(dotenvFile); err != nil {
return errors.Wrap(err, "error loading dotenv file")
}
}
}
return nil
},
2020-09-19 00:10:48 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
func init() {
RootCmd.PersistentFlags().Bool("debug", false, "debug flag")
RootCmd.PersistentFlags().String("config", "bbgo.yaml", "config file")
2020-09-19 00:10:48 +00:00
RootCmd.PersistentFlags().Bool("no-dotenv", false, "disable built-in dotenv")
RootCmd.PersistentFlags().String("dotenv", ".env.local", "the dotenv file you want to load")
2020-09-19 00:10:48 +00:00
// A flag can be 'persistent' meaning that this flag will be available to
// the command it's assigned to as well as every command under that command.
// For global flags, assign a flag as a persistent flag on the root.
RootCmd.PersistentFlags().String("slack-token", "", "slack token")
RootCmd.PersistentFlags().String("slack-channel", "dev-bbgo", "slack trading channel")
2020-09-19 00:10:48 +00:00
RootCmd.PersistentFlags().String("slack-error-channel", "bbgo-error", "slack error channel")
2020-10-15 14:36:22 +00:00
2020-12-05 06:25:19 +00:00
RootCmd.PersistentFlags().String("telegram-bot-token", "", "telegram bot token from bot father")
2021-03-10 04:31:26 +00:00
RootCmd.PersistentFlags().String("telegram-bot-auth-token", "", "telegram auth token")
2020-12-05 06:25:19 +00:00
2020-10-15 14:36:22 +00:00
RootCmd.PersistentFlags().String("binance-api-key", "", "binance api key")
RootCmd.PersistentFlags().String("binance-api-secret", "", "binance api secret")
RootCmd.PersistentFlags().String("max-api-key", "", "max api key")
RootCmd.PersistentFlags().String("max-api-secret", "", "max api secret")
2021-02-27 09:23:59 +00:00
RootCmd.PersistentFlags().String("ftx-api-key", "", "ftx api key")
RootCmd.PersistentFlags().String("ftx-api-secret", "", "ftx api secret")
RootCmd.PersistentFlags().String("ftx-subaccount-name", "", "subaccount name. Specify it if the credential is for subaccount.")
2020-09-19 00:10:48 +00:00
}
func Execute() {
2020-09-19 00:10:48 +00:00
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// Enable environment variable binding, the env vars are not overloaded yet.
viper.AutomaticEnv()
// setup the config paths for looking up the config file
2020-10-26 05:56:48 +00:00
/*
2020-12-05 06:25:19 +00:00
viper.AddConfigPath("config")
viper.AddConfigPath("$HOME/.bbgo")
viper.AddConfigPath("/etc/bbgo")
// set the config file name and format for loading the config file.
viper.SetConfigName("bbgo")
viper.SetConfigType("yaml")
err := viper.ReadInConfig()
if err != nil {
log.WithError(err).Fatal("failed to load config file")
}
2020-10-26 05:56:48 +00:00
*/
2020-09-19 00:10:48 +00:00
// Once the flags are defined, we can bind config keys with flags.
if err := viper.BindPFlags(RootCmd.PersistentFlags()); err != nil {
log.WithError(err).Errorf("failed to bind persistent flags. please check the flag settings.")
2020-09-19 00:10:48 +00:00
}
if err := viper.BindPFlags(RootCmd.Flags()); err != nil {
log.WithError(err).Errorf("failed to bind local flags. please check the flag settings.")
2020-09-19 00:10:48 +00:00
}
log.SetFormatter(&prefixed.TextFormatter{})
2020-09-19 00:10:48 +00:00
logger := log.StandardLogger()
2020-09-19 00:10:48 +00:00
if viper.GetBool("debug") {
logger.SetLevel(log.DebugLevel)
2020-09-19 00:10:48 +00:00
}
environment := os.Getenv("BBGO_ENV")
switch environment {
case "production", "prod":
writer, err := rotatelogs.New(
path.Join("log", "access_log.%Y%m%d"),
rotatelogs.WithLinkName("access_log"),
// rotatelogs.WithMaxAge(24 * time.Hour),
rotatelogs.WithRotationTime(time.Duration(24)*time.Hour),
)
if err != nil {
log.Panic(err)
2020-09-19 00:10:48 +00:00
}
logger.AddHook(
lfshook.NewHook(
lfshook.WriterMap{
log.DebugLevel: writer,
log.InfoLevel: writer,
log.WarnLevel: writer,
log.ErrorLevel: writer,
log.FatalLevel: writer,
2020-09-19 00:10:48 +00:00
},
&log.JSONFormatter{},
2020-09-19 00:10:48 +00:00
),
)
}
if err := RootCmd.Execute(); err != nil {
log.WithError(err).Fatalf("cannot execute command")
2020-09-19 00:10:48 +00:00
}
}