merge_doc.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package all
  2. import (
  3. "github.com/v2fly/v2ray-core/v4/main/commands/base"
  4. )
  5. var docMerge = &base.Command{
  6. UsageLine: "{{.Exec}} config-merge",
  7. Short: "config merge logic",
  8. Long: `
  9. Merging of config files is applied in following commands:
  10. {{.Exec}} run -c c1.json -c c2.json ...
  11. {{.Exec}} test -c c1.yaml -c c2.yaml ...
  12. {{.Exec}} convert c1.json dir1 ...
  13. Support of yaml is implemented by converting yaml to json,
  14. both merge and load. So we take json as example here.
  15. Suppose we have 2 JSON files,
  16. The 1st one:
  17. {
  18. "log": {"access": "some_value", "loglevel": "debug"},
  19. "inbounds": [{"tag": "in-1"}],
  20. "outbounds": [{"_priority": 100, "tag": "out-1"}],
  21. "routing": {"rules": [
  22. {"_tag":"default_route","inboundTag":["in-1"],"outboundTag":"out-1"}
  23. ]}
  24. }
  25. The 2nd one:
  26. {
  27. "log": {"loglevel": "error"},
  28. "inbounds": [{"tag": "in-2"}],
  29. "outbounds": [{"_priority": -100, "tag": "out-2"}],
  30. "routing": {"rules": [
  31. {"inboundTag":["in-2"],"outboundTag":"out-2"},
  32. {"_tag":"default_route","inboundTag":["in-1.1"],"outboundTag":"out-1.1"}
  33. ]}
  34. }
  35. Output:
  36. {
  37. // loglevel is overwritten
  38. "log": {"access": "some_value", "loglevel": "error"},
  39. "inbounds": [{"tag": "in-1"}, {"tag": "in-2"}],
  40. "outbounds": [
  41. {"tag": "out-2"}, // note the order is affected by priority
  42. {"tag": "out-1"}
  43. ],
  44. "routing": {"rules": [
  45. // note 3 rules are merged into 2, and outboundTag is overwritten,
  46. // because 2 of them has same tag
  47. {"inboundTag":["in-1","in-1.1"],"outboundTag":"out-1.1"}
  48. {"inboundTag":["in-2"],"outboundTag":"out-2"}
  49. ]}
  50. }
  51. Explained:
  52. - Simple values (string, number, boolean) are overwritten, others are merged
  53. - Elements with same "tag" (or "_tag") in an array will be merged
  54. - Add "_priority" property to array elements will help sort the array
  55. `,
  56. }