remove yaml, toml support

This commit is contained in:
jebbs 2022-10-18 09:25:01 +08:00
parent 55fa255949
commit ed708c3e89
8 changed files with 3 additions and 186 deletions

View File

@ -1,91 +0,0 @@
package main
import (
"os"
"path/filepath"
"github.com/sagernet/sing-box/common/conf"
"github.com/sagernet/sing-box/common/conf/mergers"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"
"github.com/spf13/cobra"
)
var commandConvertOutput string
var commandConvertOutputFormat string
var commandConvert = &cobra.Command{
Use: "convert",
Short: "Convert configuration",
Long: `Convert and merge configuration files between different formats.
Note: comments will be lost after conversion.`,
Run: func(cmd *cobra.Command, args []string) {
err := convert()
if err != nil {
log.Fatal(err)
}
},
Args: cobra.NoArgs,
}
func init() {
commandConvert.Flags().StringVarP(&commandConvertOutput, "output", "o", "", "output file")
commandConvert.Flags().StringVarP(&commandConvertOutputFormat, "output-format", "f", string(mergers.FormatJSON), "output format: json, yaml, toml")
mainCommand.AddCommand(commandConvert)
}
func convert() error {
var (
configContent []byte
err error
)
format := mergers.ParseFormat(configFormat)
outFormat := mergers.ParseFormat(commandConvertOutputFormat)
switch outFormat {
case mergers.FormatJSON,
mergers.FormatYAML,
mergers.FormatTOML:
// ok
default:
return E.New("unsupported output format: ", commandConvertOutputFormat)
}
if len(configPaths) == 1 && configPaths[0] == "stdin" {
configContent, err = conf.ReaderToJSON(os.Stdin, format)
} else {
configContent, err = conf.FilesToJSON(configPaths, format, configRecursive)
}
if err != nil {
return E.Cause(err, "read config")
}
var options option.Options
err = options.UnmarshalJSON(configContent)
if err != nil {
return E.Cause(err, "decode config")
}
content, err := conf.Sprint(options, outFormat)
if err != nil {
return E.Cause(err, "encode config")
}
if commandConvertOutput == "" {
os.Stdout.WriteString(content + "\n")
return nil
}
output, err := os.Create(commandConvertOutput)
if err != nil {
return E.Cause(err, "open output")
}
_, err = output.WriteString(content)
output.Close()
if err != nil {
return E.Cause(err, "write output")
}
outputPath, _ := filepath.Abs(commandConvertOutput)
os.Stderr.WriteString(outputPath + "\n")
return nil
}

View File

