mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
fix env var prefix by using os.Getenv directly
This commit is contained in:
parent
e6b0a0c595
commit
d8d1249293
|
@ -71,7 +71,7 @@ export default function AddExchangeSessionForm({onBack, onAdded}) {
|
|||
key: apiKey,
|
||||
secret: apiSecret,
|
||||
margin: isMargin,
|
||||
envVarPrefix: exchangeType.toUpperCase() + "_",
|
||||
envVarPrefix: exchangeType.toUpperCase(),
|
||||
isolatedMargin: isIsolatedMargin,
|
||||
isolatedMarginSymbol: isolatedMarginSymbol,
|
||||
}
|
||||
|
|
|
@ -81,11 +81,11 @@ func TestLoadConfig(t *testing.T) {
|
|||
"sessions": map[string]interface{}{
|
||||
"max": map[string]interface{}{
|
||||
"exchange": "max",
|
||||
"envVarPrefix": "MAX_",
|
||||
"envVarPrefix": "MAX",
|
||||
},
|
||||
"binance": map[string]interface{}{
|
||||
"exchange": "binance",
|
||||
"envVarPrefix": "BINANCE_",
|
||||
"envVarPrefix": "BINANCE",
|
||||
},
|
||||
},
|
||||
"build": map[string]interface{}{
|
||||
|
|
4
pkg/bbgo/testdata/strategy.yaml
vendored
4
pkg/bbgo/testdata/strategy.yaml
vendored
|
@ -2,10 +2,10 @@
|
|||
sessions:
|
||||
max:
|
||||
exchange: max
|
||||
envVarPrefix: MAX_
|
||||
envVarPrefix: MAX
|
||||
binance:
|
||||
exchange: binance
|
||||
envVarPrefix: BINANCE_
|
||||
envVarPrefix: BINANCE
|
||||
|
||||
exchangeStrategies:
|
||||
- on: ["binance"]
|
||||
|
|
|
@ -2,9 +2,10 @@ package cmdutil
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/exchange/binance"
|
||||
"github.com/c9s/bbgo/pkg/exchange/max"
|
||||
|
@ -35,10 +36,12 @@ func NewExchangeWithEnvVarPrefix(n types.ExchangeName, varPrefix string) (types.
|
|||
varPrefix = n.String()
|
||||
}
|
||||
|
||||
key := viper.GetString(varPrefix + "-api-key")
|
||||
secret := viper.GetString(varPrefix + "-api-secret")
|
||||
varPrefix = strings.ToUpper(varPrefix)
|
||||
|
||||
key := os.Getenv(varPrefix + "_API_KEY")
|
||||
secret := os.Getenv(varPrefix + "_API_SECRET")
|
||||
if len(key) == 0 || len(secret) == 0 {
|
||||
return nil, errors.New("max: empty key or secret")
|
||||
return nil, fmt.Errorf("%s: empty key or secret, env var prefix: %s", n, varPrefix)
|
||||
}
|
||||
|
||||
return NewExchangeStandard(n, key, secret)
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
|
@ -551,6 +552,21 @@ func (s *Server) setupRestart(c *gin.Context) {
|
|||
|
||||
logrus.Info("web server shutdown completed")
|
||||
|
||||
|
||||
bin := os.Args[0]
|
||||
args := os.Args[0:]
|
||||
|
||||
// filter out setup parameters
|
||||
args = filterStrings(args, "--setup")
|
||||
|
||||
envVars := os.Environ()
|
||||
|
||||
logrus.Infof("%s %v %+v", bin, args, envVars)
|
||||
|
||||
if err := syscall.Exec(bin, args, envVars) ; err != nil {
|
||||
logrus.WithError(err).Errorf("failed to restart %s", bin)
|
||||
}
|
||||
|
||||
s.Setup.Cancel()
|
||||
}()
|
||||
|
||||
|
@ -673,12 +689,12 @@ func collectSessionEnvVars(sessions map[string]*bbgo.ExchangeSession) (envVars m
|
|||
}
|
||||
|
||||
if len(session.EnvVarPrefix) > 0 {
|
||||
envVars[session.EnvVarPrefix+"API_KEY"] = session.Key
|
||||
envVars[session.EnvVarPrefix+"API_SECRET"] = session.Secret
|
||||
envVars[session.EnvVarPrefix+"_API_KEY"] = session.Key
|
||||
envVars[session.EnvVarPrefix+"_API_SECRET"] = session.Secret
|
||||
} else if len(session.Name) > 0 {
|
||||
sn := strings.ToUpper(session.Name)
|
||||
envVars[sn+"API_KEY"] = session.Key
|
||||
envVars[sn+"API_SECRET"] = session.Secret
|
||||
envVars[sn+"_API_KEY"] = session.Key
|
||||
envVars[sn+"_API_SECRET"] = session.Secret
|
||||
} else {
|
||||
err = fmt.Errorf("session %s name or env var prefix is not defined", session.Name)
|
||||
return
|
||||
|
@ -745,3 +761,17 @@ func pingAndOpenURL(ctx context.Context, baseURL string) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func filterStrings(slice []string, needle string) (ns []string) {
|
||||
for _, str := range slice {
|
||||
if str == needle {
|
||||
continue
|
||||
}
|
||||
|
||||
ns = append(ns, str)
|
||||
}
|
||||
|
||||
return ns
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user