github.com/imannamdari/v2ray-core/v5@v5.0.5/main/commands/all/engineering/reversepb.go (about)

     1  package engineering
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"io"
     7  	"os"
     8  
     9  	"github.com/golang/protobuf/proto"
    10  
    11  	core "github.com/imannamdari/v2ray-core/v5"
    12  	"github.com/imannamdari/v2ray-core/v5/infra/conf/jsonpb"
    13  	"github.com/imannamdari/v2ray-core/v5/infra/conf/v2jsonpb"
    14  	"github.com/imannamdari/v2ray-core/v5/main/commands/base"
    15  )
    16  
    17  var cmdReversePb = &base.Command{
    18  	UsageLine: "{{.Exec}} engineering reversepb [-f format]",
    19  	Flag: func() flag.FlagSet {
    20  		fs := flag.NewFlagSet("", flag.ExitOnError)
    21  		configFormat = fs.String("f", "v2jsonpb", "")
    22  		return *fs
    23  	}(),
    24  	Run: func(cmd *base.Command, args []string) {
    25  		cmd.Flag.Parse(args)
    26  		configIn := bytes.NewBuffer(nil)
    27  		io.Copy(configIn, os.Stdin)
    28  		var conf core.Config
    29  		if err := proto.Unmarshal(configIn.Bytes(), &conf); err != nil {
    30  			base.Fatalf("%s", err)
    31  		}
    32  		switch *configFormat {
    33  		case "jsonpb":
    34  			if err := jsonpb.DumpJSONPb(&conf, os.Stdout); err != nil {
    35  				base.Fatalf("%s", err)
    36  			}
    37  		case "v2jsonpb":
    38  			if value, err := v2jsonpb.DumpV2JsonPb(&conf); err != nil {
    39  				base.Fatalf("%s", err)
    40  			} else {
    41  				io.Copy(os.Stdout, bytes.NewReader(value))
    42  			}
    43  		}
    44  	},
    45  }