v2ray-core/main/confloader/external/external.go

49 lines
1.0 KiB
Go
Raw Normal View History

2018-04-09 09:43:13 +00:00
package external
import (
"io"
"os"
"strings"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/platform/ctlcmd"
"v2ray.com/core/main/confloader"
)
2018-09-30 21:08:41 +00:00
//go:generate errorgen
2018-04-09 09:43:13 +00:00
func loadConfigFile(configFile string) (io.ReadCloser, error) {
if configFile == "stdin:" {
return os.Stdin, nil
}
if strings.HasPrefix(configFile, "http://") || strings.HasPrefix(configFile, "https://") {
content, err := ctlcmd.Run([]string{"fetch", configFile}, nil)
if err != nil {
return nil, err
}
2018-11-18 18:36:36 +00:00
return &buf.MultiBufferContainer{
MultiBuffer: content,
}, nil
2018-04-09 09:43:13 +00:00
}
fixedFile := os.ExpandEnv(configFile)
file, err := os.Open(fixedFile)
if err != nil {
return nil, newError("config file not readable").Base(err)
}
defer file.Close()
2018-11-18 18:36:36 +00:00
content, err := buf.ReadFrom(file)
2018-04-09 09:43:13 +00:00
if err != nil {
return nil, newError("failed to load config file: ", fixedFile).Base(err).AtWarning()
}
2018-11-18 18:36:36 +00:00
return &buf.MultiBufferContainer{
MultiBuffer: content,
}, nil
2018-04-09 09:43:13 +00:00
}
func init() {
confloader.EffectiveConfigFileLoader = loadConfigFile
}