mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-04-12 16:55:48 -04:00
normalized env variable names for bash
This commit is contained in:
42
common/platform/platform.go
Normal file
42
common/platform/platform.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type EnvFlag struct {
|
||||
Name string
|
||||
AltName string
|
||||
}
|
||||
|
||||
func (f EnvFlag) GetValue(defaultValue string) string {
|
||||
if v, found := os.LookupEnv(f.Name); found {
|
||||
return v
|
||||
}
|
||||
if len(f.AltName) > 0 {
|
||||
if v, found := os.LookupEnv(f.AltName); found {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
func (f EnvFlag) GetValueAsInt(defaultValue int) int {
|
||||
const PlaceHolder = "xxxxxx"
|
||||
s := f.GetValue(PlaceHolder)
|
||||
if s == PlaceHolder {
|
||||
return defaultValue
|
||||
}
|
||||
v, err := strconv.ParseInt(s, 10, 32)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return int(v)
|
||||
}
|
||||
|
||||
func NormalizeEnvName(name string) string {
|
||||
return strings.Replace(strings.ToUpper(strings.TrimSpace(name)), ".", "_", -1)
|
||||
}
|
||||
41
common/platform/platform_test.go
Normal file
41
common/platform/platform_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package platform_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "v2ray.com/core/common/platform"
|
||||
"v2ray.com/core/testing/assert"
|
||||
)
|
||||
|
||||
func TestNormalizeEnvName(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
cases := []struct {
|
||||
input string
|
||||
output string
|
||||
}{
|
||||
{
|
||||
input: "a",
|
||||
output: "A",
|
||||
},
|
||||
{
|
||||
input: "a.a",
|
||||
output: "A_A",
|
||||
},
|
||||
{
|
||||
input: "A.A.B",
|
||||
output: "A_A_B",
|
||||
},
|
||||
}
|
||||
for _, test := range cases {
|
||||
assert.String(NormalizeEnvName(test.input)).Equals(test.output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvFlag(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
assert.Int(EnvFlag{
|
||||
Name: "xxxxx.y",
|
||||
}.GetValueAsInt(10)).Equals(10)
|
||||
}
|
||||
Reference in New Issue
Block a user