bbgo_origin/pkg/cmd/root.go

280 lines
7.6 KiB
Go
Raw Normal View History

2020-09-19 00:10:48 +00:00
package cmd
import (
2021-12-27 08:27:05 +00:00
"net/http"
2020-09-19 00:10:48 +00:00
"os"
"path"
2022-06-09 07:49:52 +00:00
"runtime/pprof"
2020-09-19 00:10:48 +00:00
"strings"
"time"
_ "time/tzdata"
2020-09-19 00:10:48 +00:00
2022-12-12 09:18:40 +00:00
"github.com/heroku/rollrus"
"github.com/joho/godotenv"
2022-12-28 09:15:30 +00:00
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/pkg/errors"
2021-12-27 08:27:05 +00:00
"github.com/prometheus/client_golang/prometheus/promhttp"
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
2021-12-27 08:27:05 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
2022-12-12 09:37:40 +00:00
"github.com/c9s/bbgo/pkg/util"
2020-09-19 00:10:48 +00:00
)
2022-06-09 07:49:52 +00:00
var cpuProfileFile *os.File
2021-05-14 03:53:07 +00:00
var userConfig *bbgo.Config
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 {
if err := cobraLoadDotenv(cmd); err != nil {
return err
}
if viper.GetBool("debug") {
log.Infof("debug mode is enabled")
log.SetLevel(log.DebugLevel)
}
2023-12-13 01:47:18 +00:00
env := bbgo.GetCurrentEnv()
2022-12-12 09:18:40 +00:00
2023-06-27 08:32:46 +00:00
logFormatter, err := cmd.Flags().GetString("log-formatter")
if err != nil {
return err
}
if len(logFormatter) == 0 {
2023-12-13 01:47:18 +00:00
formatter := bbgo.NewLogFormatterWithEnv(env)
log.SetFormatter(formatter)
} else {
2023-12-13 01:47:18 +00:00
formatter := bbgo.NewLogFormatter(bbgo.LogFormatterType(logFormatter))
log.SetFormatter(formatter)
}
2022-12-12 09:37:40 +00:00
if token := viper.GetString("rollbar-token"); token != "" {
log.Infof("found rollbar token %q, setting up rollbar hook...", util.MaskKey(token))
2022-12-12 09:18:40 +00:00
log.AddHook(rollrus.NewHook(
2022-12-12 09:37:40 +00:00
token,
2022-12-12 09:18:40 +00:00
env,
))
}
2021-12-27 08:27:05 +00:00
if viper.GetBool("metrics") {
http.Handle("/metrics", promhttp.Handler())
go func() {
port := viper.GetString("metrics-port")
log.Infof("starting metrics server at :%s", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.WithError(err).Errorf("metrics server error")
}
}()
}
enableProfileServer, err := cmd.Flags().GetBool("enable-profile-server")
if err != nil {
return err
}
if enableProfileServer {
profileServerBind, err := cmd.Flags().GetString("profile-server-bind")
if err != nil {
return err
}
go func() {
if err := http.ListenAndServe(profileServerBind, nil); err != nil {
log.WithError(err).Errorf("profile server error")
}
}()
}
2022-06-09 07:49:52 +00:00
cpuProfile, err := cmd.Flags().GetString("cpu-profile")
if err != nil {
return err
}
if cpuProfile != "" {
2022-08-06 17:13:57 +00:00
log.Infof("starting cpu profiler, recording at %s", cpuProfile)
2022-06-09 07:49:52 +00:00
cpuProfileFile, err = os.Create(cpuProfile)
if err != nil {
2022-08-06 17:13:57 +00:00
return errors.Wrap(err, "can not create file for CPU profile")
2022-06-09 07:49:52 +00:00
}
if err := pprof.StartCPUProfile(cpuProfileFile); err != nil {
2022-08-06 17:13:57 +00:00
return errors.Wrap(err, "can not start CPU profile")
2022-06-09 07:49:52 +00:00
}
}
return cobraLoadConfig(cmd)
},
2022-06-09 07:49:52 +00:00
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
pprof.StopCPUProfile()
if cpuProfileFile != nil {
return cpuProfileFile.Close() // error handling omitted for example
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
func cobraLoadDotenv(cmd *cobra.Command) error {
disableDotEnv, err := cmd.Flags().GetBool("no-dotenv")
if err != nil {
return err
}
if !disableDotEnv {
dotenvFile, err := cmd.Flags().GetString("dotenv")
2021-05-14 03:53:07 +00:00
if err != nil {
return err
2021-05-14 03:53:07 +00:00
}
if _, err := os.Stat(dotenvFile); err == nil {
if err := godotenv.Load(dotenvFile); err != nil {
return errors.Wrap(err, "error loading dotenv file")
2021-05-14 03:53:07 +00:00
}
}
}
return nil
}
2021-05-14 03:53:07 +00:00
func cobraLoadConfig(cmd *cobra.Command) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return errors.Wrapf(err, "failed to get the config flag")
}
// load config file nicely
if len(configFile) > 0 {
// if config file exists, use the config loaded from the config file.
// otherwise, use an empty config object
if _, err := os.Stat(configFile); err == nil {
// load successfully
userConfig, err = bbgo.Load(configFile, false)
if err != nil {
return errors.Wrapf(err, "can not load config file: %s", configFile)
}
} else if os.IsNotExist(err) {
// config file doesn't exist, we should use the empty config
userConfig = &bbgo.Config{}
} else {
// other error
return errors.Wrapf(err, "config file load error: %s", configFile)
}
}
return nil
2020-09-19 00:10:48 +00:00
}
func init() {
RootCmd.PersistentFlags().Bool("debug", false, "debug mode")
2021-12-27 08:27:05 +00:00
RootCmd.PersistentFlags().Bool("metrics", false, "enable prometheus metrics")
RootCmd.PersistentFlags().String("metrics-port", "9090", "prometheus http server port")
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")
2021-12-27 08:27:05 +00:00
RootCmd.PersistentFlags().String("config", "bbgo.yaml", "config file")
RootCmd.PersistentFlags().String("log-formatter", "", "configure log formatter")
2022-12-12 09:18:40 +00:00
RootCmd.PersistentFlags().String("rollbar-token", "", "rollbar token")
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
2022-06-09 07:49:52 +00:00
RootCmd.PersistentFlags().String("cpu-profile", "", "cpu profile")
RootCmd.PersistentFlags().Bool("enable-profile-server", false, "enable profile server binding")
RootCmd.PersistentFlags().String("profile-server-bind", "localhost:6060", "profile server binding")
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.")
2021-05-14 03:53:07 +00:00
return
2020-09-19 00:10:48 +00:00
}
environment := os.Getenv("BBGO_ENV")
2022-12-28 09:15:30 +00:00
logDir := "log"
2020-09-19 00:10:48 +00:00
switch environment {
case "production", "prod":
2022-12-28 09:15:30 +00:00
if err := os.MkdirAll(logDir, 0777); err != nil {
log.Panic(err)
}
2020-09-19 00:10:48 +00:00
writer, err := rotatelogs.New(
2022-12-28 09:15:30 +00:00
path.Join(logDir, "access_log.%Y%m%d"),
2020-09-19 00:10:48 +00:00
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
}
log.AddHook(
2020-09-19 00:10:48 +00:00
lfshook.NewHook(
lfshook.WriterMap{
log.DebugLevel: writer,
log.InfoLevel: writer,
log.WarnLevel: writer,
log.ErrorLevel: writer,
log.FatalLevel: writer,
}, &log.JSONFormatter{}),
2020-09-19 00:10:48 +00:00
)
2020-09-19 00:10:48 +00:00
}
}
2020-09-19 00:10:48 +00:00
func Execute() {
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
}
}