1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 19:45:24 +00:00
v2fly/common/platform/platform.go

86 lines
1.7 KiB
Go
Raw Normal View History

2018-04-03 20:34:59 +00:00
package platform // import "v2ray.com/core/common/platform"
2017-05-25 23:11:38 +00:00
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
}
2018-07-18 08:40:28 +00:00
func NewEnvFlag(name string) EnvFlag {
return EnvFlag{
Name: name,
AltName: NormalizeEnvName(name),
}
}
2017-11-06 11:23:21 +00:00
func (f EnvFlag) GetValue(defaultValue func() string) string {
2017-05-25 23:11:38 +00:00
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
}
}
2017-11-06 11:23:21 +00:00
return defaultValue()
2017-05-25 23:11:38 +00:00
}
func (f EnvFlag) GetValueAsInt(defaultValue int) int {
2017-11-06 11:23:21 +00:00
useDefaultValue := false
s := f.GetValue(func() string {
useDefaultValue = true
return ""
})
if useDefaultValue {
2017-05-25 23:11:38 +00:00
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
func getExecutableDir() string {
exec, err := os.Executable()
if err != nil {
return ""
}
return filepath.Dir(exec)
}
2018-03-08 09:32:36 +00:00
func getExecutableSubDir(dir string) func() string {
2017-11-22 22:00:46 +00:00
return func() string {
return filepath.Join(getExecutableDir(), dir)
}
}
2017-11-04 20:33:09 +00:00
func GetAssetLocation(file string) string {
const name = "v2ray.location.asset"
2018-07-18 08:40:28 +00:00
assetPath := NewEnvFlag(name).GetValue(getExecutableDir)
2017-11-04 20:33:09 +00:00
return filepath.Join(assetPath, file)
}
2017-11-22 22:00:46 +00:00
func GetPluginDirectory() string {
const name = "v2ray.location.plugin"
2018-07-18 08:40:28 +00:00
pluginDir := NewEnvFlag(name).GetValue(getExecutableSubDir("plugins"))
2017-11-22 22:00:46 +00:00
return pluginDir
}
2017-12-05 10:58:12 +00:00
func GetConfigurationPath() string {
const name = "v2ray.location.config"
2018-07-18 08:40:28 +00:00
configPath := NewEnvFlag(name).GetValue(getExecutableDir)
2017-12-05 10:58:12 +00:00
return filepath.Join(configPath, "config.json")
}