1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 10:26:01 -05:00
v2fly/infra/conf/mergers/mergers.go
Jebbs 7a0af318df
refine api, run command and merger (#766)
* mergers code optimize

* api log work with pipe

* remove inbounds/outbounds by tags
*fix rmo flag parse

* cmds description and message optimize
2021-09-04 11:11:45 +01:00

70 lines
1.5 KiB
Go

package mergers
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
import (
"strings"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/infra/conf/json"
)
func init() {
common.Must(RegisterMerger(makeMerger(
core.FormatJSON,
[]string{".json", ".jsonc"},
nil,
)))
common.Must(RegisterMerger(makeMerger(
core.FormatTOML,
[]string{".toml"},
json.FromTOML,
)))
common.Must(RegisterMerger(makeMerger(
core.FormatYAML,
[]string{".yml", ".yaml"},
json.FromYAML,
)))
common.Must(RegisterMerger(
&Merger{
Name: core.FormatAuto,
Extensions: nil,
Merge: Merge,
}),
)
}
// Merger is a configurable format merger for V2Ray config files.
type Merger struct {
Name string
Extensions []string
Merge MergeFunc
}
// MergeFunc is a utility to merge V2Ray config from external source into a map and returns it.
type MergeFunc func(input interface{}, m map[string]interface{}) error
var (
mergersByName = make(map[string]*Merger)
mergersByExt = make(map[string]*Merger)
)
// RegisterMerger add a new Merger.
func RegisterMerger(format *Merger) error {
if _, found := mergersByName[format.Name]; found {
return newError(format.Name, " already registered.")
}
mergersByName[format.Name] = format
for _, ext := range format.Extensions {
lext := strings.ToLower(ext)
if f, found := mergersByExt[lext]; found {
return newError(ext, " already registered to ", f.Name)
}
mergersByExt[lext] = format
}
return nil
}