fix env var prefix by using os.Getenv directly

This commit is contained in:
c9s 2021-02-04 15:14:54 +08:00
parent e6b0a0c595
commit d8d1249293
5 changed files with 46 additions and 13 deletions

View File

@ -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,
}

View File

@ -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{}{

View File

@ -2,10 +2,10 @@
sessions:
max:
exchange: max
envVarPrefix: MAX_
envVarPrefix: MAX
binance:
exchange: binance
envVarPrefix: BINANCE_
envVarPrefix: BINANCE
exchangeStrategies:
- on: ["binance"]

View File

@ -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)

View File

@ -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
}