1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-15 01:34:24 -04:00
v2fly/infra/control/config.go

87 lines
1.8 KiB
Go
Raw Normal View History

2019-12-14 08:43:47 -05:00
package control
import (
"bytes"
"io"
"io/ioutil"
"os"
"strings"
"github.com/golang/protobuf/proto"
"v2ray.com/core/common"
"v2ray.com/core/infra/conf"
"v2ray.com/core/infra/conf/serial"
)
2019-12-23 12:06:01 -05:00
// ConfigCommand is the json to pb convert struct
2019-12-14 09:25:52 -05:00
type ConfigCommand struct{}
2019-12-14 08:43:47 -05:00
2019-12-23 12:06:01 -05:00
// Name for cmd usage
2019-12-14 09:25:52 -05:00
func (c *ConfigCommand) Name() string {
2019-12-14 09:24:32 -05:00
return "config"
2019-12-14 08:43:47 -05:00
}
2019-12-23 12:06:01 -05:00
// Description for help usage
2019-12-14 09:25:52 -05:00
func (c *ConfigCommand) Description() Description {
2019-12-14 08:43:47 -05:00
return Description{
Short: "merge multiple json config",
2019-12-14 09:25:52 -05:00
Usage: []string{"v2ctl config config.json c1.json c2.json <url>.json"},
2019-12-14 08:43:47 -05:00
}
}
2019-12-23 12:06:01 -05:00
// Execute real work here.
2019-12-14 09:25:52 -05:00
func (c *ConfigCommand) Execute(args []string) error {
2019-12-14 08:43:47 -05:00
if len(args) < 1 {
return newError("empty config list")
}
conf := &conf.Config{}
for _, arg := range args {
2019-12-30 22:45:19 -05:00
ctllog.Println("Read config: ", arg)
2019-12-14 08:43:47 -05:00
r, err := c.LoadArg(arg)
common.Must(err)
c, err := serial.DecodeJSONConfig(r)
common.Must(err)
conf.Override(c, arg)
}
pbConfig, err := conf.Build()
if err != nil {
return err
}
bytesConfig, err := proto.Marshal(pbConfig)
if err != nil {
return newError("failed to marshal proto config").Base(err)
}
if _, err := os.Stdout.Write(bytesConfig); err != nil {
return newError("failed to write proto config").Base(err)
}
return nil
}
2019-12-23 12:06:01 -05:00
// LoadArg loads one arg, maybe an remote url, or local file path
2019-12-14 09:25:52 -05:00
func (c *ConfigCommand) LoadArg(arg string) (out io.Reader, err error) {
2019-12-14 08:43:47 -05:00
var data []byte
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
data, err = FetchHTTPContent(arg)
2019-12-14 10:44:10 -05:00
} else if arg == "stdin:" {
data, err = ioutil.ReadAll(os.Stdin)
2019-12-14 08:43:47 -05:00
} else {
data, err = ioutil.ReadFile(arg)
}
if err != nil {
return
}
out = bytes.NewBuffer(data)
return
}
func init() {
2019-12-14 09:25:52 -05:00
common.Must(RegisterCommand(&ConfigCommand{}))
2019-12-14 08:43:47 -05:00
}