Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package engineering
 
import (
    "bytes"
    "fmt"
    "io"
    "os"
 
    "google.golang.org/protobuf/proto"
 
    core "github.com/v2fly/v2ray-core/v5"
    "github.com/v2fly/v2ray-core/v5/common/cmdarg"
    "github.com/v2fly/v2ray-core/v5/main/commands/base"
)
 
var (
    configFiles          cmdarg.Arg
    configDirs           cmdarg.Arg
    configFormat         *string
    configDirRecursively *bool
)
 
func setConfigFlags(cmd *base.Command) {
    configFormat = cmd.Flag.String("format", core.FormatAuto, "")
    configDirRecursively = cmd.Flag.Bool("r", false, "")
 
    cmd.Flag.Var(&configFiles, "config", "")
    cmd.Flag.Var(&configFiles, "c", "")
    cmd.Flag.Var(&configDirs, "confdir", "")
    cmd.Flag.Var(&configDirs, "d", "")
}
 
var cmdConvertPb = &base.Command{
    UsageLine:   "{{.Exec}} engineering convertpb [-c config.json] [-d dir]",
    CustomFlags: true,
    Run: func(cmd *base.Command, args []string) {
        setConfigFlags(cmd)
        cmd.Flag.Parse(args)
        config, err := core.LoadConfig(*configFormat, configFiles)
        if err != nil {
            if len(configFiles) == 0 {
                base.Fatalf("%s", newError("failed to load config").Base(err))
                return
            }
            base.Fatalf("%s", newError(fmt.Sprintf("failed to load config: %s", configFiles)).Base(err))
            return
        }
        bytew, err := proto.Marshal(config)
        if err != nil {
            base.Fatalf("%s", newError("failed to marshal config").Base(err))
            return
        }
        io.Copy(os.Stdout, bytes.NewReader(bytew))
    },
}