@ -6,7 +6,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/sagernet/sing-box/common/conf/mergers"
"github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/common/json"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
@ -19,7 +18,7 @@ var commandFormatFlagWrite bool
var commandFormat = &cobra.Command{ var commandFormat = &cobra.Command{
Use: "format", Use: "format",
Short: "Format configuration (json only)", Short: "Format configuration",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
err := format() err := format()
if err != nil { if err != nil {
@ -39,16 +38,9 @@ func format() error {
return E.New("only one file can be formatted at a time") return E.New("only one file can be formatted at a time")
} }
configPath := configPaths[0] configPath := configPaths[0]
format := mergers.ParseFormat(configFormat) if strings.EqualFold(filepath.Ext(configPath), ".json") {
isJSON := strings.EqualFold(filepath.Ext(configPath), ".json")
// only json is supported, since:
// 1. all other foramts will lost comments after formatting
// 2. fields order is shuffled for yaml and toml, because of the nature of the go map
if format != mergers.FormatJSON &&
(format == mergers.FormatAuto && !isJSON) {
return E.New("only json format is supported") return E.New("only json format is supported")
} }
configContent, err := os.ReadFile(configPath) configContent, err := os.ReadFile(configPath)
if err != nil { if err != nil {
return E.Cause(err, "read config") return E.Cause(err, "read config")

View File

@ -37,7 +37,7 @@ func readConfig() (option.Options, error) {
configContent []byte configContent []byte
err error err error
) )
format := mergers.ParseFormat(configFormat) format := mergers.FormatAuto
if len(configPaths) == 1 && configPaths[0] == "stdin" { if len(configPaths) == 1 && configPaths[0] == "stdin" {
configContent, err = conf.ReaderToJSON(os.Stdin, format) configContent, err = conf.ReaderToJSON(os.Stdin, format)
} else { } else {

View File

@ -3,7 +3,6 @@ package main
import ( import (
"os" "os"
"github.com/sagernet/sing-box/common/conf/mergers"
_ "github.com/sagernet/sing-box/include" _ "github.com/sagernet/sing-box/include"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
@ -12,7 +11,6 @@ import (
var ( var (
configPaths []string configPaths []string
configFormat string
configRecursive bool configRecursive bool
workingDir string workingDir string
disableColor bool disableColor bool
@ -25,7 +23,6 @@ var mainCommand = &cobra.Command{
func init() { func init() {
mainCommand.PersistentFlags().StringArrayVarP(&configPaths, "config", "c", []string{"config.json"}, "set configuration files / directories") mainCommand.PersistentFlags().StringArrayVarP(&configPaths, "config", "c", []string{"config.json"}, "set configuration files / directories")
mainCommand.PersistentFlags().StringVarP(&configFormat, "config-format", "", string(mergers.FormatAuto), "configuration files format: auto, json, jsonc, yaml, toml")
mainCommand.PersistentFlags().BoolVarP(&configRecursive, "config-recursive", "r", false, "load configuration directories recursively") mainCommand.PersistentFlags().BoolVarP(&configRecursive, "config-recursive", "r", false, "load configuration directories recursively")
mainCommand.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory") mainCommand.PersistentFlags().StringVarP(&workingDir, "directory", "D", "", "set working directory")
mainCommand.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output") mainCommand.PersistentFlags().BoolVarP(&disableColor, "disable-color", "", false, "disable color output")

View File

@ -6,11 +6,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/pelletier/go-toml"
"github.com/sagernet/sing-box/common/conf/jsonc" "github.com/sagernet/sing-box/common/conf/jsonc"
"github.com/sagernet/sing-box/common/conf/merge"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"gopkg.in/yaml.v2"
) )
func init() { func init() {
@ -36,30 +33,6 @@ func init() {
return m, nil return m, nil
}, },
))) )))
common.Must(registerMerger(makeMerger(
FormatTOML,
[]string{".toml"},
func(v []byte) (map[string]interface{}, error) {
m := make(map[string]interface{})
if err := toml.Unmarshal(v, &m); err != nil {
return nil, err
}
return m, nil
},
)))
common.Must(registerMerger(makeMerger(
FormatYAML,
[]string{".yml", ".yaml"},
func(v []byte) (map[string]interface{}, error) {
m1 := make(map[interface{}]interface{})
err := yaml.Unmarshal(v, &m1)
if err != nil {
return nil, err
}
m2 := merge.Convert(m1)
return m2, nil
},
)))
common.Must(registerMerger( common.Must(registerMerger(
&Merger{ &Merger{
Name: FormatAuto, Name: FormatAuto,

View File

@ -1,50 +0,0 @@
package conf
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pelletier/go-toml"
"github.com/sagernet/sing-box/common/conf/mergers"
"gopkg.in/yaml.v2"
)
// Sprint returns the text of give format for v
func Sprint(v interface{}, format mergers.Format) (string, error) {
var (
out []byte
err error
)
buffer := new(bytes.Buffer)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
err = encoder.Encode(v)
if err != nil {
return "", fmt.Errorf("failed to convert to json: %s", err)
}
if format == mergers.FormatJSON || format == mergers.FormatJSONC {
return string(buffer.Bytes()), nil
}
m := make(map[string]interface{})
err = json.Unmarshal(buffer.Bytes(), &m)
if err != nil {
return "", err
}
switch format {
case mergers.FormatTOML:
out, err = toml.Marshal(m)
if err != nil {
return "", fmt.Errorf("failed to convert to toml: %s", err)
}
case mergers.FormatYAML:
out, err = yaml.Marshal(m)
if err != nil {
return "", fmt.Errorf("failed to convert to yaml: %s", err)
}
default:
return "", fmt.Errorf("invalid output format: %s", format)
}
return string(out), nil
}

2
go.mod
View File

@ -20,7 +20,6 @@ require (
github.com/mholt/acmez v1.0.4 github.com/mholt/acmez v1.0.4
github.com/miekg/dns v1.1.50 github.com/miekg/dns v1.1.50
github.com/oschwald/maxminddb-golang v1.10.0 github.com/oschwald/maxminddb-golang v1.10.0
github.com/pelletier/go-toml v1.9.5
github.com/pires/go-proxyproto v0.6.2 github.com/pires/go-proxyproto v0.6.2
github.com/refraction-networking/utls v1.1.2 github.com/refraction-networking/utls v1.1.2
github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb
@ -42,7 +41,6 @@ require (
golang.zx2c4.com/wireguard v0.0.0-20220829161405-d1d08426b27b golang.zx2c4.com/wireguard v0.0.0-20220829161405-d1d08426b27b
google.golang.org/grpc v1.49.0 google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.28.1 google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v2 v2.4.0
gvisor.dev/gvisor v0.0.0-20220901235040-6ca97ef2ce1c gvisor.dev/gvisor v0.0.0-20220901235040-6ca97ef2ce1c
) )

2
go.sum
View File

@ -128,8 +128,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.13.0 h1:7lLHu94wT9Ij0o6EWWclhu0aOh32VxhkwEJvzuWPeak= github.com/onsi/gomega v1.13.0 h1:7lLHu94wT9Ij0o6EWWclhu0aOh32VxhkwEJvzuWPeak=
github.com/oschwald/maxminddb-golang v1.10.0 h1:Xp1u0ZhqkSuopaKmk1WwHtjF0H9Hd9181uj2MQ5Vndg= github.com/oschwald/maxminddb-golang v1.10.0 h1:Xp1u0ZhqkSuopaKmk1WwHtjF0H9Hd9181uj2MQ5Vndg=
github.com/oschwald/maxminddb-golang v1.10.0/go.mod h1:Y2ELenReaLAZ0b400URyGwvYxHV1dLIxBuyOsyYjHK0= github.com/oschwald/maxminddb-golang v1.10.0/go.mod h1:Y2ELenReaLAZ0b400URyGwvYxHV1dLIxBuyOsyYjHK0=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pires/go-proxyproto v0.6.2 h1:KAZ7UteSOt6urjme6ZldyFm4wDe/z0ZUP0Yv0Dos0d8= github.com/pires/go-proxyproto v0.6.2 h1:KAZ7UteSOt6urjme6ZldyFm4wDe/z0ZUP0Yv0Dos0d8=
github.com/pires/go-proxyproto v0.6.2/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY= github.com/pires/go-proxyproto v0.6.2/go.mod h1:Odh9VFOZJCf9G8cLW5o435Xf1J95Jw9Gw5rnCjcwzAY=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=