2020-10-21 07:57:14 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-24 07:38:13 +00:00
|
|
|
"bytes"
|
2020-10-21 07:57:14 +00:00
|
|
|
"context"
|
2020-10-26 03:41:21 +00:00
|
|
|
"fmt"
|
2020-12-11 09:07:19 +00:00
|
|
|
"image/png"
|
2020-10-24 07:38:13 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2020-10-24 08:28:42 +00:00
|
|
|
"runtime"
|
2020-12-11 06:40:04 +00:00
|
|
|
"strings"
|
2020-10-21 07:57:14 +00:00
|
|
|
"syscall"
|
2020-10-24 07:38:13 +00:00
|
|
|
"text/template"
|
2020-11-12 06:50:08 +00:00
|
|
|
"time"
|
2020-10-21 07:57:14 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-12-11 09:07:19 +00:00
|
|
|
"github.com/pquerna/otp"
|
2020-10-24 07:43:55 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-21 07:57:14 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-10-24 07:38:13 +00:00
|
|
|
flag "github.com/spf13/pflag"
|
2020-10-21 07:57:14 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-12-08 06:20:49 +00:00
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
2020-10-21 07:57:14 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2020-10-21 07:58:58 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
2020-10-21 09:10:47 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/notifier/slacknotifier"
|
2020-12-05 06:20:27 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/notifier/telegramnotifier"
|
2020-12-11 06:40:04 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
2020-10-21 07:57:14 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/slack/slacklog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-10-24 07:38:13 +00:00
|
|
|
RunCmd.Flags().Bool("no-compile", false, "do not compile wrapper binary")
|
2020-10-24 08:28:42 +00:00
|
|
|
RunCmd.Flags().String("os", runtime.GOOS, "GOOS")
|
|
|
|
RunCmd.Flags().String("arch", runtime.GOARCH, "GOARCH")
|
|
|
|
|
2020-12-11 09:12:16 +00:00
|
|
|
RunCmd.Flags().String("totp-key-url", "", "time-based one-time password key URL, if defined, it will be used for restoring the otp key")
|
2020-12-11 06:40:04 +00:00
|
|
|
RunCmd.Flags().String("totp-issuer", "", "")
|
|
|
|
RunCmd.Flags().String("totp-account-name", "", "")
|
|
|
|
|
2020-10-23 06:28:07 +00:00
|
|
|
RunCmd.Flags().String("since", "", "pnl since time")
|
|
|
|
RootCmd.AddCommand(RunCmd)
|
2020-10-21 07:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 05:27:07 +00:00
|
|
|
var wrapperTemplate = template.Must(template.New("main").Parse(`package main
|
2020-10-24 07:38:13 +00:00
|
|
|
// DO NOT MODIFY THIS FILE. THIS FILE IS GENERATED FOR IMPORTING STRATEGIES
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/cmd"
|
|
|
|
|
|
|
|
{{- range .Imports }}
|
|
|
|
_ "{{ . }}"
|
|
|
|
{{- end }}
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd.Execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
`))
|
|
|
|
|
2020-10-26 13:45:02 +00:00
|
|
|
func compileRunFile(filepath string, config *bbgo.Config) error {
|
2020-10-24 07:38:13 +00:00
|
|
|
var buf = bytes.NewBuffer(nil)
|
2020-10-26 05:27:07 +00:00
|
|
|
if err := wrapperTemplate.Execute(buf, config); err != nil {
|
2020-10-23 06:49:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
return ioutil.WriteFile(filepath, buf.Bytes(), 0644)
|
|
|
|
}
|
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
func runConfig(basectx context.Context, userConfig *bbgo.Config) error {
|
|
|
|
ctx, cancelTrading := context.WithCancel(basectx)
|
2020-12-21 07:47:20 +00:00
|
|
|
defer cancelTrading()
|
2020-11-17 00:19:22 +00:00
|
|
|
|
2020-10-27 01:38:29 +00:00
|
|
|
environ := bbgo.NewEnvironment()
|
2020-10-23 06:49:54 +00:00
|
|
|
|
2020-11-02 14:22:17 +00:00
|
|
|
if viper.IsSet("mysql-url") {
|
|
|
|
db, err := cmdutil.ConnectMySQL()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-29 08:00:03 +00:00
|
|
|
environ.SetDB(db)
|
2020-10-23 06:49:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 08:00:03 +00:00
|
|
|
if err := environ.AddExchangesFromConfig(userConfig); err != nil {
|
|
|
|
return err
|
2020-10-26 09:00:17 +00:00
|
|
|
}
|
2020-10-30 20:36:45 +00:00
|
|
|
|
2020-12-08 07:03:52 +00:00
|
|
|
if userConfig.Persistence != nil {
|
|
|
|
if err := environ.ConfigurePersistence(userConfig.Persistence); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
notification := bbgo.Notifiability{
|
2020-10-27 01:38:29 +00:00
|
|
|
SymbolChannelRouter: bbgo.NewPatternChannelRouter(nil),
|
|
|
|
SessionChannelRouter: bbgo.NewPatternChannelRouter(nil),
|
|
|
|
ObjectChannelRouter: bbgo.NewObjectChannelRouter(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// for slack
|
|
|
|
slackToken := viper.GetString("slack-token")
|
2020-10-27 12:41:08 +00:00
|
|
|
if len(slackToken) > 0 && userConfig.Notifications != nil {
|
2020-10-27 01:38:29 +00:00
|
|
|
if conf := userConfig.Notifications.Slack; conf != nil {
|
|
|
|
if conf.ErrorChannel != "" {
|
|
|
|
log.Infof("found slack configured, setting up log hook...")
|
|
|
|
log.AddHook(slacklog.NewLogHook(slackToken, conf.ErrorChannel))
|
|
|
|
}
|
|
|
|
|
2020-10-28 09:24:47 +00:00
|
|
|
log.Infof("adding slack notifier with default channel: %s", conf.DefaultChannel)
|
2020-10-27 01:38:29 +00:00
|
|
|
var notifier = slacknotifier.New(slackToken, conf.DefaultChannel)
|
2020-10-30 21:21:17 +00:00
|
|
|
notification.AddNotifier(notifier)
|
2020-10-27 01:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 06:20:27 +00:00
|
|
|
// for telegram
|
|
|
|
telegramBotToken := viper.GetString("telegram-bot-token")
|
2020-12-11 06:40:04 +00:00
|
|
|
telegramBotAuthToken := viper.GetString("telegram-bot-auth-token")
|
2020-12-11 09:07:19 +00:00
|
|
|
if len(telegramBotToken) > 0 {
|
|
|
|
log.Infof("initializing telegram bot...")
|
2020-12-11 06:40:04 +00:00
|
|
|
|
2020-12-08 06:20:49 +00:00
|
|
|
bot, err := tb.NewBot(tb.Settings{
|
|
|
|
// You can also set custom API URL.
|
|
|
|
// If field is empty it equals to "https://api.telegram.org".
|
|
|
|
// URL: "http://195.129.111.17:8012",
|
|
|
|
Token: telegramBotToken,
|
|
|
|
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-05 06:20:27 +00:00
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
var persistence bbgo.PersistenceService = bbgo.NewMemoryService()
|
|
|
|
var sessionStore = persistence.NewStore("bbgo", "telegram")
|
|
|
|
|
|
|
|
tt := strings.Split(bot.Token, ":")
|
|
|
|
telegramID := tt[0]
|
|
|
|
|
2020-12-11 06:40:04 +00:00
|
|
|
if environ.PersistenceServiceFacade != nil {
|
|
|
|
if environ.PersistenceServiceFacade.Redis != nil {
|
2020-12-11 09:07:19 +00:00
|
|
|
persistence = environ.PersistenceServiceFacade.Redis
|
|
|
|
sessionStore = persistence.NewStore("bbgo", "telegram", telegramID)
|
2020-12-11 06:40:04 +00:00
|
|
|
}
|
2020-12-08 07:09:17 +00:00
|
|
|
}
|
2020-12-05 06:20:27 +00:00
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
interaction := telegramnotifier.NewInteraction(bot, sessionStore)
|
|
|
|
|
|
|
|
if len(telegramBotAuthToken) > 0 {
|
|
|
|
log.Infof("telegram bot auth token is set, using fixed token for authorization...")
|
|
|
|
interaction.SetAuthToken(telegramBotAuthToken)
|
|
|
|
log.Infof("send the following command to the bbgo bot you created to enable the notification")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof(" /auth %s", telegramBotAuthToken)
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("")
|
|
|
|
}
|
|
|
|
|
|
|
|
var session telegramnotifier.Session
|
|
|
|
if err := sessionStore.Load(&session); err != nil || session.Owner == nil {
|
|
|
|
log.Warnf("session not found, generating new one-time password key for new session...")
|
|
|
|
|
|
|
|
key, err := service.NewDefaultTotpKey()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to setup totp (time-based one time password) key")
|
|
|
|
}
|
|
|
|
|
|
|
|
displayOTPKey(key)
|
|
|
|
|
|
|
|
qrcodeImagePath := fmt.Sprintf("otp-%s.png", telegramID)
|
|
|
|
|
|
|
|
err = writeOTPKeyAsQRCodePNG(key, qrcodeImagePath)
|
|
|
|
log.Infof("To scan your OTP QR code, please run the following command:")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof(" open %s", qrcodeImagePath)
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("send the auth command with the generated one-time password to the bbgo bot you created to enable the notification")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof(" /auth {code}")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("")
|
|
|
|
|
|
|
|
session = telegramnotifier.NewSession(key)
|
|
|
|
if err := sessionStore.Save(&session); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to save session")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go interaction.Start(session)
|
2020-12-11 06:40:04 +00:00
|
|
|
|
|
|
|
var notifier = telegramnotifier.New(interaction)
|
2020-12-05 06:20:27 +00:00
|
|
|
notification.AddNotifier(notifier)
|
|
|
|
}
|
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
environ.Notifiability = notification
|
2020-10-27 01:38:29 +00:00
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
if userConfig.Notifications != nil {
|
2020-12-07 03:43:17 +00:00
|
|
|
if err := environ.ConfigureNotification(userConfig.Notifications); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-27 01:38:29 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:21:17 +00:00
|
|
|
trader := bbgo.NewTrader(environ)
|
|
|
|
|
2020-10-27 01:58:21 +00:00
|
|
|
if userConfig.RiskControls != nil {
|
|
|
|
trader.SetRiskControls(userConfig.RiskControls)
|
|
|
|
}
|
|
|
|
|
2020-10-26 07:31:13 +00:00
|
|
|
for _, entry := range userConfig.ExchangeStrategies {
|
2020-10-23 06:49:54 +00:00
|
|
|
for _, mount := range entry.Mounts {
|
2020-10-24 07:43:55 +00:00
|
|
|
log.Infof("attaching strategy %T on %s...", entry.Strategy, mount)
|
2020-10-23 06:49:54 +00:00
|
|
|
trader.AttachStrategyOn(mount, entry.Strategy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 07:31:13 +00:00
|
|
|
for _, strategy := range userConfig.CrossExchangeStrategies {
|
2020-11-17 00:19:22 +00:00
|
|
|
log.Infof("attaching cross exchange strategy %T", strategy)
|
2020-10-23 06:49:54 +00:00
|
|
|
trader.AttachCrossExchangeStrategy(strategy)
|
|
|
|
}
|
|
|
|
|
2020-10-26 07:31:13 +00:00
|
|
|
for _, report := range userConfig.PnLReporters {
|
2020-10-23 06:49:54 +00:00
|
|
|
if len(report.AverageCostBySymbols) > 0 {
|
2020-10-26 05:48:59 +00:00
|
|
|
|
2020-10-26 07:31:13 +00:00
|
|
|
log.Infof("setting up average cost pnl reporter on symbols: %v", report.AverageCostBySymbols)
|
2020-10-27 05:54:39 +00:00
|
|
|
trader.ReportPnL().
|
2020-10-23 06:49:54 +00:00
|
|
|
AverageCostBySymbols(report.AverageCostBySymbols...).
|
|
|
|
Of(report.Of...).
|
|
|
|
When(report.When...)
|
2020-10-26 05:48:59 +00:00
|
|
|
|
2020-10-23 06:49:54 +00:00
|
|
|
} else {
|
2020-11-09 08:34:35 +00:00
|
|
|
return fmt.Errorf("unsupported PnL reporter: %+v", report)
|
2020-10-23 06:49:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 06:50:08 +00:00
|
|
|
if err := trader.Run(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
cancelTrading()
|
|
|
|
|
|
|
|
shutdownCtx, cancelShutdown := context.WithDeadline(ctx, time.Now().Add(30*time.Second))
|
2020-11-12 06:50:08 +00:00
|
|
|
|
2020-11-12 06:59:47 +00:00
|
|
|
log.Infof("shutting down...")
|
2020-11-12 06:50:08 +00:00
|
|
|
trader.Graceful.Shutdown(shutdownCtx)
|
2020-11-17 00:19:22 +00:00
|
|
|
cancelShutdown()
|
2020-11-12 06:50:08 +00:00
|
|
|
return nil
|
2020-10-23 06:49:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 06:28:07 +00:00
|
|
|
var RunCmd = &cobra.Command{
|
2020-10-21 07:57:14 +00:00
|
|
|
Use: "run",
|
2020-10-23 06:49:54 +00:00
|
|
|
Short: "run strategies from config file",
|
2020-10-21 07:57:14 +00:00
|
|
|
|
|
|
|
// SilenceUsage is an option to silence usage when an error occurs.
|
|
|
|
SilenceUsage: true,
|
|
|
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
configFile, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configFile) == 0 {
|
2020-10-23 06:49:54 +00:00
|
|
|
return errors.New("--config option is required")
|
2020-10-21 07:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
noCompile, err := cmd.Flags().GetBool("no-compile")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-21 07:57:14 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2020-12-29 08:00:03 +00:00
|
|
|
userConfig, err := bbgo.Load(configFile, false)
|
2020-10-24 07:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-21 07:57:14 +00:00
|
|
|
|
2020-12-09 08:13:20 +00:00
|
|
|
shouldCompile := len(userConfig.Imports) > 0
|
|
|
|
|
2020-10-24 08:28:42 +00:00
|
|
|
// if there is no custom imports, we don't have to compile
|
2020-12-09 08:13:20 +00:00
|
|
|
if noCompile || !shouldCompile {
|
2020-12-29 08:00:03 +00:00
|
|
|
userConfig, err = bbgo.Load(configFile, true)
|
2020-11-15 05:23:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("running config without wrapper binary...")
|
2020-10-24 07:38:13 +00:00
|
|
|
if err := runConfig(ctx, userConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-12 06:50:08 +00:00
|
|
|
|
2020-10-24 07:38:13 +00:00
|
|
|
return nil
|
2020-10-24 08:28:42 +00:00
|
|
|
}
|
2020-10-24 07:38:13 +00:00
|
|
|
|
2020-10-26 05:27:07 +00:00
|
|
|
var runArgs = []string{"run", "--no-compile"}
|
2020-10-24 08:28:42 +00:00
|
|
|
cmd.Flags().Visit(func(flag *flag.Flag) {
|
2020-11-17 00:19:22 +00:00
|
|
|
runArgs = append(runArgs, "--"+flag.Name, flag.Value.String())
|
2020-10-24 08:28:42 +00:00
|
|
|
})
|
2020-10-26 05:27:07 +00:00
|
|
|
runArgs = append(runArgs, args...)
|
2020-10-24 07:38:13 +00:00
|
|
|
|
2020-10-24 08:28:42 +00:00
|
|
|
goOS, err := cmd.Flags().GetString("os")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-24 07:38:13 +00:00
|
|
|
|
2020-10-24 08:28:42 +00:00
|
|
|
goArch, err := cmd.Flags().GetString("arch")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-24 07:38:13 +00:00
|
|
|
|
2020-11-23 08:36:03 +00:00
|
|
|
runCmd, err := buildAndRun(ctx, userConfig, goOS, goArch, runArgs...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sig := cmdutil.WaitForSignal(ctx, syscall.SIGTERM, syscall.SIGINT); sig != nil {
|
|
|
|
log.Infof("sending signal to the child process...")
|
|
|
|
if err := runCmd.Process.Signal(sig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runCmd.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-10-24 08:28:42 +00:00
|
|
|
},
|
|
|
|
}
|
2020-10-24 07:38:13 +00:00
|
|
|
|
2020-10-26 13:45:02 +00:00
|
|
|
func compile(buildDir string, userConfig *bbgo.Config) error {
|
2020-10-24 08:28:42 +00:00
|
|
|
if _, err := os.Stat(buildDir); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(buildDir, 0777); err != nil {
|
|
|
|
return errors.Wrapf(err, "can not create build directory: %s", buildDir)
|
2020-10-24 07:38:13 +00:00
|
|
|
}
|
2020-10-24 08:28:42 +00:00
|
|
|
}
|
2020-10-24 07:38:13 +00:00
|
|
|
|
2020-10-24 08:28:42 +00:00
|
|
|
mainFile := filepath.Join(buildDir, "main.go")
|
|
|
|
if err := compileRunFile(mainFile, userConfig); err != nil {
|
|
|
|
return errors.Wrap(err, "compile error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:45:02 +00:00
|
|
|
func build(ctx context.Context, buildDir string, userConfig *bbgo.Config, goOS, goArch string, output *string) (string, error) {
|
2020-10-24 08:28:42 +00:00
|
|
|
if err := compile(buildDir, userConfig); err != nil {
|
2020-10-26 05:27:07 +00:00
|
|
|
return "", err
|
2020-10-24 08:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2020-10-26 05:27:07 +00:00
|
|
|
return "", err
|
2020-10-24 08:28:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 05:56:48 +00:00
|
|
|
buildEnvs := []string{
|
|
|
|
"GOOS=" + goOS,
|
|
|
|
"GOARCH=" + goArch,
|
|
|
|
}
|
|
|
|
|
2020-10-24 08:28:42 +00:00
|
|
|
buildTarget := filepath.Join(cwd, buildDir)
|
2020-10-26 05:56:48 +00:00
|
|
|
|
2020-10-26 03:41:21 +00:00
|
|
|
binary := fmt.Sprintf("bbgow-%s-%s", goOS, goArch)
|
2020-10-26 05:56:48 +00:00
|
|
|
if output != nil && len(*output) > 0 {
|
2020-10-26 05:27:07 +00:00
|
|
|
binary = *output
|
|
|
|
}
|
2020-10-26 03:41:21 +00:00
|
|
|
|
2020-10-26 05:56:48 +00:00
|
|
|
log.Infof("building binary %s from %s...", binary, buildTarget)
|
2020-10-26 03:41:21 +00:00
|
|
|
buildCmd := exec.CommandContext(ctx, "go", "build", "-tags", "wrapper", "-o", binary, buildTarget)
|
2020-10-24 08:28:42 +00:00
|
|
|
buildCmd.Env = append(os.Environ(), buildEnvs...)
|
|
|
|
|
|
|
|
buildCmd.Stdout = os.Stdout
|
|
|
|
buildCmd.Stderr = os.Stderr
|
|
|
|
if err := buildCmd.Run(); err != nil {
|
2020-10-26 05:56:48 +00:00
|
|
|
return binary, err
|
2020-10-26 05:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return binary, nil
|
|
|
|
}
|
|
|
|
|
2020-11-23 08:36:03 +00:00
|
|
|
func buildAndRun(ctx context.Context, userConfig *bbgo.Config, goOS, goArch string, args ...string) (*exec.Cmd, error) {
|
2020-10-26 05:27:07 +00:00
|
|
|
buildDir := filepath.Join("build", "bbgow")
|
|
|
|
|
|
|
|
binary, err := build(ctx, buildDir, userConfig, goOS, goArch, nil)
|
|
|
|
if err != nil {
|
2020-11-23 08:36:03 +00:00
|
|
|
return nil, err
|
2020-10-26 05:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2020-11-23 08:36:03 +00:00
|
|
|
return nil, err
|
2020-10-24 08:28:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 03:41:21 +00:00
|
|
|
executePath := filepath.Join(cwd, binary)
|
2020-10-24 08:28:42 +00:00
|
|
|
|
|
|
|
log.Infof("running wrapper binary, args: %v", args)
|
2020-11-23 08:36:03 +00:00
|
|
|
runCmd := exec.Command(executePath, args...)
|
2020-10-24 08:28:42 +00:00
|
|
|
runCmd.Stdout = os.Stdout
|
|
|
|
runCmd.Stderr = os.Stderr
|
2020-11-23 08:36:03 +00:00
|
|
|
return runCmd, runCmd.Start()
|
2020-10-21 07:57:14 +00:00
|
|
|
}
|
2020-12-11 09:07:19 +00:00
|
|
|
|
|
|
|
func writeOTPKeyAsQRCodePNG(key *otp.Key, imagePath string) error {
|
|
|
|
// Convert TOTP key into a PNG
|
|
|
|
var buf bytes.Buffer
|
|
|
|
img, err := key.Image(512, 512)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := png.Encode(&buf, img); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(imagePath, buf.Bytes(), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func displayOTPKey(key *otp.Key) {
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("====================PLEASE STORE YOUR OTP KEY=======================")
|
|
|
|
log.Infof("")
|
|
|
|
log.Infof("Issuer: %s", key.Issuer())
|
|
|
|
log.Infof("AccountName: %s", key.AccountName())
|
|
|
|
log.Infof("Secret: %s", key.Secret())
|
2020-12-11 09:08:06 +00:00
|
|
|
log.Infof("Key URL: %s", key.URL())
|
2020-12-11 09:07:19 +00:00
|
|
|
log.Infof("")
|
|
|
|
log.Infof("====================================================================")
|
|
|
|
log.Infof("")
|
|
|
|
}
|