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, key: apiKey,
secret: apiSecret, secret: apiSecret,
margin: isMargin, margin: isMargin,
envVarPrefix: exchangeType.toUpperCase() + "_", envVarPrefix: exchangeType.toUpperCase(),
isolatedMargin: isIsolatedMargin, isolatedMargin: isIsolatedMargin,
isolatedMarginSymbol: isolatedMarginSymbol, isolatedMarginSymbol: isolatedMarginSymbol,
} }

View File

@ -81,11 +81,11 @@ func TestLoadConfig(t *testing.T) {
"sessions": map[string]interface{}{ "sessions": map[string]interface{}{
"max": map[string]interface{}{ "max": map[string]interface{}{
"exchange": "max", "exchange": "max",
"envVarPrefix": "MAX_", "envVarPrefix": "MAX",
}, },
"binance": map[string]interface{}{ "binance": map[string]interface{}{
"exchange": "binance", "exchange": "binance",
"envVarPrefix": "BINANCE_", "envVarPrefix": "BINANCE",
}, },
}, },
"build": map[string]interface{}{ "build": map[string]interface{}{

View File

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

View File

@ -2,9 +2,10 @@ package cmdutil
import ( import (
"fmt" "fmt"
"os"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/exchange/binance" "github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/exchange/max" "github.com/c9s/bbgo/pkg/exchange/max"
@ -35,10 +36,12 @@ func NewExchangeWithEnvVarPrefix(n types.ExchangeName, varPrefix string) (types.
varPrefix = n.String() varPrefix = n.String()
} }
key := viper.GetString(varPrefix + "-api-key") varPrefix = strings.ToUpper(varPrefix)
secret := viper.GetString(varPrefix + "-api-secret")
key := os.Getenv(varPrefix + "_API_KEY")
secret := os.Getenv(varPrefix + "_API_SECRET")
if len(key) == 0 || len(secret) == 0 { 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) return NewExchangeStandard(n, key, secret)

View File

@ -12,6 +12,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"syscall"
"time" "time"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
@ -551,6 +552,21 @@ func (s *Server) setupRestart(c *gin.Context) {
logrus.Info("web server shutdown completed") 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() s.Setup.Cancel()
}() }()
@ -673,12 +689,12 @@ func collectSessionEnvVars(sessions map[string]*bbgo.ExchangeSession) (envVars m
} }
if len(session.EnvVarPrefix) > 0 { if len(session.EnvVarPrefix) > 0 {
envVars[session.EnvVarPrefix+"API_KEY"] = session.Key envVars[session.EnvVarPrefix+"_API_KEY"] = session.Key
envVars[session.EnvVarPrefix+"API_SECRET"] = session.Secret envVars[session.EnvVarPrefix+"_API_SECRET"] = session.Secret
} else if len(session.Name) > 0 { } else if len(session.Name) > 0 {
sn := strings.ToUpper(session.Name) sn := strings.ToUpper(session.Name)
envVars[sn+"API_KEY"] = session.Key envVars[sn+"_API_KEY"] = session.Key
envVars[sn+"API_SECRET"] = session.Secret envVars[sn+"_API_SECRET"] = session.Secret
} else { } else {
err = fmt.Errorf("session %s name or env var prefix is not defined", session.Name) err = fmt.Errorf("session %s name or env var prefix is not defined", session.Name)
return 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
}