v2ray-core/common/platform/platform.go

60 lines
1.1 KiB
Go
Raw Normal View History

2017-05-25 23:11:38 +00:00
package platform
import (
"os"
2017-11-04 20:33:09 +00:00
"path/filepath"
2017-05-25 23:11:38 +00:00
"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)
}
2017-11-04 20:33:09 +00:00
var assetPath = "/"
func init() {
defAssetLocation, err := os.Executable()
if err == nil {
defAssetLocation = filepath.Dir(defAssetLocation)
assetPath = (EnvFlag{
Name: "v2ray.location.asset",
}).GetValue(defAssetLocation)
}
}
func GetAssetLocation(file string) string {
return filepath.Join(assetPath, file)
}